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

fix(auth): request initiateAuth with retry #13854

Merged
merged 2 commits into from
Oct 8, 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
22 changes: 22 additions & 0 deletions packages/amazon-cognito-identity-js/__mocks__/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ export function netRequestMockSuccess(success, data = {}) {
}
}

/**
* Mock a single network request to be either successful or fail with an optional data object
* @param {boolean} success defines if a network request is successful
* @param {object?} optional data to return onSuccess, some tests requires specific object values
* @param {string?} optional operationName to provide clarity inside the testing function
*/
export function netRequestWithRetryMockSuccess(success, data = {}) {
if (success) {
jest
.spyOn(Client.prototype, 'requestWithRetry')
.mockImplementationOnce((...[, , callback]) => {
callback(null, data);
});
} else {
jest
.spyOn(Client.prototype, 'requestWithRetry')
.mockImplementationOnce((...[, , callback]) => {
callback(networkError, null);
});
}
}

/**
*
* @param {string} fnName function name within the AuthenticationHelper class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
callback,
authHelperMock,
netRequestMockSuccess,
netRequestWithRetryMockSuccess,
} from '../__mocks__/mocks';

import {
Expand Down Expand Up @@ -1609,7 +1610,7 @@ describe('refreshSession()', () => {
});

test('happy path for refresh session ', () => {
netRequestMockSuccess(true, {
netRequestWithRetryMockSuccess(true, {
AuthenticationResult: { RefreshToken: null },
});
cognitoUser.refreshSession(...refreshSessionDefaults);
Expand All @@ -1618,7 +1619,7 @@ describe('refreshSession()', () => {
);
});
test('client throws an error ', () => {
netRequestMockSuccess(false);
netRequestWithRetryMockSuccess(false);
cognitoUser.refreshSession(...refreshSessionDefaults);
expect(callback.mock.calls[0][0]).toMatchObject(new Error('Network Error'));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const crypto = require('crypto');

jest.mock('crypto', () => ({
getRandomValues: jest.fn(),
randomBytes: jest.fn(() => ({
readInt32LE: jest.fn().mockReturnValueOnce(54321),
})),
}));

const mockCrypto = crypto;

describe('cryptoSecureRandomInt test', () => {
let windowSpy;

Expand All @@ -19,8 +28,8 @@ describe('cryptoSecureRandomInt test', () => {
},
}));

const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt')
.default;
const cryptoSecureRandomInt =
require('../src/utils/cryptoSecureRandomInt').default;
expect(window.crypto).toBeTruthy();
expect(cryptoSecureRandomInt()).toBe(12345);
});
Expand All @@ -33,27 +42,37 @@ describe('cryptoSecureRandomInt test', () => {
},
}));

const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt')
.default;
const cryptoSecureRandomInt =
require('../src/utils/cryptoSecureRandomInt').default;
expect(window.msCrypto).toBeTruthy();
expect(cryptoSecureRandomInt()).toBe(67890);
});

test('crypto is set for Node', () => {
windowSpy.mockImplementation(() => ({
test('crypto is set for Node (< 18)', () => {
windowSpy.mockImplementationOnce(() => ({
crypto: null,
}));

const randomBytesMock = jest
.spyOn(crypto, 'randomBytes')
.mockImplementationOnce(() => ({
readInt32LE: jest.fn().mockReturnValueOnce(54321),
}));
const originalGetRandomValues = mockCrypto.getRandomValues;

const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt')
.default;
mockCrypto.getRandomValues = undefined;

const cryptoSecureRandomInt =
require('../src/utils/cryptoSecureRandomInt').default;
expect(cryptoSecureRandomInt()).toBe(54321);

randomBytesMock.mockRestore();
mockCrypto.getRandomValues = originalGetRandomValues;
});

test('crypto is set for Node (>= 18)', () => {
windowSpy.mockImplementationOnce(() => ({
crypto: null,
}));

mockCrypto.getRandomValues.mockReturnValueOnce([54321]);

const cryptoSecureRandomInt =
require('../src/utils/cryptoSecureRandomInt').default;
expect(cryptoSecureRandomInt()).toBe(54321);
});
});
4 changes: 2 additions & 2 deletions packages/amazon-cognito-identity-js/src/CognitoUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ export default class CognitoUser {
UserAttributes: attributes,
ClientMetadata: clientMetadata,
},
(err,result) => {
(err, result) => {
if (err) {
return callback(err, null);
}
Expand Down Expand Up @@ -1478,7 +1478,7 @@ export default class CognitoUser {
if (this.getUserContextData()) {
jsonReq.UserContextData = this.getUserContextData();
}
this.client.request('InitiateAuth', jsonReq, (err, authResult) => {
this.client.requestWithRetry('InitiateAuth', jsonReq, (err, authResult) => {
if (err) {
if (err.code === 'NotAuthorizedException') {
this.clearCachedUser();
Expand Down
Loading