Skip to content

Commit

Permalink
pkp#10819 Unable to update OAuth scope for authenticated ORCIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
ewhanson committed Jan 31, 2025
1 parent 704fcb9 commit 1d69c41
Show file tree
Hide file tree
Showing 17 changed files with 646 additions and 210 deletions.
2 changes: 1 addition & 1 deletion classes/core/PKPContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ protected function loadConfiguration(): void

// Cache configuration
$items['cache'] = [
'default' => Config::getVar('cache', 'default', 'opcache'),
'default' => Config::getVar('cache', 'default', 'file'),
'stores' => [
'opcache' => [
'driver' => 'opcache',
Expand Down
52 changes: 52 additions & 0 deletions classes/mail/mailables/OrcidRequestUpdateScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* @file classes/mail/mailables/OrcidRequestUpdateScope.php
*
* Copyright (c) 2014-2025 Simon Fraser University
* Copyright (c) 2000-2025 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class OrcidRequestUpdateScope
*
* @brief An automatic email sent to the update users' ORCID OAuth scope for member API deposits.
*/

namespace PKP\mail\mailables;

use APP\submission\Submission;
use PKP\context\Context;
use PKP\mail\Mailable;
use PKP\mail\traits\Configurable;
use PKP\mail\traits\OrcidVariables;
use PKP\mail\traits\Recipient;
use PKP\security\Role;

class OrcidRequestUpdateScope extends Mailable
{
use Configurable;
use Recipient;
use OrcidVariables;

protected static ?string $name = 'orcid.orcidRequestUpdateScope.name';
protected static ?string $description = 'emails.orcidRequestUpdateScope.description';
protected static ?string $emailTemplateKey = 'ORCID_REQUEST_UPDATE_SCOPE';
protected static array $toRoleIds = [Role::ROLE_ID_AUTHOR, Role::ROLE_ID_REVIEWER];

public function __construct(Context $context, Submission $submission, string $oauthUrl)
{
parent::__construct([$context, $submission]);
$this->setupOrcidVariables($oauthUrl, $context);
}

public static function getDataDescriptions(): array
{
/**
* Adds ORCID URLs to email template
*/
return array_merge(
parent::getDataDescriptions(),
static::getOrcidDataDescriptions()
);
}
}
8 changes: 7 additions & 1 deletion classes/mail/traits/OrcidVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ protected function setupOrcidVariables(string $oauthUrl, Context $context): void

$this->addData([
self::$authorOrcidUrl => $oauthUrl,
self::$orcidAboutUrl => $dispatcher->url($request, Application::ROUTE_PAGE, null, 'orcid', 'about', urlLocaleForPage: ''),
self::$orcidAboutUrl => $dispatcher->url(
$request,
Application::ROUTE_PAGE,
newContext: $context->getPath(),
handler: 'orcid',
op: 'about',
urlLocaleForPage: ''),
self::$principalContactSignature => $principalContact->getLocalizedSignature(),
]);
}
Expand Down
45 changes: 45 additions & 0 deletions classes/migration/upgrade/v3_5_0/I10819_OrcidOauthScopeMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @file classes/migration/upgrade/v3_5_0/I10819_OrcidOauthScopeMail.php
*
* Copyright (c) 2025 Simon Fraser University
* Copyright (c) 2025 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class I10819_OrcidOauthScopeMail
*
* @brief Add new email template for updating users' OAuth scope
*/

namespace PKP\migration\upgrade\v3_5_0;

use APP\facades\Repo;
use PKP\install\DowngradeNotSupportedException;
use PKP\migration\Migration;

class I10819_OrcidOauthScopeMail extends Migration
{

/**
* @inheritDoc
*/
public function up(): void
{
Repo::emailTemplate()->dao->installEmailTemplates(
Repo::emailTemplate()->dao->getMainEmailTemplatesFilename(),
[],
'ORCID_REQUEST_UPDATE_SCOPE',
true,
);
}

/**
* @inheritDoc
* @throws DowngradeNotSupportedException
*/
public function down(): void
{
throw new DowngradeNotSupportedException();
}
}
27 changes: 13 additions & 14 deletions classes/orcid/OrcidManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,29 @@ public static function isSandbox(?Context $context = null): bool
*
* @throws \Exception
*/
public static function buildOAuthUrl(string $handlerMethod, array $redirectParams): string
public static function buildOAuthUrl(string $handlerMethod, array $redirectParams, ?Context $context = null): string
{
$request = Application::get()->getRequest();
$context = $request->getContext();
$context = $context ?? $request->getContext();
if ($context === null) {
throw new \Exception('OAuth URLs can only be made in a Context, not site wide.');
}

$scope = self::isMemberApiEnabled() ? self::ORCID_API_SCOPE_MEMBER : self::ORCID_API_SCOPE_PUBLIC;
$scope = self::isMemberApiEnabled($context) ? self::ORCID_API_SCOPE_MEMBER : self::ORCID_API_SCOPE_PUBLIC;

// We need to construct a page url, but the request is using the component router.
// Use the Dispatcher to construct the url and set the page router.
$redirectUrl = $request->getDispatcher()->url(
$request,
Application::ROUTE_PAGE,
null,
'orcid',
$handlerMethod,
null,
$redirectParams,
newContext: $context->getPath(),
handler: 'orcid',
op: $handlerMethod,
params: $redirectParams,
urlLocaleForPage: '',
);

return self::getOauthPath() . 'authorize?' . http_build_query(
return self::getOauthPath($context) . 'authorize?' . http_build_query(
[
'client_id' => self::getClientId($context),
'response_type' => 'code',
Expand Down Expand Up @@ -232,7 +231,7 @@ public static function getLogLevel(?Context $context = null): string
$context = Application::get()->getRequest()->getContext();
}

return $context->getData(self::LOG_LEVEL) ?? self::LOG_LEVEL_ERROR;
return $context?->getData(self::LOG_LEVEL) ?? self::LOG_LEVEL_ERROR;
}


Expand All @@ -251,9 +250,9 @@ public static function shouldSendMailToAuthors(?Context $context = null): bool
/**
* Helper method that gets OAuth endpoint for configured ORCID URL (production or sandbox)
*/
public static function getOauthPath(): string
public static function getOauthPath(?Context $context = null): string
{
return self::getOrcidUrl() . 'oauth/';
return self::getOrcidUrl($context) . 'oauth/';
}

/**
Expand Down Expand Up @@ -352,9 +351,9 @@ private static function writeLog(string $message, string $level): void
/**
* Gets the ORCID API endpoint to revoke an access token
*/
public static function getTokenRevocationUrl(): string
public static function getTokenRevocationUrl(?Context $context = null): string
{
return self::getOauthPath() . 'revoke';
return self::getOauthPath($context) . 'revoke';
}

/**
Expand Down
3 changes: 2 additions & 1 deletion classes/orcid/actions/PKPSendSubmissionToOrcid.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use APP\orcid\actions\SendReviewToOrcid;
use APP\publication\Publication;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use PKP\context\Context;
use PKP\jobs\orcid\DepositOrcidSubmission;
use PKP\orcid\OrcidManager;
Expand Down Expand Up @@ -78,7 +79,7 @@ public function execute(): void
return;
}
foreach ($authorsWithOrcid as $orcid => $author) {
dispatch(new DepositOrcidSubmission($author, $this->context, $orcidWork->toArray(), $orcid));
dispatch(new DepositOrcidSubmission($author, $this->context, $orcidWork->toArray(), $orcid, $this->publication->getId()));
}
}

Expand Down
177 changes: 0 additions & 177 deletions classes/orcid/actions/VerifyAuthorWithOrcid.php

This file was deleted.

Loading

0 comments on commit 1d69c41

Please sign in to comment.