-
Notifications
You must be signed in to change notification settings - Fork 4k
Fix Okta SAML double login on native session resume #94396
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
| import {act, render} from '@testing-library/react-native'; | ||
| import {openAuthSessionAsync} from 'expo-web-browser'; | ||
| import React from 'react'; | ||
| import Onyx from 'react-native-onyx'; | ||
| import type {OnyxEntry} from 'react-native-onyx'; | ||
| import OnyxListItemProvider from '@components/OnyxListItemProvider'; | ||
| import * as Session from '@libs/actions/Session'; | ||
| import SAMLSignInPage from '@pages/signin/SAMLSignInPage'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; | ||
|
|
||
| jest.mock('expo-web-browser', () => ({ | ||
| openAuthSessionAsync: jest.fn(() => Promise.resolve({type: 'cancel'})), | ||
| })); | ||
|
|
||
| jest.mock('@libs/LoginUtils', () => ({ | ||
| postSAMLLogin: jest.fn(() => Promise.resolve({url: 'https://idp.example.com/saml'})), | ||
| handleSAMLLoginError: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@libs/Navigation/Navigation', () => ({ | ||
| isNavigationReady: jest.fn(() => Promise.resolve()), | ||
| goBack: jest.fn(), | ||
| navigate: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@hooks/useLocalize', () => | ||
| jest.fn(() => ({ | ||
| translate: (key: string) => key, | ||
| })), | ||
| ); | ||
|
|
||
| // Stub the presentational wrappers — SAMLSignInPage's effects (which is what we're testing) run on mount | ||
| // regardless of what these render, and stubbing them avoids pulling in the navigation-heavy render tree. | ||
| jest.mock('@components/ScreenWrapper', () => ({__esModule: true, default: () => null})); | ||
| jest.mock('@components/BlockingViews/FullPageOfflineBlockingView', () => ({__esModule: true, default: () => null})); | ||
| jest.mock('@components/HeaderWithBackButton', () => ({__esModule: true, default: () => null})); | ||
| jest.mock('@components/SAMLLoadingIndicator', () => ({__esModule: true, default: () => null})); | ||
|
|
||
| const mockedOpenAuthSessionAsync = jest.mocked(openAuthSessionAsync); | ||
|
|
||
| async function setCredentials() { | ||
| await act(async () => { | ||
| await Onyx.set(ONYXKEYS.CREDENTIALS, {login: 'test@example.com'}); | ||
| await Onyx.set(ONYXKEYS.ACCOUNT, {isLoading: false}); | ||
| await Onyx.set(ONYXKEYS.RAM_ONLY_IS_AUTHENTICATING_WITH_SHORT_LIVED_TOKEN, false); | ||
| }); | ||
| await waitForBatchedUpdatesWithAct(); | ||
| } | ||
|
|
||
| describe('SAMLSignInPage (native)', () => { | ||
| beforeEach(async () => { | ||
| jest.clearAllMocks(); | ||
| await act(async () => { | ||
| await Onyx.clear(); | ||
| }); | ||
| }); | ||
|
|
||
| test('sets the short-lived-token guard before opening the SAML browser (prevents the resume-after-idle teardown)', async () => { | ||
| const setGuardSpy = jest.spyOn(Session, 'setIsAuthenticatingWithShortLivedToken'); | ||
| await setCredentials(); | ||
|
|
||
| render( | ||
| <OnyxListItemProvider> | ||
| <SAMLSignInPage /> | ||
| </OnyxListItemProvider>, | ||
| ); | ||
| await waitForBatchedUpdatesWithAct(); | ||
|
|
||
| // The in-app browser was opened, and the guard was set to true BEFORE that call, so a reconnectApp() | ||
| // 407 firing while the app is backgrounded aborts reauthenticate() instead of tearing down the session. | ||
| expect(mockedOpenAuthSessionAsync).toHaveBeenCalledTimes(1); | ||
| expect(setGuardSpy).toHaveBeenCalledWith(true); | ||
| const setTrueOrder = setGuardSpy.mock.calls.findIndex((call) => call.at(0) === true); | ||
| expect(setGuardSpy.mock.invocationCallOrder.at(setTrueOrder)).toBeLessThan(mockedOpenAuthSessionAsync.mock.invocationCallOrder.at(0) ?? Infinity); | ||
| }); | ||
|
|
||
| test('clears the guard when the SAML browser is cancelled so future reauthentication is not blocked', async () => { | ||
| let guard: OnyxEntry<boolean>; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.RAM_ONLY_IS_AUTHENTICATING_WITH_SHORT_LIVED_TOKEN, | ||
| callback: (value) => { | ||
| guard = value; | ||
| }, | ||
| }); | ||
| // openAuthSessionAsync is mocked to resolve as a cancellation by default (see top of file) | ||
| await setCredentials(); | ||
|
|
||
| render( | ||
| <OnyxListItemProvider> | ||
| <SAMLSignInPage /> | ||
| </OnyxListItemProvider>, | ||
| ); | ||
| await waitForBatchedUpdatesWithAct(); | ||
|
|
||
| expect(mockedOpenAuthSessionAsync).toHaveBeenCalledTimes(1); | ||
| // After the user cancels, the guard must be cleared (otherwise reauthenticate stays blocked forever) | ||
| expect(guard).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this SAML page is opened from a state where
account.isLoadingis alreadyfalse(for example the SSO choice path afterBeginSigninhas completed), setting onlyRAM_ONLY_IS_AUTHENTICATING_WITH_SHORT_LIVED_TOKENis not enough:reauthenticate()treats that combination as stale (account?.isLoading === false), clears the guard, and then still follows theisSAMLRequiredredirect path. In that resume/407 case the new guard is removed before the SAML callback can run, so the double-login race remains for those flows unless this path also marks the account as actively loading orreauthenticate()can distinguish an open native SAML browser from a stale short-lived-token request.Useful? React with 👍 / 👎.