-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathCognitoUserPool.js
194 lines (159 loc) · 5.81 KB
/
CognitoUserPool.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*!
* Copyright 2016 Amazon.com,
* Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the
* License. A copy of the License is located at
*
* http://aws.amazon.com/asl/
*
* or in the "license" file accompanying this file. This file is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, express or implied. See the License
* for the specific language governing permissions and
* limitations under the License.
*/
import Client from './Client';
import CognitoUser from './CognitoUser';
import StorageHelper from './StorageHelper';
/** @class */
var CognitoUserPool = function () {
/**
* Constructs a new CognitoUserPool object
* @param {object} data Creation options.
* @param {string} data.UserPoolId Cognito user pool id.
* @param {string} data.ClientId User pool application client id.
* @param {object} data.Storage Optional storage object.
* @param {boolean} data.AdvancedSecurityDataCollectionFlag Optional:
* boolean flag indicating if the data collection is enabled
* to support cognito advanced security features. By default, this
* flag is set to true.
*/
function CognitoUserPool(data) {
_classCallCheck(this, CognitoUserPool);
var _ref = data || {},
UserPoolId = _ref.UserPoolId,
ClientId = _ref.ClientId,
endpoint = _ref.endpoint,
AdvancedSecurityDataCollectionFlag = _ref.AdvancedSecurityDataCollectionFlag;
if (!UserPoolId || !ClientId) {
throw new Error('Both UserPoolId and ClientId are required.');
}
if (!/^[\w-]+_.+$/.test(UserPoolId)) {
throw new Error('Invalid UserPoolId format.');
}
var region = UserPoolId.split('_')[0];
this.userPoolId = UserPoolId;
this.clientId = ClientId;
this.client = new Client(region, endpoint);
/**
* By default, AdvancedSecurityDataCollectionFlag is set to true,
* if no input value is provided.
*/
this.advancedSecurityDataCollectionFlag = AdvancedSecurityDataCollectionFlag !== false;
this.storage = data.Storage || new StorageHelper().getStorage();
}
/**
* @returns {string} the user pool id
*/
CognitoUserPool.prototype.getUserPoolId = function getUserPoolId() {
return this.userPoolId;
};
/**
* @returns {string} the client id
*/
CognitoUserPool.prototype.getClientId = function getClientId() {
return this.clientId;
};
/**
* @typedef {object} SignUpResult
* @property {CognitoUser} user New user.
* @property {bool} userConfirmed If the user is already confirmed.
*/
/**
* method for signing up a user
* @param {string} username User's username.
* @param {string} password Plain-text initial password entered by user.
* @param {(AttributeArg[])=} userAttributes New user attributes.
* @param {(AttributeArg[])=} validationData Application metadata.
* @param {nodeCallback<SignUpResult>} callback Called on error or with the new user.
* @returns {void}
*/
CognitoUserPool.prototype.signUp = function signUp(username, password, userAttributes, validationData, callback) {
var _this = this;
var jsonReq = {
ClientId: this.clientId,
Username: username,
Password: password,
UserAttributes: userAttributes,
ValidationData: validationData
};
if (this.getUserContextData(username)) {
jsonReq.UserContextData = this.getUserContextData(username);
}
this.client.request('SignUp', jsonReq, function (err, data) {
if (err) {
return callback(err, null);
}
var cognitoUser = {
Username: username,
Pool: _this,
Storage: _this.storage
};
var returnData = {
user: new CognitoUser(cognitoUser),
userConfirmed: data.UserConfirmed,
userSub: data.UserSub
};
return callback(null, returnData);
});
};
/**
* method for getting the current user of the application from the local storage
*
* @returns {CognitoUser} the user retrieved from storage
*/
CognitoUserPool.prototype.getCurrentUser = function getCurrentUser() {
var lastUserKey = 'CognitoIdentityServiceProvider.' + this.clientId + '.LastAuthUser';
var lastAuthUser = this.storage.getItem(lastUserKey);
if (lastAuthUser) {
var cognitoUser = {
Username: lastAuthUser,
Pool: this,
Storage: this.storage
};
return new CognitoUser(cognitoUser);
}
return null;
};
/**
* This method returns the encoded data string used for cognito advanced security feature.
* This would be generated only when developer has included the JS used for collecting the
* data on their client. Please refer to documentation to know more about using AdvancedSecurity
* features
* @param {string} username the username for the context data
* @returns {string} the user context data
**/
CognitoUserPool.prototype.getUserContextData = function getUserContextData(username) {
if (typeof AmazonCognitoAdvancedSecurityData === 'undefined') {
return undefined;
}
/* eslint-disable */
var amazonCognitoAdvancedSecurityDataConst = AmazonCognitoAdvancedSecurityData;
/* eslint-enable */
if (this.advancedSecurityDataCollectionFlag) {
var advancedSecurityData = amazonCognitoAdvancedSecurityDataConst.getData(username, this.userPoolId, this.clientId);
if (advancedSecurityData) {
var userContextData = {
EncodedData: advancedSecurityData
};
return userContextData;
}
}
return {};
};
return CognitoUserPool;
}();
export default CognitoUserPool;