OIDC: Log in (#11199)
* add delegatedauthentication to validated server config * dynamic client registration functions * test OP registration functions * add stubbed nativeOidc flow setup in Login * cover more error cases in Login * tidy * test dynamic client registration in Login * comment oidc_static_clients * register oidc inside Login.getFlows * strict fixes * remove unused code * and imports * comments * comments 2 * util functions to get static client id * check static client ids in login flow * remove dead code * OidcRegistrationClientMetadata type * navigate to oidc authorize url * exchange code for token * navigate to oidc authorize url * navigate to oidc authorize url * test * adjust for js-sdk code * login with oidc native flow: messy version * tidy * update test for response_mode query * tidy up some TODOs * use new types * add identityServerUrl to stored params * unit test completeOidcLogin * test tokenlogin * strict * whitespace * tidy * unit test oidc login flow in MatrixChat * strict * tidy * extract success/failure handlers from token login function * typo * use for no homeserver error dialog too * reuse post-token login functions, test * shuffle testing utils around * shuffle testing utils around * i18n * tidy * Update src/Lifecycle.ts Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * tidy * comment * update tests for id token validation * move try again responsibility * prettier * use more future proof config for static clients * test util for oidcclientconfigs * rename type and lint * correct oidc test util * store issuer and clientId pre auth navigation * adjust for js-sdk changes * update for js-sdk userstate, tidy * update MatrixChat tests * update tests --------- Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
This commit is contained in:
parent
186497a67d
commit
7b3d0ad209
7 changed files with 490 additions and 67 deletions
|
@ -2,7 +2,7 @@
|
|||
Copyright 2015, 2016 OpenMarket Ltd
|
||||
Copyright 2017 Vector Creations Ltd
|
||||
Copyright 2018 New Vector Ltd
|
||||
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
||||
Copyright 2019, 2020, 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
@ -65,6 +65,7 @@ import AbstractLocalStorageSettingsHandler from "./settings/handlers/AbstractLoc
|
|||
import { OverwriteLoginPayload } from "./dispatcher/payloads/OverwriteLoginPayload";
|
||||
import { SdkContextClass } from "./contexts/SDKContext";
|
||||
import { messageForLoginError } from "./utils/ErrorUtils";
|
||||
import { completeOidcLogin } from "./utils/oidc/authorize";
|
||||
|
||||
const HOMESERVER_URL_KEY = "mx_hs_url";
|
||||
const ID_SERVER_URL_KEY = "mx_is_url";
|
||||
|
@ -182,6 +183,9 @@ export async function getStoredSessionOwner(): Promise<[string, boolean] | [null
|
|||
}
|
||||
|
||||
/**
|
||||
* If query string includes OIDC authorization code flow parameters attempt to login using oidc flow
|
||||
* Else, we may be returning from SSO - attempt token login
|
||||
*
|
||||
* @param {Object} queryParams string->string map of the
|
||||
* query-parameters extracted from the real query-string of the starting
|
||||
* URI.
|
||||
|
@ -189,6 +193,92 @@ export async function getStoredSessionOwner(): Promise<[string, boolean] | [null
|
|||
* @param {string} defaultDeviceDisplayName
|
||||
* @param {string} fragmentAfterLogin path to go to after a successful login, only used for "Try again"
|
||||
*
|
||||
* @returns {Promise} promise which resolves to true if we completed the delegated auth login
|
||||
* else false
|
||||
*/
|
||||
export async function attemptDelegatedAuthLogin(
|
||||
queryParams: QueryDict,
|
||||
defaultDeviceDisplayName?: string,
|
||||
fragmentAfterLogin?: string,
|
||||
): Promise<boolean> {
|
||||
if (queryParams.code && queryParams.state) {
|
||||
return attemptOidcNativeLogin(queryParams);
|
||||
}
|
||||
|
||||
return attemptTokenLogin(queryParams, defaultDeviceDisplayName, fragmentAfterLogin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to login by completing OIDC authorization code flow
|
||||
* @param queryParams string->string map of the query-parameters extracted from the real query-string of the starting URI.
|
||||
* @returns Promise that resolves to true when login succceeded, else false
|
||||
*/
|
||||
async function attemptOidcNativeLogin(queryParams: QueryDict): Promise<boolean> {
|
||||
try {
|
||||
const { accessToken, homeserverUrl, identityServerUrl } = await completeOidcLogin(queryParams);
|
||||
|
||||
const {
|
||||
user_id: userId,
|
||||
device_id: deviceId,
|
||||
is_guest: isGuest,
|
||||
} = await getUserIdFromAccessToken(accessToken, homeserverUrl, identityServerUrl);
|
||||
|
||||
const credentials = {
|
||||
accessToken,
|
||||
homeserverUrl,
|
||||
identityServerUrl,
|
||||
deviceId,
|
||||
userId,
|
||||
isGuest,
|
||||
};
|
||||
|
||||
logger.debug("Logged in via OIDC native flow");
|
||||
await onSuccessfulDelegatedAuthLogin(credentials);
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.error("Failed to login via OIDC", error);
|
||||
|
||||
// TODO(kerrya) nice error messages https://github.com/vector-im/element-web/issues/25665
|
||||
await onFailedDelegatedAuthLogin(_t("Something went wrong."));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets information about the owner of a given access token.
|
||||
* @param accessToken
|
||||
* @param homeserverUrl
|
||||
* @param identityServerUrl
|
||||
* @returns Promise that resolves with whoami response
|
||||
* @throws when whoami request fails
|
||||
*/
|
||||
async function getUserIdFromAccessToken(
|
||||
accessToken: string,
|
||||
homeserverUrl: string,
|
||||
identityServerUrl?: string,
|
||||
): Promise<ReturnType<MatrixClient["whoami"]>> {
|
||||
try {
|
||||
const client = createClient({
|
||||
baseUrl: homeserverUrl,
|
||||
accessToken: accessToken,
|
||||
idBaseUrl: identityServerUrl,
|
||||
});
|
||||
|
||||
return await client.whoami();
|
||||
} catch (error) {
|
||||
logger.error("Failed to retrieve userId using accessToken", error);
|
||||
throw new Error("Failed to retrieve userId using accessToken");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {QueryDict} queryParams string->string map of the
|
||||
* query-parameters extracted from the real query-string of the starting
|
||||
* URI.
|
||||
*
|
||||
* @param {string} defaultDeviceDisplayName
|
||||
* @param {string} fragmentAfterLogin path to go to after a successful login, only used for "Try again"
|
||||
*
|
||||
* @returns {Promise} promise which resolves to true if we completed the token
|
||||
* login, else false
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue