forked from sean-xiao-zhao7/pkp-orcidprofile
-
Notifications
You must be signed in to change notification settings - Fork 51
/
OrcidProfileHandler.php
375 lines (341 loc) · 16.5 KB
/
OrcidProfileHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
* @file OrcidProfileHandler.php
*
* Copyright (c) 2015-2019 University of Pittsburgh
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v2 or later. For full terms see the file docs/COPYING.
*
* @class OrcidProfileHandler
*
* @ingroup plugins_generic_orcidprofile
*
* @brief Pass off internal ORCID API requests to ORCID
*/
namespace APP\plugins\generic\orcidProfile;
use APP\core\Application;
use APP\core\Request;
use APP\facades\Repo;
use APP\handler\Handler;
use APP\template\TemplateManager;
use Carbon\Carbon;
use Exception;
use PKP\core\Core;
use PKP\core\PKPRequest;
use PKP\core\PKPSessionGuard;
use PKP\plugins\PluginRegistry;
use PKP\security\authorization\PKPSiteAccessPolicy;
use PKP\security\authorization\UserRequiredPolicy;
use PKP\submission\PKPSubmission;
class OrcidProfileHandler extends Handler
{
public const TEMPLATE = 'orcidVerify.tpl';
public const ORCIDPROFILEPLUGIN = 'orcidprofileplugin';
private bool $isSandBox;
private OrcidProfilePlugin $plugin;
public function __construct()
{
$request = Application::get()->getRequest();
$context = $request->getContext();
$contextId = $context?->getId() ?? \PKP\core\PKPApplication::CONTEXT_ID_NONE;
/** @var OrcidProfilePlugin */
$plugin = PluginRegistry::getPlugin('generic', self::ORCIDPROFILEPLUGIN);
$this->plugin = $plugin;
$this->isSandBox = $this->plugin->getSetting($contextId, 'orcidProfileAPIPath') == ORCID_API_URL_MEMBER_SANDBOX ||
$this->plugin->getSetting($contextId, 'orcidProfileAPIPath') == ORCID_API_URL_PUBLIC_SANDBOX;
}
/**
* @copydoc PKPHandler::authorize()
*/
public function authorize($request, &$args, $roleAssignments)
{
// Authorize all requests
$this->addPolicy(new PKPSiteAccessPolicy(
$request,
['orcidVerify', 'orcidAuthorize', 'about'],
PKPSiteAccessPolicy::SITE_ACCESS_ALL_ROLES
));
$op = $request->getRequestedOp();
$targetOp = $request->getUserVar('targetOp');
if ($op === 'orcidAuthorize' && in_array($targetOp, ['profile', 'submit'])) {
// ... but user must be logged in for orcidAuthorize with profile or submit
$this->addPolicy(new UserRequiredPolicy($request));
}
if (!Application::isInstalled()) {
PKPSessionGuard::disableSession();
}
$this->setEnforceRestrictedSite(false);
return parent::authorize($request, $args, $roleAssignments);
}
/**
* Authorize handler
*
* @param array $args
* @param Request $request
*/
public function orcidAuthorize($args, $request)
{
$context = $request->getContext();
$contextId = $context?->getId() ?? \PKP\core\PKPApplication::CONTEXT_ID_NONE;
$httpClient = Application::get()->getHttpClient();
// API request: Get an OAuth token and ORCID.
$response = $httpClient->request(
'POST',
$url = $this->plugin->getSetting($contextId, 'orcidProfileAPIPath') . OAUTH_TOKEN_URL,
[
'form_params' => [
'code' => $request->getUserVar('code'),
'grant_type' => 'authorization_code',
'client_id' => $this->plugin->getSetting($contextId, 'orcidClientId'),
'client_secret' => $this->plugin->getSetting($contextId, 'orcidClientSecret')
],
'headers' => ['Accept' => 'application/json'],
'allow_redirects' => ['strict' => true],
]
);
if ($response->getStatusCode() != 200) {
error_log('ORCID token URL error: ' . $response->getStatusCode() . ' (' . __FILE__ . ' line ' . __LINE__ . ', URL ' . $url . ')');
$orcidUri = $orcid = $accessToken = null;
} else {
$response = json_decode($response->getBody(), true);
$orcid = $response['orcid'];
$accessToken = $response['access_token'];
$orcidUri = ($this->isSandBox == true ? ORCID_URL_SANDBOX : ORCID_URL) . $orcid;
}
switch ($request->getUserVar('targetOp')) {
case 'register':
// API request: get user profile (for names; email; etc)
$response = $httpClient->request(
'GET',
$url = $this->plugin->getSetting($contextId, 'orcidProfileAPIPath') . ORCID_API_VERSION_URL . urlencode($orcid) . '/' . ORCID_PROFILE_URL,
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
],
]
);
if ($response->getStatusCode() != 200) {
error_log('ORCID profile URL error: ' . $response->getStatusCode() . ' (' . __FILE__ . ' line ' . __LINE__ . ', URL ' . $url . ')');
$profileJson = null;
} else {
$profileJson = json_decode($response->getBody(), true);
}
// API request: get employments (for affiliation field)
$httpClient->request(
'GET',
$url = $this->plugin->getSetting($contextId, 'orcidProfileAPIPath') . ORCID_API_VERSION_URL . urlencode($orcid) . '/' . ORCID_EMPLOYMENTS_URL,
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
],
]
);
if ($response->getStatusCode() != 200) {
error_log('ORCID deployments URL error: ' . $response->getStatusCode() . ' (' . __FILE__ . ' line ' . __LINE__ . ', URL ' . $url . ')');
$employmentJson = null;
} else {
$employmentJson = json_decode($response->getBody(), true);
}
// Suppress errors for nonexistent array indexes
echo '
<html><body><script type="text/javascript">
opener.document.getElementById("givenName").value = ' . json_encode(@$profileJson['name']['given-names']['value']) . ';
opener.document.getElementById("familyName").value = ' . json_encode(@$profileJson['name']['family-name']['value']) . ';
opener.document.getElementById("email").value = ' . json_encode(@$profileJson['emails']['email'][0]['email']) . ';
opener.document.getElementById("country").value = ' . json_encode(@$profileJson['addresses']['address'][0]['country']['value']) . ';
opener.document.getElementById("affiliation").value = ' . json_encode(@$employmentJson['employment-summary'][0]['organization']['name']) . ';
opener.document.getElementById("orcid").value = ' . json_encode($orcidUri) . ';
opener.document.getElementById("connect-orcid-button").style.display = "none";
window.close();
</script></body></html>
';
break;
case 'profile':
$user = $request->getUser();
// Store the access token and other data for the user
$this->_setOrcidData($user, $orcidUri, $response);
Repo::user()->edit($user, ['orcidAccessDenied', 'orcidAccessToken', 'orcidAccessScope', 'orcidRefreshToken', 'orcidAccessExpiresOn']);
// Reload the public profile tab (incl. form)
echo '
<html><body><script type="text/javascript">
opener.$("#profileTabs").tabs("load", 3);
window.close();
</script></body></html>
';
break;
default:
throw new Exception('Invalid target');
}
}
/**
* Verify an incoming author claim for an ORCiD association.
*
* @param array $args
* @param PKPRequest $request
*/
public function orcidVerify($args, $request)
{
$templateMgr = TemplateManager::getManager($request);
$context = $request->getContext();
$contextId = $context?->getId() ?? \PKP\core\PKPApplication::CONTEXT_ID_NONE;
$templatePath = $this->plugin->getTemplateResource(self::TEMPLATE);
$publicationId = $request->getUserVar('state');
$authors = Repo::author()
->getCollector()
->filterByPublicationIds([$publicationId])
->getMany();
$publication = Repo::publication()->get($publicationId);
$authorToVerify = null;
// Find the author entry, for which the ORCID verification was requested
if ($request->getUserVar('token')) {
foreach ($authors as $author) {
if ($author->getData('orcidEmailToken') == $request->getUserVar('token')) {
$authorToVerify = $author;
}
}
}
// Initialise template parameters
$templateMgr->assign([
'currentUrl' => $request->url(null, 'index'),
'verifySuccess' => false,
'authFailure' => false,
'notPublished' => false,
'sendSubmission' => false,
'sendSubmissionSuccess' => false,
'denied' => false,
]);
if ($authorToVerify == null) {
// no Author exists in the database with the supplied orcidEmailToken
$this->plugin->logError('OrcidProfileHandler::orcidverify - No author found with supplied token');
$templateMgr->assign('verifySuccess', false);
$templateMgr->display($templatePath);
return;
}
if ($request->getUserVar('error') === 'access_denied') {
// User denied access
// Store the date time the author denied ORCID access to remember this
$authorToVerify->setData('orcidAccessDenied', Core::getCurrentDate());
// remove all previously stored ORCID access token
$authorToVerify->setData('orcidAccessToken', null);
$authorToVerify->setData('orcidAccessScope', null);
$authorToVerify->setData('orcidRefreshToken', null);
$authorToVerify->setData('orcidAccessExpiresOn', null);
$authorToVerify->setData('orcidEmailToken', null);
Repo::author()->dao->update($authorToVerify);
$this->plugin->logError('OrcidProfileHandler::orcidverify - ORCID access denied. Error description: ' . $request->getUserVar('error_description'));
$templateMgr->assign('denied', true);
$templateMgr->display($templatePath);
return;
}
// fetch the access token
$url = $this->plugin->getSetting($contextId, 'orcidProfileAPIPath') . OAUTH_TOKEN_URL;
$httpClient = Application::get()->getHttpClient();
$header = ['Accept' => 'application/json'];
$postData = [
'code' => $request->getUserVar('code'),
'grant_type' => 'authorization_code',
'client_id' => $this->plugin->getSetting($contextId, 'orcidClientId'),
'client_secret' => $this->plugin->getSetting($contextId, 'orcidClientSecret')
];
$this->plugin->logInfo('POST ' . $url);
$this->plugin->logInfo('Request header: ' . var_export($header, true));
$this->plugin->logInfo('Request body: ' . http_build_query($postData));
try {
$response = $httpClient->request(
'POST',
$url,
[
'headers' => $header,
'form_params' => $postData,
'allow_redirects' => ['strict' => true],
]
);
if ($response->getStatusCode() != 200) {
$this->plugin->logError('OrcidProfileHandler::orcidverify - unexpected response: ' . $response->getStatusCode());
$templateMgr->assign('authFailure', true);
}
$response = json_decode($response->getBody(), true);
$this->plugin->logInfo('Response body: ' . print_r($response, true));
if (($response['error'] ?? null) === 'invalid_grant') {
$this->plugin->logError('Authorization code invalid, maybe already used');
$templateMgr->assign('authFailure', true);
}
if (isset($response['error'])) {
$this->plugin->logError('Invalid ORCID response: ' . $response['error']);
$templateMgr->assign('authFailure', true);
}
// Set the orcid id using the full https uri
$orcidUri = ($this->isSandBox ? ORCID_URL_SANDBOX : ORCID_URL) . $response['orcid'];
if (!empty($authorToVerify->getOrcid()) && $orcidUri != $authorToVerify->getOrcid()) {
// another ORCID id is stored for the author
$templateMgr->assign('duplicateOrcid', true);
}
$authorToVerify->setOrcid($orcidUri);
if (in_array($this->plugin->getSetting($contextId, 'orcidProfileAPIPath'), [ORCID_API_URL_MEMBER_SANDBOX, ORCID_API_URL_PUBLIC_SANDBOX])) {
// Set a flag to mark that the stored orcid id and access token came form the sandbox api
$authorToVerify->setData('orcidSandbox', true);
$templateMgr->assign('orcid', ORCID_URL_SANDBOX . $response['orcid']);
} else {
$templateMgr->assign('orcid', $orcidUri);
}
// remove the email token
$authorToVerify->setData('orcidEmailToken', null);
$this->_setOrcidData($authorToVerify, $orcidUri, $response);
Repo::author()->dao->update($authorToVerify);
if ($this->plugin->isMemberApiEnabled($contextId)) {
if ($publication->getData('status') == PKPSubmission::STATUS_PUBLISHED) {
$templateMgr->assign('sendSubmission', true);
$sendResult = $this->plugin->sendSubmissionToOrcid($publication, $request);
if ($sendResult === true || (is_array($sendResult) && $sendResult[$response['orcid']])) {
$templateMgr->assign('sendSubmissionSuccess', true);
}
} else {
$templateMgr->assign('submissionNotPublished', true);
}
}
$templateMgr->assign([
'verifySuccess' => true,
'orcidIcon' => $this->plugin->getIcon()
]);
} catch (\GuzzleHttp\Exception\ClientException $exception) {
$this->plugin->logInfo('Publication fail:' . $exception->getMessage());
$templateMgr->assign('orcidAPIError', $exception->getMessage());
}
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
}
public function _setOrcidData($userOrAuthor, $orcidUri, $orcidResponse)
{
// Save the access token
$orcidAccessExpiresOn = Carbon::now();
// expires_in field from the response contains the lifetime in seconds of the token
// See https://members.orcid.org/api/get-oauthtoken
$orcidAccessExpiresOn->addSeconds($orcidResponse['expires_in']);
$userOrAuthor->setOrcid($orcidUri);
// remove the access denied marker, because now the access was granted
$userOrAuthor->setData('orcidAccessDenied', null);
$userOrAuthor->setData('orcidAccessToken', $orcidResponse['access_token']);
$userOrAuthor->setData('orcidAccessScope', $orcidResponse['scope']);
$userOrAuthor->setData('orcidRefreshToken', $orcidResponse['refresh_token']);
$userOrAuthor->setData('orcidAccessExpiresOn', $orcidAccessExpiresOn->toDateTimeString());
return $userOrAuthor;
}
/**
* Show explanation and information about ORCID
*
* @param array $args
* @param PKPRequest $request
*/
public function about($args, $request)
{
$context = $request->getContext();
$contextId = $context?->getId() ?? \PKP\core\PKPApplication::CONTEXT_ID_NONE;
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('orcidIcon', $this->plugin->getIcon());
$templateMgr->assign('isMemberApi', $this->plugin->isMemberApiEnabled($contextId));
$templateMgr->display($this->plugin->getTemplateResource('orcidAbout.tpl'));
}
}