fix(oidc): use fallback to get prefered username (#517)

function `getPreferredUsername()` is failed to fetch prefered username when using Google as OpenID
provider because the payload inside the JWT does not contains exact "preferred_username" key

Signed-off-by: widnyana <wid@widnyana.web.id>
This commit is contained in:
wid 2023-09-28 22:36:19 +07:00 committed by GitHub
parent 18fe02f597
commit 5db3ae6b16
1 changed files with 25 additions and 1 deletions

View File

@ -100,12 +100,36 @@ const onlyGuestTaskCompletion = () => {
return false; return false;
}; };
/**
* Return prefered username
* Somehow if using Google as the OpenID provider, the field `preferred_username` is not returned
* therefore a special handling is added to cover the issue.
* Please refer to following link, section 5.1 Standard Claims to find the details:
* https://openid.net/specs/openid-connect-core-1_0.html
* @returns string
*/
const getPreferredUsername = () => { const getPreferredUsername = () => {
const idToken = getIdToken(); const idToken = getIdToken();
if (idToken) { if (idToken) {
const idObject = jwt(idToken); const idObject = jwt(idToken);
if (idToken === undefined || idToken === 'undefined') {
return null;
}
if ((idObject as any).preferred_username !== undefined) {
return (idObject as any).preferred_username; return (idObject as any).preferred_username;
} }
if ((idObject as any).name !== undefined) {
// note: handling response if OpenID is using Google SSO as the provider
return (idObject as any).name;
}
// fallback to `given_name` as the default value.
return (idObject as any).given_name;
}
return null; return null;
}; };