2023-06-29 09:08:56 +12:00
|
|
|
/*
|
|
|
|
|
Copyright 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.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-11 16:09:18 +12:00
|
|
|
import { completeAuthorizationCodeGrant } from "matrix-js-sdk/src/oidc/authorize";
|
|
|
|
|
import { QueryDict } from "matrix-js-sdk/src/utils";
|
2023-07-10 12:57:16 +12:00
|
|
|
import { OidcClientConfig } from "matrix-js-sdk/src/autodiscovery";
|
|
|
|
|
import { generateOidcAuthorizationUrl } from "matrix-js-sdk/src/oidc/authorize";
|
|
|
|
|
import { randomString } from "matrix-js-sdk/src/randomstring";
|
2023-06-29 09:08:56 +12:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start OIDC authorization code flow
|
|
|
|
|
* Generates auth params, stores them in session storage and
|
|
|
|
|
* Navigates to configured authorization endpoint
|
|
|
|
|
* @param delegatedAuthConfig from discovery
|
|
|
|
|
* @param clientId this client's id as registered with configured issuer
|
2023-07-10 12:57:16 +12:00
|
|
|
* @param homeserverUrl target homeserver
|
|
|
|
|
* @param identityServerUrl OPTIONAL target identity server
|
2023-06-29 09:08:56 +12:00
|
|
|
* @returns Promise that resolves after we have navigated to auth endpoint
|
|
|
|
|
*/
|
|
|
|
|
export const startOidcLogin = async (
|
2023-07-10 12:57:16 +12:00
|
|
|
delegatedAuthConfig: OidcClientConfig,
|
2023-06-29 09:08:56 +12:00
|
|
|
clientId: string,
|
2023-07-10 12:57:16 +12:00
|
|
|
homeserverUrl: string,
|
|
|
|
|
identityServerUrl?: string,
|
2023-06-29 09:08:56 +12:00
|
|
|
): Promise<void> => {
|
|
|
|
|
const redirectUri = window.location.origin;
|
|
|
|
|
|
2023-07-10 12:57:16 +12:00
|
|
|
const nonce = randomString(10);
|
2023-06-29 09:08:56 +12:00
|
|
|
|
2023-07-10 12:57:16 +12:00
|
|
|
const authorizationUrl = await generateOidcAuthorizationUrl({
|
|
|
|
|
metadata: delegatedAuthConfig.metadata,
|
|
|
|
|
redirectUri,
|
2023-06-29 09:08:56 +12:00
|
|
|
clientId,
|
2023-07-10 12:57:16 +12:00
|
|
|
homeserverUrl,
|
|
|
|
|
identityServerUrl,
|
|
|
|
|
nonce,
|
|
|
|
|
});
|
2023-06-29 09:08:56 +12:00
|
|
|
|
|
|
|
|
window.location.href = authorizationUrl;
|
|
|
|
|
};
|
2023-07-11 16:09:18 +12:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets `code` and `state` query params
|
|
|
|
|
*
|
|
|
|
|
* @param queryParams
|
|
|
|
|
* @returns code and state
|
|
|
|
|
* @throws when code and state are not valid strings
|
|
|
|
|
*/
|
|
|
|
|
const getCodeAndStateFromQueryParams = (queryParams: QueryDict): { code: string; state: string } => {
|
|
|
|
|
const code = queryParams["code"];
|
|
|
|
|
const state = queryParams["state"];
|
|
|
|
|
|
|
|
|
|
if (!code || typeof code !== "string" || !state || typeof state !== "string") {
|
|
|
|
|
throw new Error("Invalid query parameters for OIDC native login. `code` and `state` are required.");
|
|
|
|
|
}
|
|
|
|
|
return { code, state };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempt to complete authorization code flow to get an access token
|
|
|
|
|
* @param queryParams the query-parameters extracted from the real query-string of the starting URI.
|
|
|
|
|
* @returns Promise that resolves with accessToken, identityServerUrl, and homeserverUrl when login was successful
|
|
|
|
|
* @throws When we failed to get a valid access token
|
|
|
|
|
*/
|
|
|
|
|
export const completeOidcLogin = async (
|
|
|
|
|
queryParams: QueryDict,
|
|
|
|
|
): Promise<{
|
|
|
|
|
homeserverUrl: string;
|
|
|
|
|
identityServerUrl?: string;
|
|
|
|
|
accessToken: string;
|
2023-07-21 09:30:19 +12:00
|
|
|
clientId: string;
|
|
|
|
|
issuer: string;
|
2023-07-11 16:09:18 +12:00
|
|
|
}> => {
|
|
|
|
|
const { code, state } = getCodeAndStateFromQueryParams(queryParams);
|
2023-07-21 09:30:19 +12:00
|
|
|
const { homeserverUrl, tokenResponse, identityServerUrl, oidcClientSettings } =
|
|
|
|
|
await completeAuthorizationCodeGrant(code, state);
|
2023-07-11 16:09:18 +12:00
|
|
|
|
|
|
|
|
// @TODO(kerrya) do something with the refresh token https://github.com/vector-im/element-web/issues/25444
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
homeserverUrl: homeserverUrl,
|
|
|
|
|
identityServerUrl: identityServerUrl,
|
|
|
|
|
accessToken: tokenResponse.access_token,
|
2023-07-21 09:30:19 +12:00
|
|
|
clientId: oidcClientSettings.clientId,
|
|
|
|
|
issuer: oidcClientSettings.issuer,
|
2023-07-11 16:09:18 +12:00
|
|
|
};
|
|
|
|
|
};
|