Repository navigation
Fix identity not being sent to Group-IB FP after authentication #82675
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
12f51f3
Log sessions and accountData when authentication completes
cristipaval 9a1a71d
Merge remote-tracking branch 'origin/main' into cristi_fix-FP-session…
cristipaval 97f6e2d
Fix FP identity not being sent after authentication
cristipaval 32a9f34
Remove log
cristipaval 9f705c7
Add a test
cristipaval 018c81f
Make checks pass
cristipaval 1b45493
Merge remote-tracking branch 'origin/main' into cristi_fix-FP-session…
cristipaval efe2432
Forward attribute updates and clear on logout
cristipaval 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import Onyx from 'react-native-onyx'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; | ||
|
|
||
| const mockSetAuthenticationData = jest.fn(); | ||
| const mockSetAttribute = jest.fn(); | ||
|
|
||
| jest.mock('@libs/FraudProtection/GroupIBSdkBridge', () => ({ | ||
| init: jest.fn(), | ||
| sendEvent: jest.fn(), | ||
| setAttribute: mockSetAttribute, | ||
| setAuthenticationData: mockSetAuthenticationData, | ||
| })); | ||
|
|
||
| // Load the module once. Onyx connections are registered at module scope. | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| require('@libs/FraudProtection'); | ||
|
|
||
| beforeAll(() => | ||
| Onyx.init({ | ||
| keys: ONYXKEYS, | ||
| }), | ||
| ); | ||
|
|
||
| beforeEach(async () => { | ||
| await Onyx.clear(); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| // Clear mocks AFTER Onyx.clear() finishes, because the clear triggers callbacks. | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| /** Returns the sessionID argument from the Nth call to setAuthenticationData. */ | ||
| function getSessionIDFromCall(callIndex: number): string { | ||
| const call = mockSetAuthenticationData.mock.calls.at(callIndex) as unknown[]; | ||
| return call.at(1) as string; | ||
| } | ||
|
|
||
| describe('FraudProtection', () => { | ||
| it('should not send auth data when only account data arrives without a session', async () => { | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: 'user@expensify.com', requiresTwoFactorAuth: false, validated: true}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not send auth data when session arrives without account data', async () => { | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token123', accountID: 12345}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should send auth data once both account and session are available', async () => { | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: 'user@expensify.com', requiresTwoFactorAuth: true, validated: true}); | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token123', accountID: 12345}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).toHaveBeenCalledTimes(1); | ||
| expect(mockSetAuthenticationData).toHaveBeenCalledWith('12345', expect.any(String)); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('email', 'user@expensify.com', false, true); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('mfa', '2fa_enabled', false, true); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('is_validated', 'true', false, true); | ||
| }); | ||
|
|
||
| it('should send auth data when session arrives after account (multi-step sign-in)', async () => { | ||
| // Step 1: BeginSignIn returns account data (no session yet) | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: 'user@expensify.com', requiresTwoFactorAuth: false, validated: true}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).not.toHaveBeenCalled(); | ||
|
|
||
| // Step 2: Magic code verified, session is created | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token123', accountID: 12345}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).toHaveBeenCalledTimes(1); | ||
| expect(mockSetAuthenticationData).toHaveBeenCalledWith('12345', expect.any(String)); | ||
| }); | ||
|
|
||
| it('should not re-send identity when session updates but identity has not changed, but should re-send attributes', async () => { | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: 'user@expensify.com', requiresTwoFactorAuth: false, validated: false}); | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token123', accountID: 12345}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).toHaveBeenCalledTimes(1); | ||
| jest.clearAllMocks(); | ||
|
|
||
| // Session token refreshes but accountID stays the same | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token456', accountID: 12345}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).not.toHaveBeenCalled(); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('email', 'user@expensify.com', false, true); | ||
| }); | ||
|
|
||
| it('should forward updated account attributes for the same identity', async () => { | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: 'user@expensify.com', requiresTwoFactorAuth: false, validated: false}); | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token123', accountID: 12345}); | ||
| await waitForBatchedUpdates(); | ||
| jest.clearAllMocks(); | ||
|
|
||
| // User enables 2FA while already logged in | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {requiresTwoFactorAuth: true}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).not.toHaveBeenCalled(); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('mfa', '2fa_enabled', false, true); | ||
| }); | ||
|
|
||
| it('should send new session with empty identity on logout', async () => { | ||
| // Login | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: 'user@expensify.com', requiresTwoFactorAuth: false, validated: true}); | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token123', accountID: 12345}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const firstSessionID = getSessionIDFromCall(0); | ||
| jest.clearAllMocks(); | ||
|
|
||
| // Logout: clear the session (Onyx.merge strips undefined, so use set with null) | ||
| await Onyx.set(ONYXKEYS.SESSION, null); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).toHaveBeenCalledTimes(1); | ||
| expect(mockSetAuthenticationData).toHaveBeenCalledWith('', expect.any(String)); | ||
|
|
||
| // The new sessionID should differ from the one used during auth | ||
| const logoutSessionID = getSessionIDFromCall(0); | ||
| expect(logoutSessionID).not.toBe(firstSessionID); | ||
|
|
||
| // Previous user's attributes should be cleared on logout | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('email', '', false, true); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('mfa', '2fa_disabled', false, true); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('is_validated', 'false', false, true); | ||
| }); | ||
|
|
||
| it('should use a new sessionID after logout and re-login', async () => { | ||
| // First login | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: 'user@expensify.com', requiresTwoFactorAuth: false, validated: true}); | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token123', accountID: 12345}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const firstSessionID = getSessionIDFromCall(0); | ||
|
|
||
| // Logout | ||
| await Onyx.set(ONYXKEYS.SESSION, null); | ||
| await waitForBatchedUpdates(); | ||
| jest.clearAllMocks(); | ||
|
|
||
| // Second login with a different account | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: 'other@expensify.com', requiresTwoFactorAuth: true, validated: false}); | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token999', accountID: 67890}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAuthenticationData).toHaveBeenCalledTimes(1); | ||
| expect(mockSetAuthenticationData).toHaveBeenCalledWith('67890', expect.any(String)); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('email', 'other@expensify.com', false, true); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('mfa', '2fa_enabled', false, true); | ||
| expect(mockSetAttribute).toHaveBeenCalledWith('is_validated', 'false', false, true); | ||
|
|
||
| // Should use a different sessionID than the first login | ||
| const secondSessionID = getSessionIDFromCall(0); | ||
| expect(secondSessionID).not.toBe(firstSessionID); | ||
| }); | ||
|
|
||
| it('should set 2FA attribute correctly based on account data', async () => { | ||
| // Without 2FA | ||
| await Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin: 'user@expensify.com', requiresTwoFactorAuth: false, validated: true}); | ||
| await Onyx.merge(ONYXKEYS.SESSION, {authToken: 'token123', accountID: 11111}); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| expect(mockSetAttribute).toHaveBeenCalledWith('mfa', '2fa_disabled', false, true); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.