Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Next, if you are not already logged in to the Vendia Share [Command Line Interfa
share login
```

After that, you're ready to creat your Vendia Share Uni.
After that, you're ready to create your Vendia Share Uni.

```
cd uni_configuration
Expand Down
117 changes: 78 additions & 39 deletions features/share/access-controls/share-auth/cognitoLogin.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,85 @@
import AmazonCognitoIdentity from 'amazon-cognito-identity-js';
import AWS from 'aws-sdk';
import AmazonCognitoIdentity from "amazon-cognito-identity-js";
import AWS from "aws-sdk";
import inquirer from "inquirer";

export function Login(username, password, userPoolId, clientId, region, identityPoolId) {
export function Login(
username,
password,
userPoolId,
clientId,
region,
identityPoolId
) {

return new Promise((resolve, reject) => {
let authenticationData = { Username: username, Password: password };
let authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(
authenticationData
);
let poolData = { UserPoolId: userPoolId, ClientId: clientId };
let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
let userData = { Username: username, Pool: userPool };
let cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
const onSuccess = (result) => {
const identityJwt = result.getIdToken().getJwtToken();
let Logins = {};
Logins[
`cognito-idp.${region}.amazonaws.com/${userPoolId}`
] = identityJwt;

return new Promise((resolve,reject)=> {
AWS.config.region = region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials(
{
IdentityPoolId: identityPoolId,
Logins,
}
);

let authenticationData = {Username: username, Password: password};
let authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
let poolData = {UserPoolId: userPoolId, ClientId: clientId};
let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
let userData = {Username: username, Pool: userPool};
let cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
AWS.config.credentials.get(function () {
const accessKeyId = AWS.config.credentials.accessKeyId;
const secretAccessKey =
AWS.config.credentials.secretAccessKey;
const sessionToken = AWS.config.credentials.sessionToken;

cognitoUser.authenticateUser(authenticationDetails, {

onSuccess: function(result) {
const identityJwt = result.getIdToken().getJwtToken();

let Logins={};
Logins[`cognito-idp.${region}.amazonaws.com/${userPoolId}`] = identityJwt;

AWS.config.region = region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: identityPoolId,
Logins
});

AWS.config.credentials.get(
function() {
const accessKeyId = AWS.config.credentials.accessKeyId;
const secretAccessKey = AWS.config.credentials.secretAccessKey;
const sessionToken = AWS.config.credentials.sessionToken;

resolve({identityJwt, accessKeyId, secretAccessKey, sessionToken});
}
);
resolve({
identityJwt,
accessKeyId,
secretAccessKey,
sessionToken,
});
});
}
const onFailure = function (err) {
reject(err.message || JSON.stringify(err));
};
cognitoUser.authenticateUser(authenticationDetails, {
totpRequired: function (challengeName, challengeParameters) {
inquirer
.prompt([
{
name: "AuthCode",
type: "password",
message: "Auth code:",
},

onFailure: function(err) {
reject(err.message || JSON.stringify(err));
])
.then(
(result) => {
cognitoUser.sendMFACode(
result.AuthCode,
{
onSuccess,
onFailure,
},
challengeName
);
},
(err) => {
console.error(err);
}

});
);
},
onSuccess,
onFailure,
});
};
});
}
Loading