Skip to content

Commit

Permalink
MSFTMPP-697: Update code getting OIDC tokens via username to use userid
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmcq committed Oct 28, 2018
1 parent 0f8bcee commit 7fb5c8c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
11 changes: 10 additions & 1 deletion auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,16 @@ public function user_authenticated_hook(&$user, $username, $password) {
global $DB;
if (!empty($user) && !empty($user->auth) && $user->auth === 'oidc') {
$tokenrec = $DB->get_record('auth_oidc_token', ['userid' => $user->id]);
if (empty($tokenrec)) {
if (!empty($tokenrec)) {
// If the token record username is out of sync (ie username changes), update it.
if ($tokenrec->username != $user->username) {
$updatedtokenrec = new \stdClass;
$updatedtokenrec->id = $tokenrec->id;
$updatedtokenrec->username = $user->username;
$DB->update_record('auth_oidc_token', $updatedtokenrec);
$tokenrec = $updatedtokenrec;
}
} else {
// There should always be a token record here, so a failure here means
// the user's token record doesn't yet contain their userid.
$tokenrec = $DB->get_record('auth_oidc_token', ['username' => $username]);
Expand Down
2 changes: 1 addition & 1 deletion classes/loginflow/authcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ protected function handlemigration($oidcuniqid, $authparams, $tokenparams, $idto
}

// Check if Moodle user is already connected to an OIDC user.
$tokenrec = $DB->get_record('auth_oidc_token', ['username' => $USER->username]);
$tokenrec = $DB->get_record('auth_oidc_token', ['userid' => $USER->id]);
if (!empty($tokenrec)) {
if ($tokenrec->oidcuniqid === $oidcuniqid) {
// Already connected to current user.
Expand Down
6 changes: 3 additions & 3 deletions classes/loginflow/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function disconnect($justremovetokens = false, $donotremovetokens = false

if ($justremovetokens === true) {
// Delete token data.
$DB->delete_records('auth_oidc_token', ['username' => $userrec->username]);
$DB->delete_records('auth_oidc_token', ['userid' => $userrec->id]);
$eventdata = ['objectid' => $userrec->id, 'userid' => $userrec->id];
$event = \auth_oidc\event\user_disconnected::create($eventdata);
$event->trigger();
Expand Down Expand Up @@ -210,7 +210,7 @@ public function disconnect($justremovetokens = false, $donotremovetokens = false
// Check to see if the user has a username created by OIDC, or a self-created username.
// OIDC-created usernames are usually very verbose, so we'll allow them to choose a sensible one.
// Otherwise, keep their existing username.
$oidctoken = $DB->get_record('auth_oidc_token', ['username' => $userrec->username]);
$oidctoken = $DB->get_record('auth_oidc_token', ['userid' => $userrec->id]);
$ccun = (isset($oidctoken->oidcuniqid) && strtolower($oidctoken->oidcuniqid) === $userrec->username) ? true : false;
$customdata = [
'canchooseusername' => $ccun,
Expand Down Expand Up @@ -276,7 +276,7 @@ public function disconnect($justremovetokens = false, $donotremovetokens = false

// Delete token data.
if (empty($fromform->donotremovetokens)) {
$DB->delete_records('auth_oidc_token', ['username' => $origusername]);
$DB->delete_records('auth_oidc_token', ['userid' => $userrec->id]);

$eventdata = ['objectid' => $userrec->id, 'userid' => $userrec->id];
$event = \auth_oidc\event\user_disconnected::create($eventdata);
Expand Down
3 changes: 2 additions & 1 deletion classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static function get_metadata(collection $collection): collection {
'auth_oidc_token' => [
'oidcuniqid',
'username',
'userid',
'oidcusername',
'scope',
'resource',
Expand Down Expand Up @@ -137,7 +138,7 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
protected static function get_table_user_map(\stdClass $user): array {
$tables = [
'auth_oidc_prevlogin' => ['userid' => $user->id],
'auth_oidc_token' => ['username' => $user->username],
'auth_oidc_token' => ['userid' => $user->id],
];
return $tables;
}
Expand Down
1 change: 1 addition & 0 deletions lang/en/auth_oidc.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
$string['privacy:metadata:auth_oidc_token'] = 'Information about OpenID Connect tokens for users';
$string['privacy:metadata:auth_oidc_token:oidcuniqid'] = 'The OIDC unique user identifier.';
$string['privacy:metadata:auth_oidc_token:username'] = 'The username of the Moodle user';
$string['privacy:metadata:auth_oidc_token:userid'] = 'The user ID of the Moodle user';
$string['privacy:metadata:auth_oidc_token:oidcusername'] = 'The username of the OIDC user';
$string['privacy:metadata:auth_oidc_token:scope'] = 'The scope of the token';
$string['privacy:metadata:auth_oidc_token:resource'] = 'The resource of the token';
Expand Down
4 changes: 2 additions & 2 deletions ucp.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

$action = optional_param('action', null, PARAM_TEXT);

$oidctoken = $DB->get_record('auth_oidc_token', ['username' => $USER->username]);
$oidctoken = $DB->get_record('auth_oidc_token', ['userid' => $USER->id]);
$oidcconnected = (!empty($oidctoken)) ? true : false;

$oidcloginconnected = ($USER->auth === 'oidc') ? true : false;
Expand Down Expand Up @@ -104,4 +104,4 @@
echo \html_writer::end_div();

echo $OUTPUT->footer();
}
}

0 comments on commit 7fb5c8c

Please sign in to comment.