From aded3cb2f6bc18931e28bcb0c290519616ffecbc Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Fri, 5 Aug 2022 07:45:04 -0700 Subject: [PATCH 1/5] Fix email syncing issue on login --- Topcoder/class.topcoder.plugin.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Topcoder/class.topcoder.plugin.php b/Topcoder/class.topcoder.plugin.php index 18391ae..112997e 100644 --- a/Topcoder/class.topcoder.plugin.php +++ b/Topcoder/class.topcoder.plugin.php @@ -505,6 +505,10 @@ public function gdn_auth_startAuthenticator_handler() { if ($userID) { $this->syncTopcoderRoles($userID,$topcoderRoles); + if($decodedToken->hasClaim('email')) { + $email = $decodedToken->getClaim('email'); + $this->syncTopcoderEmail($userID, $email); + } Gdn::authenticator()->setIdentity($userID, true); Gdn::session()->start($userID, true); Gdn::authenticator()->trigger(Gdn_Authenticator::AUTH_SUCCESS); @@ -659,6 +663,23 @@ private function syncTopcoderRoles($userID, $roles) { } } + /** + * Sync a email of Topcoder user + * @param $userID + * @param $email Email of Topcoder user + * + */ + private function syncTopcoderEmail($userID, $email) { + $userModel = new UserModel(); + $user = $userModel->getID($userID, DATASET_TYPE_ARRAY); + $currentEmail = val('Email', $user) + + // Update email if there are any changes only + if($currentEmail !== $email) { + $userModel->setField($userID, 'Email', $email); + } + } + /** * Get a role by name and type. * From 918273ea77b8429f38b9f3c4ad9339c09202478c Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Fri, 5 Aug 2022 09:21:50 -0700 Subject: [PATCH 2/5] Revert "Fix email syncing issue on login" This reverts commit aded3cb2f6bc18931e28bcb0c290519616ffecbc. --- Topcoder/class.topcoder.plugin.php | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/Topcoder/class.topcoder.plugin.php b/Topcoder/class.topcoder.plugin.php index 112997e..18391ae 100644 --- a/Topcoder/class.topcoder.plugin.php +++ b/Topcoder/class.topcoder.plugin.php @@ -505,10 +505,6 @@ public function gdn_auth_startAuthenticator_handler() { if ($userID) { $this->syncTopcoderRoles($userID,$topcoderRoles); - if($decodedToken->hasClaim('email')) { - $email = $decodedToken->getClaim('email'); - $this->syncTopcoderEmail($userID, $email); - } Gdn::authenticator()->setIdentity($userID, true); Gdn::session()->start($userID, true); Gdn::authenticator()->trigger(Gdn_Authenticator::AUTH_SUCCESS); @@ -663,23 +659,6 @@ private function syncTopcoderRoles($userID, $roles) { } } - /** - * Sync a email of Topcoder user - * @param $userID - * @param $email Email of Topcoder user - * - */ - private function syncTopcoderEmail($userID, $email) { - $userModel = new UserModel(); - $user = $userModel->getID($userID, DATASET_TYPE_ARRAY); - $currentEmail = val('Email', $user) - - // Update email if there are any changes only - if($currentEmail !== $email) { - $userModel->setField($userID, 'Email', $email); - } - } - /** * Get a role by name and type. * From 95e71f4518aeab3a4dcee00e3ce11112abc5bd3d Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Fri, 5 Aug 2022 09:54:34 -0700 Subject: [PATCH 3/5] Test for syncing email updates --- Topcoder/class.topcoder.plugin.php | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Topcoder/class.topcoder.plugin.php b/Topcoder/class.topcoder.plugin.php index 18391ae..9573732 100644 --- a/Topcoder/class.topcoder.plugin.php +++ b/Topcoder/class.topcoder.plugin.php @@ -505,6 +505,7 @@ public function gdn_auth_startAuthenticator_handler() { if ($userID) { $this->syncTopcoderRoles($userID,$topcoderRoles); + $this->syncTopcoderEmail($userID,$decodedToken->getClaim('email')); Gdn::authenticator()->setIdentity($userID, true); Gdn::session()->start($userID, true); Gdn::authenticator()->trigger(Gdn_Authenticator::AUTH_SUCCESS); @@ -659,6 +660,45 @@ private function syncTopcoderRoles($userID, $roles) { } } + /** + * Sync the e-mail addressof Topcoder for an user + * @param $userID + * @param $roles array a list of role names + * + */ + private function syncTopcoderEmail($userID,$topcoder_email) { + $userModel = new UserModel(); + $user = $userModel->getID($userID); + $vanilla_email = val('Email', $user); + + // Update if two e-mail addresses are different + if($vanilla_email !== $topcoder_email) { + $userData = [ + "UserID" => $userID, + "Email" => $topcoder_email, + "EmailConfirmed" => true + ]; + + $settings = [ + 'NoConfirmEmail' => true + ]; + $ret = $userModel->save($userData, $settings); + if($ret) { + $modified_user = $userModel->getID($userID); + $modified_email = val('Email', $user); + if($modified_email === $topcoder_email) { + self::log('Succeeded to modify e-mail', ["new_email"=>$modified_email]); + } else { + self::log('Failed to modify e-mail', []); + } + } else { + self::log('Failed to modify e-mail', []); + } + } else { + self::log('No need to modify e-mail.', []); + } + } + /** * Get a role by name and type. * From c3791a6186b67af1fa3085fbdf80ccba1121149d Mon Sep 17 00:00:00 2001 From: obog Date: Wed, 7 Dec 2022 12:54:20 +0300 Subject: [PATCH 4/5] Universal nav integration updates --- Topcoder/class.topcoder.plugin.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Topcoder/class.topcoder.plugin.php b/Topcoder/class.topcoder.plugin.php index 9573732..631cc91 100644 --- a/Topcoder/class.topcoder.plugin.php +++ b/Topcoder/class.topcoder.plugin.php @@ -219,6 +219,10 @@ public function settingsController_topcoder_create($sender) { 'Plugins.Topcoder.SSO.CookieName' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Cookie Name'], 'Plugins.Topcoder.SSO.TopcoderHS256.UsernameClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Username Claim for HS256 JWT'], 'Plugins.Topcoder.SSO.TopcoderRS256.UsernameClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Username Claim for RS256 JWT'], + 'Plugins.Topcoder.SSO.TopcoderHS256.UserIDClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder User ID Claim for HS256 JWT'], + 'Plugins.Topcoder.SSO.TopcoderRS256.UserIDClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder User ID Claim for RS256 JWT'], + 'Plugins.Topcoder.SSO.TopcoderHS256.PhotoUrlClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Photo URL Claim for HS256 JWT'], + 'Plugins.Topcoder.SSO.TopcoderRS256.PhotoUrlClaim' => ['Control' => 'TextBox', 'Default' => '', 'Description' => 'Topcoder Photo URL Claim for RS256 JWT'], ]); $cf->renderAll(); @@ -373,9 +377,13 @@ public function gdn_auth_startAuthenticator_handler() { $AUTH0_AUDIENCE = null; $USERNAME_CLAIM = null; + $PHOTOURL_CLAIM = null; + $USERID_CLAIM = null; if ($decodedToken->getHeader('alg') === 'RS256') { $AUTH0_AUDIENCE = c('Plugins.Topcoder.SSO.TopcoderRS256.ID'); $USERNAME_CLAIM = c('Plugins.Topcoder.SSO.TopcoderRS256.UsernameClaim'); + $USERID_CLAIM = c('Plugins.Topcoder.SSO.TopcoderRS256.UserIDClaim'); + $PHOTOURL_CLAIM = c('Plugins.Topcoder.SSO.TopcoderRS256.PhotoUrlClaim'); $jwksUri = $issuer . '.well-known/jwks.json'; $jwksHttpOptions = ['base_uri' => $jwksUri]; $jwksFetcher = new JWKFetcher($this->cacheHandler, $jwksHttpOptions); @@ -383,6 +391,8 @@ public function gdn_auth_startAuthenticator_handler() { } else if ($decodedToken->getHeader('alg') === 'HS256') { $USERNAME_CLAIM = c('Plugins.Topcoder.SSO.TopcoderHS256.UsernameClaim'); + $USERID_CLAIM = c('Plugins.Topcoder.SSO.TopcoderHS256.UserIDClaim'); + $PHOTOURL_CLAIM = c('Plugins.Topcoder.SSO.TopcoderHS256.PhotoUrlClaim'); $AUTH0_AUDIENCE = c('Plugins.Topcoder.SSO.TopcoderHS256.ID'); $CLIENT_H256SECRET = c('Plugins.Topcoder.SSO.TopcoderHS256.Secret'); $signatureVerifier = new SymmetricVerifier($CLIENT_H256SECRET); @@ -450,8 +460,11 @@ public function gdn_auth_startAuthenticator_handler() { $this->checkTopcoderRoles($topcoderRoles); $topcoderUserName = $decodedToken->getClaim($USERNAME_CLAIM); + $topcoderPhotoUrl = $decodedToken->getClaim($PHOTOURL_CLAIM); + $topcoderUserID = $decodedToken->getClaim($USERID_CLAIM); + if ($topcoderUserName) { - self::log('Trying to signIn ...', ['username' => $topcoderUserName]); + self::log('Trying to signIn ...', ['username' => $topcoderUserName, 'topcoderId'=> $topcoderUserID , 'photoUrl' => $topcoderPhotoUrl, ]); $userModel = new UserModel(); $user = $userModel->getByUsername($topcoderUserName, false); @@ -515,6 +528,10 @@ public function gdn_auth_startAuthenticator_handler() { self::log('The session could not be started.', []); throw new ClientException('The session could not be started.', 401); } + + Gdn::userModel()->saveAttribute( + Gdn::session()->UserID, + ['TopcoderUserID' => $topcoderUserID, 'TopcoderPhotoUrl' => $topcoderPhotoUrl]); } else { self::log('Go with the next Vanilla Authenticator', []); } From 8f97713d457b853ee1fe073f3c924c1889add77d Mon Sep 17 00:00:00 2001 From: obog Date: Sun, 11 Dec 2022 10:48:51 +0300 Subject: [PATCH 5/5] Fixed the RegisterUrl in TopcoderPlugin's settings --- Topcoder/modules/TopcoderConfigurationModule.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Topcoder/modules/TopcoderConfigurationModule.php b/Topcoder/modules/TopcoderConfigurationModule.php index e669c44..fca9309 100644 --- a/Topcoder/modules/TopcoderConfigurationModule.php +++ b/Topcoder/modules/TopcoderConfigurationModule.php @@ -133,6 +133,7 @@ public function initialize($schema = null) { $authform = new Gdn_Form(); $authform->setModel($model); $authform->setFormValue('AuthenticationKey','topcoder'); + $authform->setFormValue('RegisterUrl' , $authenticationProviderData['RegisterUrl']); $authform->setFormValue('SignInUrl' , $authenticationProviderData['SignInUrl']); $authform->setFormValue('SignOutUrl' , $authenticationProviderData['SignOutUrl']); $authform->setFormValue('IsDefault' , $authenticationProviderData['IsDefault']);