Skip to content

Commit 92c9028

Browse files
authored
Merge pull request Expensify#87368 from s77rt/handle-saml-required-reauth
[Payment due @huult] [Internal QA] Trigger SAML sign-in when trying to re-auth a SAML required account
2 parents fcf869e + 348f22c commit 92c9028

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/libs/Reauthentication.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ function reauthenticate(command = ''): Promise<boolean> {
130130
const partnerName = shouldUseNewPartnerName ? CONFIG.EXPENSIFY.PARTNER_NAME : CONFIG.EXPENSIFY.LEGACY_PARTNER_NAME;
131131
const partnerPassword = shouldUseNewPartnerName ? CONFIG.EXPENSIFY.PARTNER_PASSWORD : CONFIG.EXPENSIFY.LEGACY_PARTNER_PASSWORD;
132132

133+
if (account?.isSAMLRequired) {
134+
Log.info(`[Reauthenticate] Redirecting to Sign In because SAML is required`);
135+
setIsAuthenticating(false);
136+
redirectToSignIn(undefined, true);
137+
return false;
138+
}
139+
133140
// Prevent reauthentication if credentials are missing (e.g. after sign out)
134141
if (!credentials?.autoGeneratedLogin || !credentials?.autoGeneratedPassword) {
135142
Log.info('[Reauthenticate] No credentials available, redirecting to sign in');

src/libs/actions/SignInRedirect.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {clearAllPolicies} from './Policy/Policy';
1212
let currentShouldForceOffline: boolean | undefined;
1313
let currentIsUsingImportedState: boolean | undefined;
1414
let currentSessionAuthToken: string | undefined;
15+
let currentSessionEmail: string | undefined;
1516
let currentCredentialsValidateCode: string | undefined;
1617

1718
Onyx.connectWithoutView({
@@ -32,6 +33,7 @@ Onyx.connectWithoutView({
3233
key: ONYXKEYS.SESSION,
3334
callback: (session) => {
3435
currentSessionAuthToken = session?.authToken;
36+
currentSessionEmail = session?.email;
3537
},
3638
});
3739

@@ -42,7 +44,7 @@ Onyx.connectWithoutView({
4244
},
4345
});
4446

45-
function clearStorageAndRedirect(errorMessage?: string): Promise<void> {
47+
function clearStorageAndRedirect(errorMessage?: string, isSAMLReauthentication?: boolean): Promise<void> {
4648
// Under certain conditions, there are key-values we'd like to keep in storage even when a user is logged out.
4749
// We pass these into the clear() method in order to avoid having to reset them on a delayed tick and getting
4850
// flashes of unwanted default state.
@@ -79,6 +81,14 @@ function clearStorageAndRedirect(errorMessage?: string): Promise<void> {
7981
keysToPreserve.push(ONYXKEYS.ACCOUNT);
8082
}
8183

84+
// Mark the account as loading and set the login in credentials to trigger the `SAML_SIGN_IN` transition
85+
if (isSAMLReauthentication) {
86+
keysToPreserve.push(ONYXKEYS.CREDENTIALS);
87+
keysToPreserve.push(ONYXKEYS.ACCOUNT);
88+
Onyx.merge(ONYXKEYS.CREDENTIALS, {login: currentSessionEmail, autoGeneratedLogin: null, autoGeneratedPassword: null});
89+
Onyx.merge(ONYXKEYS.ACCOUNT, {isLoading: true});
90+
}
91+
8292
return Onyx.clear(keysToPreserve).then(() => {
8393
if (CONFIG.IS_HYBRID_APP) {
8494
resetSignInFlow();
@@ -108,10 +118,11 @@ function clearStorageAndRedirect(errorMessage?: string): Promise<void> {
108118
*
109119
* Normally this method would live in Session.js, but that would cause a circular dependency with Network.js.
110120
*
111-
* @param [errorMessage] error message to be displayed on the sign in page
121+
* @param errorMessage Error message to be displayed on the sign in page
122+
* @param isSAMLReauthentication Whether the redirection was triggered by reauthentication for SAML required account
112123
*/
113-
function redirectToSignIn(errorMessage?: string): Promise<void> {
114-
return clearStorageAndRedirect(errorMessage).then(() => {
124+
function redirectToSignIn(errorMessage?: string, isSAMLReauthentication?: boolean): Promise<void> {
125+
return clearStorageAndRedirect(errorMessage, isSAMLReauthentication).then(() => {
115126
clearSessionStorage();
116127
});
117128
}

0 commit comments

Comments
 (0)