This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OIDC: Persist details in session storage, create store (#11302)
* utils to persist clientId and issuer after oidc authentication * add dep oidc-client-ts * persist issuer and clientId after successful oidc auth * add OidcClientStore * comments and tidy * format
- Loading branch information
Kerry
authored
Jul 20, 2023
1 parent
882c85a
commit 0b0d77c
Showing
11 changed files
with
446 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
import { IDelegatedAuthConfig, MatrixClient, M_AUTHENTICATION } from "matrix-js-sdk/src/client"; | ||
import { discoverAndValidateAuthenticationConfig } from "matrix-js-sdk/src/oidc/discovery"; | ||
import { logger } from "matrix-js-sdk/src/logger"; | ||
import { OidcClient } from "oidc-client-ts"; | ||
|
||
import { getStoredOidcTokenIssuer, getStoredOidcClientId } from "../../utils/oidc/persistOidcSettings"; | ||
|
||
/** | ||
* @experimental | ||
* Stores information about configured OIDC provider | ||
*/ | ||
export class OidcClientStore { | ||
private oidcClient?: OidcClient; | ||
private initialisingOidcClientPromise: Promise<void> | undefined; | ||
private authenticatedIssuer?: string; | ||
private _accountManagementEndpoint?: string; | ||
|
||
public constructor(private readonly matrixClient: MatrixClient) { | ||
this.authenticatedIssuer = getStoredOidcTokenIssuer(); | ||
// don't bother initialising store when we didnt authenticate via oidc | ||
if (this.authenticatedIssuer) { | ||
this.getOidcClient(); | ||
} | ||
} | ||
|
||
/** | ||
* True when the active user is authenticated via OIDC | ||
*/ | ||
public get isUserAuthenticatedWithOidc(): boolean { | ||
return !!this.authenticatedIssuer; | ||
} | ||
|
||
public get accountManagementEndpoint(): string | undefined { | ||
return this._accountManagementEndpoint; | ||
} | ||
|
||
private async getOidcClient(): Promise<OidcClient | undefined> { | ||
if (!this.oidcClient && !this.initialisingOidcClientPromise) { | ||
this.initialisingOidcClientPromise = this.initOidcClient(); | ||
} | ||
await this.initialisingOidcClientPromise; | ||
this.initialisingOidcClientPromise = undefined; | ||
return this.oidcClient; | ||
} | ||
|
||
private async initOidcClient(): Promise<void> { | ||
const wellKnown = this.matrixClient.getClientWellKnown(); | ||
if (!wellKnown) { | ||
logger.error("Cannot initialise OidcClientStore: client well known required."); | ||
return; | ||
} | ||
|
||
const delegatedAuthConfig = M_AUTHENTICATION.findIn<IDelegatedAuthConfig>(wellKnown) ?? undefined; | ||
try { | ||
const clientId = getStoredOidcClientId(); | ||
const { account, metadata, signingKeys } = await discoverAndValidateAuthenticationConfig( | ||
delegatedAuthConfig, | ||
); | ||
// if no account endpoint is configured default to the issuer | ||
this._accountManagementEndpoint = account ?? metadata.issuer; | ||
this.oidcClient = new OidcClient({ | ||
...metadata, | ||
authority: metadata.issuer, | ||
signingKeys, | ||
redirect_uri: window.location.origin, | ||
client_id: clientId, | ||
}); | ||
} catch (error) { | ||
logger.error("Failed to initialise OidcClientStore", error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
const clientIdStorageKey = "mx_oidc_client_id"; | ||
const tokenIssuerStorageKey = "mx_oidc_token_issuer"; | ||
|
||
/** | ||
* Persists oidc clientId and issuer in session storage | ||
* Only set after successful authentication | ||
* @param clientId | ||
* @param issuer | ||
*/ | ||
export const persistOidcAuthenticatedSettings = (clientId: string, issuer: string): void => { | ||
sessionStorage.setItem(clientIdStorageKey, clientId); | ||
sessionStorage.setItem(tokenIssuerStorageKey, issuer); | ||
}; | ||
|
||
/** | ||
* Retrieve stored oidc issuer from session storage | ||
* When user has token from OIDC issuer, this will be set | ||
* @returns issuer or undefined | ||
*/ | ||
export const getStoredOidcTokenIssuer = (): string | undefined => { | ||
return sessionStorage.getItem(tokenIssuerStorageKey) ?? undefined; | ||
}; | ||
|
||
/** | ||
* Retrieves stored oidc client id from session storage | ||
* @returns clientId | ||
* @throws when clientId is not found in session storage | ||
*/ | ||
export const getStoredOidcClientId = (): string => { | ||
const clientId = sessionStorage.getItem(clientIdStorageKey); | ||
if (!clientId) { | ||
throw new Error("Oidc client id not found in storage"); | ||
} | ||
return clientId; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.