Skip to content

Commit

Permalink
added oauth2 ajax module
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkobori committed Apr 12, 2024
1 parent b9fd0d1 commit 407faf7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/libs/ajax/OAuth2.ts
Original file line number Diff line number Diff line change
@@ -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<OAuthConfig> => getConfig(),
};

const getConfig = async (): Promise<OAuthConfig> => {
const configUrl = `${await Config.getApiUrl()}/oauth2/configuration`;
const res: OAuthConfig = (await axios.get(configUrl)).data;
return res;
};
23 changes: 10 additions & 13 deletions src/libs/auth/oidcBroker.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,19 +16,15 @@ 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;

const generateOidcUserManagerSettings = async (
config: OAuthConfig
): Promise<UserManagerSettings> => {
const metadata = {
const metadata: Partial<OidcMetadata> = {
authorization_endpoint: `${await Config.getApiUrl()}/oauth2/authorize`,
token_endpoint: `${await Config.getApiUrl()}/oauth2/token`,
};
Expand All @@ -52,15 +48,16 @@ const generateOidcUserManagerSettings = async (
};

export const OidcBroker = {
initializeAuth: async (): Promise<void> => {
// 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<void> => {
config = await OAuth2.getConfig();
const ums: UserManagerSettings = await generateOidcUserManagerSettings(
config
);
userManagerSettings = ums;
},
getOidcUserManagerSettings: (): UserManagerSettings => userManagerSettings!,
getOidcUser: async (): Promise<OidcUser | null> => {
const userManager: OidcUserManager = new UserManager(OidcBroker.getOidcUserManagerSettings());
return await userManager.getUser();
}
};

0 comments on commit 407faf7

Please sign in to comment.