forked from IxorTalk/ixortalk.aws.cognito.token.retrieval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (129 loc) · 4.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node
var AWS = require('aws-sdk');
var AWSCognito = require('amazon-cognito-identity-js');
var util = require('util');
var request = require('request');
var rp = require('request-promise');
var readline = require('readline');
var fs = require('fs');
var os = require('os');
var program = require('commander');
program
.version('0.0.1')
.option('-e, --environment <environment>', 'The AWS cognito environment')
.option('-u, --username <username>', 'The username')
.option('-p, --password <password>', 'The password (when not provided, the one from the config will be used)')
.parse(process.argv);
console.log(fs.readFileSync(__dirname + '/resources/banner.txt', 'utf8'));
var awsEnvironments = {};
try {
var fileContents = fs.readFileSync(os.homedir() + '/.ixortalk.aws.cognito/cognito-env.json', 'utf8')
awsEnvironments = JSON.parse(fileContents);
} catch (err) {
console.log ("No cognito environment file found in " + os.homedir() + "/.ixortalk.aws.cognito/");
program.help()
}
try {
var fileContents = fs.readFileSync(os.homedir() + '/.ixortalk.aws.cognito/users.json', 'utf8')
credentialList = JSON.parse(fileContents);
} catch (err) {
console.log ("No users file found in " + os.homedir() + "/.ixortalk.aws.cognito/");
program.help()
}
if (!program.environment) {
console.log ("No valid environment specified.");
program.help()
}
var awsEnvironment = awsEnvironments[program.environment];
if (!awsEnvironment) {
console.log ("Environment " + program.environment + " not found.");
program.help()
}
if (!program.username) {
console.log ("No username provided");
program.help()
}
var credentials = credentialList[program.username];
if (!credentials) {
if (!program.password) {
console.log("No password provided for {}",program.username)
program.help()
} else {
credentials = {Password: program.password}
}
} else {
if (program.password) {
credentials = {Password: program.password}
}
}
console.log("\nawsEnvironment : \n",awsEnvironment)
console.log("\nuser : \n",program.username)
console.log("\ncredential : \n",credentials)
AWS.config.update({region: awsEnvironment.AWSRegion});
var authenticationData = {
Username : program.username,
Password : credentials.Password
};
var authenticationDetails = new AWSCognito.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId : awsEnvironment.UserPoolId,
ClientId : awsEnvironment.ClientId
};
var userPool = new AWSCognito.CognitoUserPool(poolData);
var userData = {
Username : program.username,
Pool : userPool
};
var cognitoUser = new AWSCognito.CognitoUser(userData);
// cognitoUser.forgotPassword({
// onSuccess: function (result) {
// console.log('call result: ' + result);
// },
// onFailure: function(err) {
// console.log('call err: ' + err);
// },
// inputVerificationCode() {
// console.log("Entering verification code..")
//
// const rl = readline.createInterface({
// input: process.stdin,
// output: process.stdout
// });
//
// rl.question('Please enter your verification code ', (verificationCode) => {
// console.log("You entered : ${verificationCode}");
// rl.close();
// var newPassword = "AdminAdmin_123"
// cognitoUser.confirmPassword(verificationCode, newPassword, this);
// });
//
// }
// });
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
cognitoUserPoolLoginProvider = 'cognito-idp.' + awsEnvironment.AWSRegion + '.amazonaws.com/' + awsEnvironment.UserPoolId;
var logins = {};
logins[cognitoUserPoolLoginProvider] = result.getIdToken().getJwtToken();
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId : awsEnvironment.IdentityPoolId,
Logins : logins
});
console.log("\n\nexport TOKEN=" + result.getIdToken().getJwtToken())
},
onFailure: function(err) {
console.log("Error authenticating ! ",err);
},
// When the user is in a FORCE_CHANGE_PASSWORD state, the user needs to enter a new password.
// Hence this function. By default we will simply set the same password again.
newPasswordRequired: function(userAttributes, requiredAttributes) {
cognitoUser.completeNewPasswordChallenge(credentials.Password, {}, {
onSuccess: function (result) {
console.log("New password challenge confirmed, re-executing will provide a token.")
},
onFailure: function (error) {
console.error(error.message)
}
}
)
}
});