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

Add option to always use UPN #2524

Open
wants to merge 1 commit into
base: MOODLE_402_STABLE
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
28 changes: 24 additions & 4 deletions auth/oidc/classes/loginflow/authcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,12 @@ protected function handleauthresponse(array $authparams) {
// If user is already logged in and trying to link Microsoft 365 account or use it for OIDC.
// Check if that Microsoft 365 account already exists in moodle.
if (get_config('auth_oidc', 'idptype') == AUTH_OIDC_IDP_TYPE_MICROSOFT) {
$upn = $idtoken->claim('preferred_username');
if (isset($this->config->useupn) && $this->config->useupn) {
$upn = $idtoken->claim('upn');
}
if (empty($upn)) {
$upn = $idtoken->claim('preferred_username');
}
if (empty($upn)) {
$upn = $idtoken->claim('email');
}
Expand Down Expand Up @@ -540,7 +545,12 @@ protected function handlelogin(string $oidcuniqid, array $authparams, array $tok
// Find the latest real Microsoft username.
// Determine remote username depending on IdP type, or fall back to standard 'sub'.
if (get_config('auth_oidc', 'idptype') == AUTH_OIDC_IDP_TYPE_MICROSOFT) {
$oidcusername = $idtoken->claim('preferred_username');
if (isset($this->config->useupn) && $this->config->useupn) {
$oidcusername = $idtoken->claim('upn');
}
if (empty($oidcusername)) {
$oidcusername = $idtoken->claim('preferred_username');
}
if (empty($oidcusername)) {
$oidcusername = $idtoken->claim('email');
}
Expand Down Expand Up @@ -672,7 +682,12 @@ protected function handlelogin(string $oidcuniqid, array $authparams, array $tok
$existinguser = core_user::get_user($existingmatching->moodleid);

if (get_config('auth_oidc', 'idptype') == AUTH_OIDC_IDP_TYPE_MICROSOFT) {
$username = $idtoken->claim('preferred_username');
if (isset($this->config->useupn) && $this->config->useupn) {
$username = $idtoken->claim('upn');
}
if (empty($username)) {
$username = $idtoken->claim('preferred_username');
}
if (empty($username)) {
$username = $idtoken->claim('email');
}
Expand Down Expand Up @@ -742,7 +757,12 @@ protected function handlelogin(string $oidcuniqid, array $authparams, array $tok
// Generate a Moodle username.
// Use 'upn' if available for username (Azure-specific), or fall back to lower-case oidcuniqid.
if (get_config('auth_oidc', 'idptype') == AUTH_OIDC_IDP_TYPE_MICROSOFT) {
$username = $idtoken->claim('preferred_username');
if (isset($this->config->useupn) && $this->config->useupn) {
$username = $idtoken->claim('upn');
}
if (empty($username)) {
$username = $idtoken->claim('preferred_username');
}
if (empty($username)) {
$username = $idtoken->claim('email');
}
Expand Down
28 changes: 24 additions & 4 deletions auth/oidc/classes/loginflow/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ public function get_userinfo($username) {

if (!isset($userdata['userPrincipalName'])) {
if (get_config('auth_oidc', 'idptype') == AUTH_OIDC_IDP_TYPE_MICROSOFT) {
$upn = $token->claim('preferred_username');
if (isset($this->config->useupn) && $this->config->useupn) {
$upn = $idtoken->claim('upn');
}
if (empty($upn)) {
$upn = $idtoken->claim('preferred_username');
}
if (empty($upn)) {
$upn = $token->claim('email');
}
Expand Down Expand Up @@ -235,7 +240,12 @@ public function get_userinfo($username) {

if (!isset($userdata['userPrincipalName'])) {
if (get_config('auth_oidc', 'idptype') == AUTH_OIDC_IDP_TYPE_MICROSOFT) {
$upn = $token->claim('preferred_username');
if (isset($this->config->useupn) && $this->config->useupn) {
$upn = $idtoken->claim('upn');
}
if (empty($upn)) {
$upn = $idtoken->claim('preferred_username');
}
if (empty($upn)) {
$upn = $token->claim('email');
}
Expand Down Expand Up @@ -566,7 +576,12 @@ protected function checkrestrictions(jwt $idtoken) {
$restrictions = explode("\n", $restrictions);
// Check main user identifier claim based on IdP type, and falls back to oidc-standard "sub" if still empty.
if (get_config('auth_oidc', 'idptype') == AUTH_OIDC_IDP_TYPE_MICROSOFT) {
$tomatch = $idtoken->claim('preferred_username');
if (isset($this->config->useupn) && $this->config->useupn) {
$tomatch = $idtoken->claim('upn');
}
if (empty($tomatch)) {
$tomatch = $idtoken->claim('preferred_username');
}
if (empty($tomatch)) {
$tomatch = $idtoken->claim('email');
}
Expand Down Expand Up @@ -640,7 +655,12 @@ protected function createtoken($oidcuniqid, $username, $authparams, $tokenparams
} else {
// Determine remote username depending on IdP type, or fall back to standard 'sub'.
if (get_config('auth_oidc', 'idptype') == AUTH_OIDC_IDP_TYPE_MICROSOFT) {
$oidcusername = $idtoken->claim('preferred_username');
if (isset($this->config->useupn) && $this->config->useupn) {
$oidcusername = $idtoken->claim('upn');
}
if (empty($oidcusername)) {
$oidcusername = $idtoken->claim('preferred_username');
}
if (empty($oidcusername)) {
$oidcusername = $idtoken->claim('email');
}
Expand Down
2 changes: 2 additions & 0 deletions auth/oidc/lang/en/auth_oidc.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
$string['cfg_err_invalidclientsecret'] = 'Invalid client secret';
$string['cfg_forceredirect_key'] = 'Force redirect';
$string['cfg_forceredirect_desc'] = 'If enabled, will skip the login index page and redirect to the OpenID Connect page. Can be bypassed with ?noredirect=1 URL param';
$string['cfg_useupn_key'] = 'Always use UPN';
$string['cfg_useupn_desc'] = 'If enabled and IdP type is Microsoft, UPN will be used for login flow instead of preferred name.';
$string['cfg_icon_key'] = 'Icon';
$string['cfg_icon_desc'] = 'An icon to display next to the provider name on the login page.';
$string['cfg_iconalt_o365'] = 'Microsoft 365 icon';
Expand Down
4 changes: 4 additions & 0 deletions auth/oidc/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
$settings->add(new admin_setting_configcheckbox('auth_oidc/forceredirect',
get_string('cfg_forceredirect_key', 'auth_oidc'), get_string('cfg_forceredirect_desc', 'auth_oidc'), 0));

// Use UPN.
$settings->add(new admin_setting_configcheckbox('auth_oidc/useupn',
get_string('cfg_useupn_key', 'auth_oidc'), get_string('cfg_useupn_desc', 'auth_oidc'), 0));

// Auto-append.
$settings->add(new admin_setting_configtext('auth_oidc/autoappend',
get_string('cfg_autoappend_key', 'auth_oidc'), get_string('cfg_autoappend_desc', 'auth_oidc'), '', PARAM_TEXT));
Expand Down