Skip to content

Commit 99c0a1f

Browse files
authored
Merge pull request #118 from topcoder-platform/develop
Universal Navigation
2 parents 73d9aa1 + 9f7a5fb commit 99c0a1f

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

Topcoder/class.topcoder.plugin.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ public function settingsController_topcoder_create($sender) {
219219
'Plugins.Topcoder.SSO.CookieName' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Cookie Name'],
220220
'Plugins.Topcoder.SSO.TopcoderHS256.UsernameClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Username Claim for HS256 JWT'],
221221
'Plugins.Topcoder.SSO.TopcoderRS256.UsernameClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Username Claim for RS256 JWT'],
222+
'Plugins.Topcoder.SSO.TopcoderHS256.UserIDClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder User ID Claim for HS256 JWT'],
223+
'Plugins.Topcoder.SSO.TopcoderRS256.UserIDClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder User ID Claim for RS256 JWT'],
224+
'Plugins.Topcoder.SSO.TopcoderHS256.PhotoUrlClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Photo URL Claim for HS256 JWT'],
225+
'Plugins.Topcoder.SSO.TopcoderRS256.PhotoUrlClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Photo URL Claim for RS256 JWT'],
222226
]);
223227

224228
$cf->renderAll();
@@ -373,16 +377,22 @@ public function gdn_auth_startAuthenticator_handler() {
373377

374378
$AUTH0_AUDIENCE = null;
375379
$USERNAME_CLAIM = null;
380+
$PHOTOURL_CLAIM = null;
381+
$USERID_CLAIM = null;
376382
if ($decodedToken->getHeader('alg') === 'RS256') {
377383
$AUTH0_AUDIENCE = c('Plugins.Topcoder.SSO.TopcoderRS256.ID');
378384
$USERNAME_CLAIM = c('Plugins.Topcoder.SSO.TopcoderRS256.UsernameClaim');
385+
$USERID_CLAIM = c('Plugins.Topcoder.SSO.TopcoderRS256.UserIDClaim');
386+
$PHOTOURL_CLAIM = c('Plugins.Topcoder.SSO.TopcoderRS256.PhotoUrlClaim');
379387
$jwksUri = $issuer . '.well-known/jwks.json';
380388
$jwksHttpOptions = ['base_uri' => $jwksUri];
381389
$jwksFetcher = new JWKFetcher($this->cacheHandler, $jwksHttpOptions);
382390
$signatureVerifier = new AsymmetricVerifier($jwksFetcher);
383391

384392
} else if ($decodedToken->getHeader('alg') === 'HS256') {
385393
$USERNAME_CLAIM = c('Plugins.Topcoder.SSO.TopcoderHS256.UsernameClaim');
394+
$USERID_CLAIM = c('Plugins.Topcoder.SSO.TopcoderHS256.UserIDClaim');
395+
$PHOTOURL_CLAIM = c('Plugins.Topcoder.SSO.TopcoderHS256.PhotoUrlClaim');
386396
$AUTH0_AUDIENCE = c('Plugins.Topcoder.SSO.TopcoderHS256.ID');
387397
$CLIENT_H256SECRET = c('Plugins.Topcoder.SSO.TopcoderHS256.Secret');
388398
$signatureVerifier = new SymmetricVerifier($CLIENT_H256SECRET);
@@ -450,8 +460,11 @@ public function gdn_auth_startAuthenticator_handler() {
450460
$this->checkTopcoderRoles($topcoderRoles);
451461

452462
$topcoderUserName = $decodedToken->getClaim($USERNAME_CLAIM);
463+
$topcoderPhotoUrl = $decodedToken->getClaim($PHOTOURL_CLAIM);
464+
$topcoderUserID = $decodedToken->getClaim($USERID_CLAIM);
465+
453466
if ($topcoderUserName) {
454-
self::log('Trying to signIn ...', ['username' => $topcoderUserName]);
467+
self::log('Trying to signIn ...', ['username' => $topcoderUserName, 'topcoderId'=> $topcoderUserID , 'photoUrl' => $topcoderPhotoUrl, ]);
455468

456469
$userModel = new UserModel();
457470
$user = $userModel->getByUsername($topcoderUserName, false);
@@ -505,6 +518,7 @@ public function gdn_auth_startAuthenticator_handler() {
505518

506519
if ($userID) {
507520
$this->syncTopcoderRoles($userID,$topcoderRoles);
521+
$this->syncTopcoderEmail($userID,$decodedToken->getClaim('email'));
508522
Gdn::authenticator()->setIdentity($userID, true);
509523
Gdn::session()->start($userID, true);
510524
Gdn::authenticator()->trigger(Gdn_Authenticator::AUTH_SUCCESS);
@@ -514,6 +528,10 @@ public function gdn_auth_startAuthenticator_handler() {
514528
self::log('The session could not be started.', []);
515529
throw new ClientException('The session could not be started.', 401);
516530
}
531+
532+
Gdn::userModel()->saveAttribute(
533+
Gdn::session()->UserID,
534+
['TopcoderUserID' => $topcoderUserID, 'TopcoderPhotoUrl' => $topcoderPhotoUrl]);
517535
} else {
518536
self::log('Go with the next Vanilla Authenticator', []);
519537
}
@@ -659,6 +677,45 @@ private function syncTopcoderRoles($userID, $roles) {
659677
}
660678
}
661679

680+
/**
681+
* Sync the e-mail addressof Topcoder for an user
682+
* @param $userID
683+
* @param $roles array a list of role names
684+
*
685+
*/
686+
private function syncTopcoderEmail($userID,$topcoder_email) {
687+
$userModel = new UserModel();
688+
$user = $userModel->getID($userID);
689+
$vanilla_email = val('Email', $user);
690+
691+
// Update if two e-mail addresses are different
692+
if($vanilla_email !== $topcoder_email) {
693+
$userData = [
694+
"UserID" => $userID,
695+
"Email" => $topcoder_email,
696+
"EmailConfirmed" => true
697+
];
698+
699+
$settings = [
700+
'NoConfirmEmail' => true
701+
];
702+
$ret = $userModel->save($userData, $settings);
703+
if($ret) {
704+
$modified_user = $userModel->getID($userID);
705+
$modified_email = val('Email', $user);
706+
if($modified_email === $topcoder_email) {
707+
self::log('Succeeded to modify e-mail', ["new_email"=>$modified_email]);
708+
} else {
709+
self::log('Failed to modify e-mail', []);
710+
}
711+
} else {
712+
self::log('Failed to modify e-mail', []);
713+
}
714+
} else {
715+
self::log('No need to modify e-mail.', []);
716+
}
717+
}
718+
662719
/**
663720
* Get a role by name and type.
664721
*

Topcoder/modules/TopcoderConfigurationModule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function initialize($schema = null) {
133133
$authform = new Gdn_Form();
134134
$authform->setModel($model);
135135
$authform->setFormValue('AuthenticationKey','topcoder');
136+
$authform->setFormValue('RegisterUrl' , $authenticationProviderData['RegisterUrl']);
136137
$authform->setFormValue('SignInUrl' , $authenticationProviderData['SignInUrl']);
137138
$authform->setFormValue('SignOutUrl' , $authenticationProviderData['SignOutUrl']);
138139
$authform->setFormValue('IsDefault' , $authenticationProviderData['IsDefault']);

0 commit comments

Comments
 (0)