diff --git a/src/libs/ajax/OAuth2.ts b/src/libs/ajax/OAuth2.ts new file mode 100644 index 000000000..7f1f264bb --- /dev/null +++ b/src/libs/ajax/OAuth2.ts @@ -0,0 +1,17 @@ +import axios from "axios"; +import { Config } from "../config"; + +export interface OAuthConfig { + clientId: string; + authorityEndpoint: string; +} + +export const OAuth2 = { + getConfig: async (): Promise => getConfig(), +}; + +const getConfig = async (): Promise => { + const configUrl = `${await Config.getApiUrl()}/oauth2/configuration`; + const res: OAuthConfig = (await axios.get(configUrl)).data; + return res; +}; diff --git a/src/libs/auth/oidcBroker.ts b/src/libs/auth/oidcBroker.ts index 03a7bf3a3..02696c3df 100644 --- a/src/libs/auth/oidcBroker.ts +++ b/src/libs/auth/oidcBroker.ts @@ -1,6 +1,6 @@ -import { IdTokenClaims, User, UserManagerSettings, WebStorageStateStore } from 'oidc-client-ts'; +import { IdTokenClaims, OidcMetadata, User, UserManager, UserManagerSettings, WebStorageStateStore } from 'oidc-client-ts'; import { Config } from '../config'; -import axios from 'axios'; // TODO: move this to ajax +import { OAuth2, OAuthConfig } from '../ajax/OAuth2'; // Our config for b2C claims are defined here: https://github.com/broadinstitute/terraform-ap-deployments/tree/master/azure/b2c/policies // The standard b2C claims are defined here: https://learn.microsoft.com/en-us/azure/active-directory/develop/id-token-claims-reference @@ -16,11 +16,7 @@ export interface OidcUser extends User { profile: B2cIdTokenClaims; } -interface OAuthConfig { - clientId: string; - authorityEndpoint: string; -} - +type OidcUserManager = UserManager; let config: OAuthConfig | null = null; let userManagerSettings: UserManagerSettings | null = null; @@ -28,7 +24,7 @@ let userManagerSettings: UserManagerSettings | null = null; const generateOidcUserManagerSettings = async ( config: OAuthConfig ): Promise => { - const metadata = { + const metadata: Partial = { authorization_endpoint: `${await Config.getApiUrl()}/oauth2/authorize`, token_endpoint: `${await Config.getApiUrl()}/oauth2/token`, }; @@ -52,15 +48,16 @@ const generateOidcUserManagerSettings = async ( }; export const OidcBroker = { - initializeAuth: async (): Promise => { - // TODO: Move request to an AJAX call - const configUrl = `${await Config.getApiUrl()}/oauth2/configuration`; - const res: OAuthConfig = (await axios.get(configUrl)).data; - config = res; + initialize: async (): Promise => { + config = await OAuth2.getConfig(); const ums: UserManagerSettings = await generateOidcUserManagerSettings( config ); userManagerSettings = ums; }, getOidcUserManagerSettings: (): UserManagerSettings => userManagerSettings!, + getOidcUser: async (): Promise => { + const userManager: OidcUserManager = new UserManager(OidcBroker.getOidcUserManagerSettings()); + return await userManager.getUser(); + } };