Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use strong types in authentication module #3164

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions src/modules/authentication/AuthenticationWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
AccountInfo,
AuthenticationResult,
BrowserAuthError,
InteractionRequiredAuthError,
PopupRequest,
SilentRequest
Expand Down Expand Up @@ -45,8 +46,8 @@ export class AuthenticationWrapper implements IAuthenticationWrapper {
public getSessionId(): string | null {
const account = this.getAccount();
if (account) {
const idTokenClaims: any = account?.idTokenClaims;
return idTokenClaims?.sid;
const idTokenClaims = account?.idTokenClaims;
return idTokenClaims?.sid ?? null;
}
return null;
}
Expand Down Expand Up @@ -81,10 +82,12 @@ export class AuthenticationWrapper implements IAuthenticationWrapper {
const result = await msalApplication.loginPopup(popUpRequest);
this.storeHomeAccountId(result.account!);
return result;
} catch (error: any) {
const { errorCode } = error;
if (errorCode === 'interaction_in_progress') {
this.eraseInteractionInProgressCookie();
} catch (error: unknown) {
if (error instanceof BrowserAuthError){
const { errorCode } = error;
if (errorCode === 'interaction_in_progress') {
this.eraseInteractionInProgressCookie();
}
}
throw error;
}
Expand Down Expand Up @@ -166,17 +169,14 @@ export class AuthenticationWrapper implements IAuthenticationWrapper {
const result = await msalApplication.acquireTokenSilent(silentRequest);
this.storeHomeAccountId(result.account!);
return result;
} catch (error: any) {
} catch (error: unknown) {
if (error instanceof InteractionRequiredAuthError || !this.getAccount()) {
return this.loginWithInteraction(scopes.length > 0 ? scopes : defaultScopes, sessionId);

} else if (signInAuthError(error)) {
this.deleteHomeAccountId();
throw error;
}
else {
throw error;
if (typeof error === 'string' && signInAuthError(error as string)) {
this.deleteHomeAccountId();
}
throw error;
}
}

Expand Down Expand Up @@ -225,22 +225,20 @@ export class AuthenticationWrapper implements IAuthenticationWrapper {
const result = await msalApplication.loginPopup(popUpRequest);
this.storeHomeAccountId(result.account!);
return result;
} catch (error: any) {
const { errorCode } = error;
if (signInAuthError(errorCode) && !this.consentingToNewScopes && (errorCode && errorCode !== 'user_cancelled')) {
this.clearSession();
if (errorCode === 'interaction_in_progress') {
this.eraseInteractionInProgressCookie();
} catch (error: unknown) {
if(error instanceof BrowserAuthError){
const { errorCode } = error;
const valid = !this.consentingToNewScopes && errorCode !== 'user_cancelled';
if (signInAuthError(errorCode) && valid) {
this.clearSession();
if (errorCode === 'interaction_in_progress') {
this.eraseInteractionInProgressCookie();
}
}
throw error;
}
else {
throw error;
}

throw error;
}
}

private storeHomeAccountId(account: AccountInfo): void {
localStorage.setItem(HOME_ACCOUNT_KEY, account.homeAccountId);
}
Expand Down
5 changes: 3 additions & 2 deletions src/modules/authentication/msal-app.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable no-console */
import { Configuration, LogLevel, PublicClientApplication } from '@azure/msal-browser';
import { eventTypes, telemetry } from '../../telemetry';

function getClientIdFromWindow() {
return (window as any).ClientId;
return window?.ClientId ?? '';
}

function getClientIdFromEnv() {
return process.env.REACT_APP_CLIENT_ID;
return process.env?.REACT_APP_CLIENT_ID ?? '';
}

const windowHasClientId = getClientIdFromWindow();
Expand Down
8 changes: 8 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export {};
declare global {
interface Window {
ClientId: string | undefined
}
}

window.ClientId = window.ClientId || undefined;
Loading