-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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(amazon-cognito-identity-js): set crypto for Node #7136
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const crypto = require('crypto'); | ||
|
||
describe('cryptoSecureRandomInt test', () => { | ||
let windowSpy; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
windowSpy = jest.spyOn(window, 'window', 'get'); | ||
}); | ||
|
||
afterEach(() => { | ||
windowSpy.mockRestore(); | ||
}); | ||
|
||
test('crypto is set for window (browser)', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
crypto: { | ||
getRandomValues: () => [12345], | ||
}, | ||
})); | ||
|
||
const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt') | ||
.default; | ||
expect(window.crypto).toBeTruthy(); | ||
expect(cryptoSecureRandomInt()).toBe(12345); | ||
}); | ||
|
||
test('crypto is set for window (IE 11)', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
crypto: undefined, | ||
msCrypto: { | ||
getRandomValues: () => [67890], | ||
}, | ||
})); | ||
|
||
const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt') | ||
.default; | ||
expect(window.msCrypto).toBeTruthy(); | ||
expect(cryptoSecureRandomInt()).toBe(67890); | ||
}); | ||
|
||
test('crypto is set for Node', () => { | ||
windowSpy.mockImplementation(() => ({ | ||
crypto: null, | ||
})); | ||
|
||
const randomBytesMock = jest | ||
.spyOn(crypto, 'randomBytes') | ||
.mockImplementationOnce(() => ({ | ||
readInt32LE: jest.fn().mockReturnValueOnce(54321), | ||
})); | ||
|
||
const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt') | ||
.default; | ||
expect(cryptoSecureRandomInt()).toBe(54321); | ||
|
||
randomBytesMock.mockRestore(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,9 @@ var config = { | |
devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils') | ||
.devtoolModuleFilenameTemplate, | ||
}, | ||
externals: { | ||
crypto: 'crypto', | ||
}, | ||
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed for the |
||
plugins: [ | ||
new webpack.optimize.OccurrenceOrderPlugin(), | ||
new webpack.BannerPlugin({ banner, raw: true }), | ||
|
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.
This is based on how
crypto-js
handles this case: https://github.com/brix/crypto-js/blob/develop/src/core.js#L25-L30