forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add option to disable idp oauth flow
- Loading branch information
Ashwin Kumar
committed
May 29, 2024
1 parent
b88df66
commit e2d577f
Showing
3 changed files
with
147 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -281,6 +281,7 @@ const createMockLocalStorage = () => | |
|
||
import { AuthOptions, SignUpParams, AwsCognitoOAuthOpts } from '../src/types'; | ||
import { AuthClass as Auth } from '../src/Auth'; | ||
import * as urlListener from '../src/urlListener'; | ||
import { Credentials, StorageHelper, Hub } from '@aws-amplify/core'; | ||
import { AuthError, NoUserPoolError } from '../src/Errors'; | ||
import { AuthErrorTypes } from '../src/types/Auth'; | ||
|
@@ -3544,6 +3545,53 @@ describe('auth unit test', () => { | |
spyon3.mockClear(); | ||
expect(urlOpener).not.toBeCalled(); | ||
}); | ||
|
||
test('should add SP initiated inflight flag to local storage', async () => { | ||
const localStorageSetItemMock = jest.fn(); | ||
jest | ||
.spyOn(StorageHelper.prototype, 'getStorage') | ||
.mockImplementation(() => { | ||
return { | ||
setItem: localStorageSetItemMock, | ||
getItem: jest.fn(), | ||
removeItem: jest.fn(), | ||
}; | ||
}); | ||
let user; | ||
jest.spyOn(Credentials, 'set').mockImplementationOnce(() => { | ||
user = { name: 'username', email: '[email protected]' }; | ||
return Promise.resolve('cred' as any); | ||
}); | ||
jest | ||
.spyOn(Auth.prototype, 'currentAuthenticatedUser') | ||
.mockImplementation(() => { | ||
if (!user) return Promise.reject('error'); | ||
else return Promise.resolve(user); | ||
}); | ||
|
||
const options: AuthOptions = { | ||
region: 'region', | ||
identityPoolId: 'awsCognitoIdentityPoolId', | ||
userPoolId: 'userPoolId', | ||
oauth: { | ||
domain: 'mydomain.auth.us-east-1.amazoncognito.com', | ||
scope: ['aws.cognito.signin.user.admin'], | ||
redirectSignIn: 'http://localhost:3000/', | ||
redirectSignOut: 'http://localhost:3000/', | ||
responseType: 'code', | ||
urlOpener: jest.fn(), | ||
}, | ||
}; | ||
|
||
const auth = new Auth(options); | ||
await auth.federatedSignIn(); | ||
|
||
expect(localStorageSetItemMock).toBeCalled(); | ||
expect(localStorageSetItemMock).toHaveBeenCalledWith( | ||
'amplify-sp-initiated-oauth-inFlight', | ||
'true' | ||
); | ||
}); | ||
}); | ||
|
||
describe('handleAuthResponse test', () => { | ||
|
@@ -3561,6 +3609,12 @@ describe('auth unit test', () => { | |
setItem() { | ||
return null; | ||
}, | ||
getItem() { | ||
return null; | ||
}, | ||
removeItem() { | ||
return null; | ||
}, | ||
}; | ||
}); | ||
}); | ||
|
@@ -3759,6 +3813,75 @@ describe('auth unit test', () => { | |
}); | ||
}); | ||
|
||
describe('OAuth flow', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should call urlListener by default', async () => { | ||
const urlListenerSpy = jest.spyOn(urlListener, 'default'); | ||
const options: AuthOptions = { | ||
region: 'region', | ||
userPoolId: 'userPoolId', | ||
oauth: { | ||
domain: 'mydomain.auth.us-east-1.amazoncognito.com', | ||
scope: ['aws.cognito.signin.user.admin'], | ||
redirectSignIn: 'http://localhost:3000/', | ||
redirectSignOut: 'http://localhost:3000/', | ||
responseType: 'code', | ||
}, | ||
identityPoolId: 'awsCognitoIdentityPoolId', | ||
}; | ||
new Auth(options); | ||
expect(urlListenerSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('should not call urlListener when IDP initiated oauth is disabled', async () => { | ||
const urlListenerSpy = jest.spyOn(urlListener, 'default'); | ||
const options: AuthOptions = { | ||
region: 'region', | ||
userPoolId: 'userPoolId', | ||
oauth: { | ||
domain: 'mydomain.auth.us-east-1.amazoncognito.com', | ||
scope: ['aws.cognito.signin.user.admin'], | ||
redirectSignIn: 'http://localhost:3000/', | ||
redirectSignOut: 'http://localhost:3000/', | ||
responseType: 'code', | ||
idpEnabled: false, | ||
}, | ||
identityPoolId: 'awsCognitoIdentityPoolId', | ||
}; | ||
new Auth(options); | ||
expect(urlListenerSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should call urlListener when SP initiated oauth is in flight and IDP is disabled', async () => { | ||
const mockLocalStorage = createMockLocalStorage(); | ||
jest | ||
.spyOn(StorageHelper.prototype, 'getStorage') | ||
.mockImplementation(() => mockLocalStorage); | ||
mockLocalStorage.setItem('amplify-sp-initiated-oauth-inFlight', 'true'); | ||
|
||
const urlListenerSpy = jest.spyOn(urlListener, 'default'); | ||
|
||
const options: AuthOptions = { | ||
region: 'region', | ||
userPoolId: 'userPoolId', | ||
oauth: { | ||
domain: 'mydomain.auth.us-east-1.amazoncognito.com', | ||
scope: ['aws.cognito.signin.user.admin'], | ||
redirectSignIn: 'http://localhost:3000/', | ||
redirectSignOut: 'http://localhost:3000/', | ||
responseType: 'code', | ||
idpEnabled: false, | ||
}, | ||
identityPoolId: 'awsCognitoIdentityPoolId', | ||
}; | ||
new Auth(options); | ||
expect(urlListenerSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('verifiedContact test', () => { | ||
test('happy case with unverified', async () => { | ||
const spyon = jest | ||
|
This file contains 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 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