Skip to content

Commit

Permalink
pkp#229 OrcidProfilePlugin-705
Browse files Browse the repository at this point in the history
  • Loading branch information
withanage committed Feb 8, 2023
1 parent fafbdac commit 38583ac
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 71 deletions.
143 changes: 73 additions & 70 deletions OrcidProfileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use APP\facades\Repo;
use APP\handler\Handler;
use APP\template\TemplateManager;
use Carbon\Carbon;
use PKP\core\Core;
use PKP\plugins\PluginRegistry;
use PKP\security\authorization\PKPSiteAccessPolicy;
Expand All @@ -32,8 +33,7 @@
class OrcidProfileHandler extends Handler
{
public const TEMPLATE = 'orcidVerify.tpl';

protected $plugin;
const ORCIDPROFILEPLUGIN = 'orcidprofileplugin';


public function __construct()
Expand Down Expand Up @@ -78,7 +78,7 @@ public function orcidAuthorize($args, $request)
{
$context = $request->getContext();
$op = $request->getRequestedOp();
$plugin = PluginRegistry::getPlugin('generic', 'orcidprofileplugin');
$plugin = PluginRegistry::getPlugin('generic', self::ORCIDPROFILEPLUGIN);
$contextId = ($context == null) ? \PKP\core\PKPApplication::CONTEXT_ID_NONE : $context->getId();
$httpClient = Application::get()->getHttpClient();

Expand Down Expand Up @@ -172,7 +172,8 @@ public function orcidAuthorize($args, $request)
</script></body></html>
';
break;
default: assert(false);
default:
assert(false);
}
}

Expand All @@ -187,12 +188,12 @@ public function orcidVerify($args, $request)
$templateMgr = TemplateManager::getManager($request);
$context = $request->getContext();
$contextId = $context == null ? \PKP\core\PKPApplication::CONTEXT_ID_NONE : $context->getId();
$plugin = PluginRegistry::getPlugin('generic', self::ORCIDPROFILEPLUGIN);

$plugin = $this->plugin;
$templatePath = $plugin->getTemplateResource(self::TEMPLATE);


