I'm getting
Native crypto module could not be used to get secure random number.
call
Auth.signIn
sample code
let user = await Auth.signIn({username:email, password:password})
Expected behavior
cognitoUser should be returned
Callstack
Native crypto module could not be used to get secure random number.
Also getting this error when calling signIn api inside jest:
const user = await Auth.signIn(username, pass);
win10
reverted to v1 as a temporary workaround
"@aws-amplify/auth": "^1.3.1",
"@aws-amplify/core": "^1.1.2",
I'm getting
Native crypto module could not be used to get secure random number.call
Auth.signInsample code
let user = await Auth.signIn({username:email, password:password})Expected behavior
cognitoUser should be returnedCallstack
Native crypto module could not be used to get secure random number.
- node_modules\crypto-js\core.js:45:25 in secureRandom
- node_modules\crypto-js\core.js:337:41 in random
- node_modules\amazon-cognito-identity-js\src\AuthenticationHelper.js:25:50 in randomBytes
- node_modules\amazon-cognito-identity-js\src\AuthenticationHelper.js:104:32 in generateRandomSmallA
- node_modules\amazon-cognito-identity-js\src\AuthenticationHelper.js:64:21 in AuthenticationHelper
- node_modules\amazon-cognito-identity-js\src\CognitoUser.js:405:3 in authenticateUserPlainUsernamePassword
- node_modules\amazon-cognito-identity-js\src\CognitoUser.js:211:53 in authenticateUser
- node_modules@aws-amplify\auth\lib\Auth.js:490:34 in
- node_modules\promise\setimmediate\core.js:45:7 in tryCallTwo
- node_modules\promise\setimmediate\core.js:200:23 in doResolve
- node_modules\promise\setimmediate\core.js:66:12 in Promise
node_modules@aws-amplify\auth\lib\Auth.js:489:27 in signInWithPassword
screens\auth\Authentication.js:279:39 in _callee8$
...Device: Android Emulator
Getting same error
Also getting this error when calling signIn api inside jest:
const user = await Auth.signIn(username, pass);
win10
reverted to v1 as a temporary workaround
"@aws-amplify/auth": "^1.3.1", "@aws-amplify/core": "^1.1.2",
@tommy-dev Thanks for sharing your workaround
this is due to crypto-js/core.js doing this
30:var crypto = global.crypto;
33:return Number('0.' + crypto.randomBytes(3).readUIntBE(0, 3));
I replaced 33 for
return 0.4
and it works. it seems global.crypto is not defined
@tommy-dev
which aws-amplify version you are using? or you are just using auth and core modules?
@vichoconejeros should be the latest auth and core modules, since I installed them in a fresh project today
```js
"@aws-amplify/auth":"^2.1.5",
"@aws-amplify/core": "^2.2.4",
I am getting this same error using the CognitoUser.authenticateUser
method in the amazon-cognito-identity-js
package (v3.2.4).
Get the same error when use CognitoUser.authenticateUser
method in the amazon-cognito-identity-js package (v3.2.4)
And I cannot signin and get cognito token any more, is there any work around?
Error: Native crypto module could not be used to get secure random number.
at secureRandom (/srv/node_modules/crypto-js/core.js:45:16)
at Object.random (/srv/node_modules/crypto-js/core.js:337:30)
at randomBytes (/srv/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:48:59)
at AuthenticationHelper.generateRandomSmallA (/srv/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:117:21)
at new AuthenticationHelper (/srv/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:69:29)
at CognitoUser.authenticateUserDefaultAuth (/srv/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:295:32)
at CognitoUser.authenticateUser (/srv/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:268:19)
So as @vichoconejeros pointed out. This is a [email protected]
issue. A simple workaround for now is to go into your node_modules/amazon-cognito-identity-js/
and yarn add
or npm install
the fixed version of [email protected]
.
There is an open issue at crypto-js
https://github.com/brix/crypto-js/issues/256
Another easy solution is Add resolution in your package.json remove node module
and run npm install
"resolutions": {
"crypto-js": "3.1.9-1"
}
Just a reminder. A new crypto-js version 3.2.1. has been released just 2 hours ago. All working on my side.
You may need to clear your cache before running install again!
Just for others who chance across this, it does appear that the fix with 3.2.1 makes it so that crypto-js isn't usage with ReactNative, however the resolutions solution appear to work at this time.
The error you get when trying to use 3.2.1 with RN/expo
The package at "node_modules/crypto-js/core.js" attempted to import the Node standard library module "crypto". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo
Failed building JavaScript bundle.
Another easy solution is Add resolution in your package.json remove
node module
and runnpm install
"resolutions": { "crypto-js": "3.1.9-1" }
it works!
@mdshaner you are right,"resolutions": {
"crypto-js": "3.1.9-1"
}, is causing
The package at "node_modules/crypto-js/core.js" attempted to import the Node standard library module "crypto". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo
whats is the solution
@ptrobert I'm getting the exact same error. Because of it, I'm unable to use Amplify in my expo project.
crypto-js released 3.3.0 which solves Native crypto module could not be used to get secure random number.
and node_modules/crypto-js/core.js" attempted to import the Node standard library module "crypto-js"
. Please delete node_modules and run npm install again to fix related issues
@mdshaner you are right,"resolutions": {
"crypto-js": "3.1.9-1"
}, is causingThe package at "node_modules/crypto-js/core.js" attempted to import the Node standard library module "crypto". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo
whats is the solution
use this resolution for react native, expo
"resolutions": {
"crypto-js": "3.1.9-1",
"**/crypto-js": "3.1.9-1"
},
Another easy solution is Add resolution in your package.json remove
node module
and runnpm install
"resolutions": { "crypto-js": "3.1.9-1" }
This is the solution. you can close this issue
There have been several issues with crypto-js
introducing regressions. In fact, 3.3.0
is the same as 3.1.9-1
because of bugs with 3.2.0
:
3.4.0
is out, but we're only upgrading as required because of stability issues such as these.
Closing since upgrading to the latest versions of Amplify or specifying resolutions
unblocks the original bug:
https://github.com/aws-amplify/amplify-js/issues/4886#issuecomment-584768632
I'm seeing this error again in the latest release of "@aws-amplify/auth": "^3.4.9"
Adding
"resolutions": {
"crypto-js": "3.2.1"
}
to my package.json and removing node_modules
and reinstalling doesn't help.
EDIT: forgot I was using NPM instead of Yarn. But it sounds like based on a comment below that this would still happen if I used yarn.
I'm seeing this error again in the latest release of
"@aws-amplify/auth": "^3.4.9"
Adding
"resolutions": { "crypto-js": "3.2.1" }
to my package.json and removing
node_modules
and reinstalling doesn't help.
Yeah I was playing around with [email protected] and got this error today. I searched the error and finally got here. I tried resolutions with yarn and even with npm-force-resolutions as npm doesn't support resolutions. it's very frustrating.
Yeah I was playing around with [email protected] and got this error today. I searched the error and finally got here. I tried resolutions with yarn and even with npm-force-resolutions as npm doesn't support resolutions. it's very frustrating.
Dang, I was just about to try changing my package manager to yarn. :(
If it's any help, I'm running a typescript file using ts-node ./createUser.ts
and it's throwing this error when signing in a user.
@tqhoughton I just downgraded to 4.5.1 and seems start working again
@bidva do you know what version of @aws-amplify/auth
that would correlate to?
@tqhoughton no, sorry but you can try to test decrementally
Downgraded to @aws-amplify/auth
version 3.1.0, which reintroduced a global fetch is not defined
error, but installing node-fetch
seems to have fixed that issue.
Can you share the call stack of the error you're receiving? And can you also share details of your operating environment? Sounds like @tqhoughton you're running under node - what version?
@tqhoughton @bidva we just merged a fix that should address the issue you're having in Node. Can you try installing all Amplify packages you are using again with the unstable
tag (i.e. yarn add aws-amplify@unstable
) for testing? We are planning to do a release early next week to latest
that will contain this fix.
You will also need to set the crypto
module with: global.crypto = require('crypto')
. Be sure to set that before requiring any Amplify library in your code.
I'm running "amazon-cognito-identity-js": "^4.5.2"
which uses "crypto-js": "^3.3.0"
and get the same error. None of the solutions proposed have worked for me. Even downgrading amazon-cognito-identity-js
to version 4.5.1
did not work. Any idea how to resolve the issue?
@Benzer1406 could you try using the unstable
tag with amazon-cognito-identity-js
? Also, could you share more about your app? Is it Node? If so, see my comment above regarding setting the crypto
module to global
.
@amhinson I just tried using the unstable
tag with npm install amazon-cognito-identity-js@unstable
unfortunately it keeps throwing the same error.
I'm using a serverless.com framework app, that uses the aws-nodejs-typescript template. So it is basically implementing the authentication in a nodejs app that I want to deploy in a serverless architecture (API Gateway, Lambda, ...).
I'm not sure if it's helpful, but I run into the error when invoking the lambda function locally, I haven't deployed the app to AWS yet, but I guess that shouldn't make a difference.
Trying to set crypto to global results in the following error:
@amhinson Thanks for your help. Indeed, I had to install the unstable tag on the project root and the service root folder. And as you pointed out, I had not put that line at the very top of the files that imported amazon-cognito-identity-js
. Thank you. Please keep me posted as soon as the final release is out.
@Benzer1406 ok great! We should be getting a release out in the next day or two.
@tqhoughton @bidva we just merged a fix that should address the issue you're having in Node. Can you try installing all Amplify packages you are using again with the
unstable
tag (i.e.yarn add aws-amplify@unstable
) for testing? We are planning to do a release early next week tolatest
that will contain this fix.You will also need to set the
crypto
module with:global.crypto = require('crypto')
. Be sure to set that before requiring any Amplify library in your code.
I tried doing this, and I'm still getting the same error. This is my call stack:
Error: Native crypto module could not be used to get secure random number.
at cryptoSecureRandomInt (/mnt/c/Users/tqhou/OneDrive/Documents/Projects/Century/aws/node_modules/aws-amplify/node_modules/amazon-cognito-identity-js/lib/utils/cryptoSecureRandomInt.js:43:9)
at WordArray.random (/mnt/c/Users/tqhou/OneDrive/Documents/Projects/Century/aws/node_modules/aws-amplify/node_modules/amazon-cognito-identity-js/lib/utils/WordArray.js:50:56)
at randomBytes (/mnt/c/Users/tqhou/OneDrive/Documents/Projects/Century/aws/node_modules/aws-amplify/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:40:58)
at AuthenticationHelper.generateRandomSmallA (/mnt/c/Users/tqhou/OneDrive/Documents/Projects/Century/aws/node_modules/aws-amplify/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:101:21)
at new AuthenticationHelper (/mnt/c/Users/tqhou/OneDrive/Documents/Projects/Century/aws/node_modules/aws-amplify/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:56:29)
at CognitoUser.authenticateUserDefaultAuth (/mnt/c/Users/tqhou/OneDrive/Documents/Projects/Century/aws/node_modules/aws-amplify/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:264:32)
at CognitoUser.authenticateUser (/mnt/c/Users/tqhou/OneDrive/Documents/Projects/Century/aws/node_modules/aws-amplify/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:237:19)
at /mnt/c/Users/tqhou/OneDrive/Documents/Projects/Century/aws/node_modules/aws-amplify/node_modules/@aws-amplify/auth/src/Auth.ts:571:9
at new Promise (<anonymous>)
at AuthClass.signInWithPassword (/mnt/c/Users/tqhou/OneDrive/Documents/Projects/Century/aws/node_modules/aws-amplify/node_modules/@aws-amplify/auth/src/Auth.ts:570:10)
I'm using node 12.13.1, running on an Ubuntu distribution accessed via bash.exe on my Windows 10 PC.
@tqhoughton Do you have global.crypto = require('crypto')
at the top of your file before any imports?
@tqhoughton Do you have
global.crypto = require('crypto')
at the top of your file before any imports?
馃う I did not have that. Looks like it's working now with the unstable build.
Here is my file for those who want to see what I had to add:
// needed to fix amplify signin
global.crypto = require('crypto');
import Auth from '@aws-amplify/auth';
import yargs from 'yargs';
import fs from 'fs';
import path from 'path';
const argv = yargs
.option('userPoolId', {
alias: 'u',
type: 'string',
description: 'Cognito User Pool Id'
})
.option('appClientId', {
alias: 'a',
type: 'string',
description: 'Cognito Web App Client Id'
})
.option('email', {
alias: 'e',
type: 'string',
description: 'email address of the user to create'
})
.option('password', {
alias: 'p',
type: 'string',
description: 'password of the user to create'
})
.option('region', {
alias: 'r',
type: 'string',
description: 'AWS Region to create the user in'
})
.option('output', {
alias: 'o',
type: 'string',
default: 'idToken',
description: 'Where to write the token output to'
}).argv;
const { userPoolId, appClientId, email, password, region, output } = argv;
Auth.configure({
userPoolId: userPoolId,
userPoolWebClientId: appClientId,
region
});
(async () => {
const user = await Auth.signIn({ username: email, password: password });
const idToken = user.signInUserSession.idToken.jwtToken;
console.log(idToken);
fs.writeFileSync(path.resolve(__dirname, `../${output}.token`), idToken);
})()
.catch(error => console.error(error));
Ok great! Here is an open issue you can track to see when it gets released to latest
: https://github.com/aws-amplify/amplify-js/issues/7098
"amazon-cognito-identity-js": "^4.4.0",
"aws-amplify": "^3.3.7",
"crypto-js": "^3.2.1"
and adding global.crypto = require('crypto') on top of my main.ts worked for me
I was facing this issue when I was working with amazon cognito in nodejs. After so many hit and trial i found the solution, just change the amazon-cognito-identity-js version to :
"amazon-cognito-identity-js": "^4.5.4-unstable.6"
and now it is working for me, hope it will save your time and will work for you all who is facing this issue.
Update: latest
is now up to date with the fix so global.crypto = require('crypto')
is not required anymore 馃憤
Most helpful comment
I am getting this same error using the
CognitoUser.authenticateUser
method in theamazon-cognito-identity-js
package (v3.2.4).