Skip to content

Universal Navigation #118

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

Merged
merged 7 commits into from
Dec 19, 2022
Merged
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
59 changes: 58 additions & 1 deletion Topcoder/class.topcoder.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -373,16 +377,22 @@ 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);
$signatureVerifier = new AsymmetricVerifier($jwksFetcher);

} 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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -505,6 +518,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);
Expand All @@ -514,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', []);
}
Expand Down Expand Up @@ -659,6 +677,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.
*
Expand Down
1 change: 1 addition & 0 deletions Topcoder/modules/TopcoderConfigurationModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down