$publicationId = $request->getUserVar('publicationId');
$publicationId = $request->getUserVar('state');
$authors = Repo::author()
->getCollector()
->filterByPublicationIds([$publicationId])
Expand Down Expand Up @@ -263,80 +264,82 @@ public function orcidVerify($args, $request)
$plugin->logInfo('POST ' . $url);
$plugin->logInfo('Request header: ' . var_export($header, true));
$plugin->logInfo('Request body: ' . http_build_query($postData));
try {
$response = $httpClient->request(
'POST',
$url,
[
'headers' => $header,
'form_params' => $postData,
]
);
if ($response->getStatusCode() != 200) {
$plugin->logError('OrcidProfileHandler::orcidverify - unexpected response: ' . $response->getStatusCode());
$templateMgr->assign('authFailure', true);

$response = $httpClient->request(
'POST',
$url,
[
'headers' => $header,
'form_params' => $postData,
]
);
if ($response->getStatusCode() != 200) {
$plugin->logError('OrcidProfileHandler::orcidverify - unexpected response: ' . $response->getStatusCode());
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
return;
}
$response = json_decode($response->getBody(), true);
}
$response = json_decode($response->getBody(), true);

$plugin->logInfo('Response body: ' . print_r($response, true));
if (($response['error'] ?? null) === 'invalid_grant') {
$plugin->logError('Authorization code invalid, maybe already used');
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
return;
}
if (isset($response['error'])) {
$plugin->logError("Invalid ORCID response: " . $response['error']);
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
}
// Set the orcid id using the full https uri
$orcidUri = ($plugin->getSetting($contextId, 'isSandBox') == true ? 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);
$templateMgr->display($templatePath);
return;
}
$authorToVerify->setOrcid($orcidUri);
if (in_array($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);
}
$plugin->logInfo('Response body: ' . print_r($response, true));
if (($response['error'] ?? null) === 'invalid_grant') {
$plugin->logError('Authorization code invalid, maybe already used');
$templateMgr->assign('authFailure', true);

// remove the email token
$authorToVerify->setData('orcidEmailToken', null);
$this->_setOrcidData($authorToVerify, $orcidUri, $response);
Repo::author()->dao->update($authorToVerify);
if ($plugin->isMemberApiEnabled($contextId)) {
if ($publication->getData('status') == PKPSubmission::STATUS_PUBLISHED) {
$templateMgr->assign('sendSubmission', true);
$sendResult = $plugin->sendSubmissionToOrcid($publication, $request);
if ($sendResult === true || (is_array($sendResult) && $sendResult[$response['orcid']])) {
$templateMgr->assign('sendSubmissionSuccess', true);
}
if (isset($response['error'])) {
$plugin->logError("Invalid ORCID response: " . $response['error']);
$templateMgr->assign('authFailure', true);
}
// Set the orcid id using the full https uri
$orcidUri = ($plugin->getSetting($contextId, 'isSandBox') == true ? 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($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('submissionNotPublished', true);
$templateMgr->assign('orcid', $orcidUri);
}
}

$templateMgr->assign([
'verifySuccess' => true,
'orcidIcon' => $plugin->getIcon()
]);
// remove the email token
$authorToVerify->setData('orcidEmailToken', null);
$this->_setOrcidData($authorToVerify, $orcidUri, $response);
Repo::author()->dao->update($authorToVerify);
if ($plugin->isMemberApiEnabled($contextId)) {
if ($publication->getData('status') == PKPSubmission::STATUS_PUBLISHED) {
$templateMgr->assign('sendSubmission', true);
$sendResult = $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' => $plugin->getIcon()
]);


} catch (\GuzzleHttp\Exception\ClientException $exception) {
$reason = $exception->getResponse()->getBody(false);
$plugin->logInfo("Publication fail: ${reason}");

}
$templateMgr->assign('authFailure', true);
$templateMgr->display($templatePath);
}

public function _setOrcidData($userOrAuthor, $orcidUri, $orcidResponse)
{
// Save the access token
$orcidAccessExpiresOn = Carbon\Carbon::now();
$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']);
Expand All @@ -360,10 +363,10 @@ public function about($args, $request)
$context = $request->getContext();
$contextId = $context == null ? \PKP\core\PKPApplication::CONTEXT_ID_NONE : $context->getId();
$templateMgr = TemplateManager::getManager($request);
$plugin = PluginRegistry::getPlugin('generic', 'orcidprofileplugin');
$templateMgr->assign('orcidIcon', $plugin->getIcon());
$templateMgr->assign('isMemberApi', $plugin->isMemberApiEnabled($contextId));
$templateMgr->display($plugin->getTemplateResource('orcidAbout.tpl'));
$plugin = PluginRegistry::getPlugin('generic', self::ORCIDPROFILEPLUGIN);
$templateMgr->assign('orcidIcon', $plugin->getIcon());
$templateMgr->assign('isMemberApi', $plugin->isMemberApiEnabled($contextId));
$templateMgr->display($plugin->getTemplateResource('orcidAbout.tpl'));
}


Expand Down
2 changes: 1 addition & 1 deletion OrcidProfilePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ public function sendAuthorMail($author, $updateAuthor = false)

$emailToken = md5(microtime() . $author->getEmail());
$author->setData('orcidEmailToken', $emailToken);
$oauthUrl = $this->buildOAuthUrl('orcidVerify', ['token' => $emailToken, 'publicationId' => $publicationId]);
$oauthUrl = $this->buildOAuthUrl('orcidVerify', ['token' => $emailToken, 'state' => $publicationId]);

if ($this->isMemberApiEnabled($contextId)) {
$mailable = new OrcidRequestAuthorAuthorization($context, $submission, $oauthUrl);
Expand Down

0 comments on commit 38583ac

Please sign in to comment.