Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orcid two factor authentication #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion forms/OpenIDPluginSettingsForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function initData()
'generateAPIKey' => $settings['generateAPIKey'] ? $settings['generateAPIKey'] : 0,
'providerSync' => key_exists('providerSync', $settings) ? $settings['providerSync'] : false,
'disableFields' => $settings['disableFields'],
'orcid2fa' => key_exists('orcid2fa', $settings) ? $settings['orcid2fa'] : false,
);
} else {
$this->_data = array(
Expand All @@ -105,7 +106,7 @@ function initData()
function readInputData()
{
$this->readUserVars(
array('provider', 'legacyLogin', 'legacyRegister', 'disableConnect', 'hashSecret', 'generateAPIKey', 'providerSync', 'disableFields')
array('provider', 'legacyLogin', 'legacyRegister', 'disableConnect', 'hashSecret', 'generateAPIKey', 'providerSync', 'disableFields', 'orcid2fa')
);
parent::readInputData();
}
Expand Down Expand Up @@ -157,6 +158,7 @@ function execute(...$functionArgs)
'generateAPIKey' => $this->getData('generateAPIKey'),
'providerSync' => $this->getData('providerSync'),
'disableFields' => $this->getData('disableFields'),
'orcid2fa' => $this->getData('orcid2fa'),
);
$this->plugin->updateSetting($contextId, 'openIDSettings', json_encode($settings), 'string');
import('classes.notification.NotificationManager');
Expand Down
8 changes: 7 additions & 1 deletion handler/OpenIDHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ function doAuthentication($args, $request, $provider = null)

if (isset($token) && isset($publicKey)) {
$tokenPayload = $this->_validateAndExtractToken($token, $publicKey);
//check if orcid two-factor authentication is required
if ($settings['orcid2fa'] == true && $tokenPayload['amr'] != 'mfa') {
$ssoErrors['sso_error'] = '2fa';
return $request->redirect($context, 'login', null, null, $ssoErrors);
}
if (isset($tokenPayload) && is_array($tokenPayload)) {
$tokenPayload['selectedProvider'] = $selectedProvider;
$user = $this->_getUserViaKeycloakId($tokenPayload);
Expand Down Expand Up @@ -391,7 +396,6 @@ private function _validateAndExtractToken(array $token, array $publicKeys)
try {
if (!empty($t)) {
$jwtPayload = JWT::decode($t, $publicKey, array('RS256'));

if (isset($jwtPayload)) {
$credentials = [
'id' => property_exists($jwtPayload, 'sub') ? $jwtPayload->sub : null,
Expand All @@ -400,6 +404,8 @@ private function _validateAndExtractToken(array $token, array $publicKeys)
'given_name' => property_exists($jwtPayload, 'given_name') ? $jwtPayload->given_name : null,
'family_name' => property_exists($jwtPayload, 'family_name') ? $jwtPayload->family_name : null,
'email_verified' => property_exists($jwtPayload, 'email_verified') ? $jwtPayload->email_verified : null,
//orcid property for checking two factor authentication status
'amr' => property_exists($jwtPayload, 'amr') ? $jwtPayload->amr : null,
];
}
if (isset($credentials) && key_exists('id', $credentials) && !empty($credentials['id'])) {
Expand Down
8 changes: 6 additions & 2 deletions handler/OpenIDLoginHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function index($args, $request)
$legacyLogin = false;
$templateMgr = TemplateManager::getManager($request);
$context = $request->getContext();

if (!Validation::isLoggedIn()) {
$router = $request->getRouter();
$contextId = ($context == null) ? 0 : $context->getId();
Expand All @@ -67,7 +66,9 @@ function index($args, $request)
foreach ($providerList as $name => $settings) {
if (key_exists('authUrl', $settings) && !empty($settings['authUrl'])
&& key_exists('clientId', $settings) && !empty($settings['clientId'])) {
if (sizeof($providerList) == 1 && !$legacyLogin && !$legacyRegister) {
//if there is a single provider, make sure there are no login errors before redirecting
$ssoErrorCheck = $request->getUserVar('sso_error');
if (sizeof($providerList) == 1 && !$legacyLogin && !$legacyRegister && !isset($ssoErrorCheck) && empty($ssoErrorCheck)) {
$request->redirectUrl(
$settings['authUrl'].
'?client_id='.$settings['clientId'].
Expand Down Expand Up @@ -222,6 +223,9 @@ private function _setSSOErrorMessages($ssoError, $templateMgr, $request)
case 'cert':
$templateMgr->assign('errorMsg', 'plugins.generic.openid.error.openid.cert.desc');
break;
case '2fa':
$templateMgr->assign('errorMsg', 'plugins.generic.openid.error.openid.orcid2fa.desc');
break;
case 'disabled':
$reason = $request->getUserVar('sso_error_msg');
$templateMgr->assign('accountDisabled', true);
Expand Down
6 changes: 6 additions & 0 deletions locale/en_US/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ msgstr "Please enter the label of the login button."
msgid "plugins.generic.openid.settings.orcid.enable"
msgstr "ORCID OpenID Connect"

msgid "plugins.generic.openid.settings.orcid.orcid2fa.enable"
msgstr "Require Two-Factor Authentication for OrcId login"

msgid "plugins.generic.openid.settings.orcid.desc"
msgstr "Please use this redirect URL (see <a href='https://github.com/ORCID/ORCID-Source/blob/master/orcid-web/ORCID_AUTH_WITH_OPENID_CONNECT.md' target='_blank' rel='noopener'>tutorial</a>):"

Expand Down Expand Up @@ -237,6 +240,9 @@ msgstr "<strong>An error occurred while receiving your data from the OpenId prov
msgid "plugins.generic.openid.error.openid.cert.desc"
msgstr "<strong>An error occurred while validation and extracting your data.</strong><br />The service may not be available right now.<br />Please try again later and <a href='mailto:{$supportEmail}'>contact technical support</a> if the problem still exists."

msgid "plugins.generic.openid.error.openid.orcid2fa.desc"
msgstr "Orcid two-factor authentication is required for login. Please go to <a href=\"https://orcid.org\">orcid.org</a>, log into your Orcid profile and turn on two-factor authentication in the security section of your account settings."

msgid "plugins.generic.openid.error.openid.disabled.without"
msgstr "<strong>This account is disabled without any specific reason.</strong><br />Please <a href='mailto:{$supportEmail}'>contact technical support</a> to enable this account."

Expand Down
4 changes: 4 additions & 0 deletions templates/settings.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
{else}
{fbvElement type="hidden" id="provider[{$name}][configUrl]" value=$settings['configUrl'] }
{/if}
{if $name eq 'orcid'}
{fbvElement type="checkbox" name="orcid2fa" id="orcid2fa" checked=$orcid2fa value="1" label="plugins.generic.openid.settings.orcid.orcid2fa.enable"}
<div style="clear: both;">&nbsp;</div>
{/if}
<div>
<div><strong>{translate key="plugins.generic.openid.settings.provider.settings"}</strong></div>
{fbvElement type="text" id="provider[{$name}][clientId]" value=$provider[{$name}]['clientId'] maxlength="250" label="plugins.generic.openid.settings.clientId.desc" inline=true size=$fbvStyles.size.MEDIUM}
Expand Down