1 | /*!
|
2 | * Copyright 2016 Amazon.com,
|
3 | * Inc. or its affiliates. All Rights Reserved.
|
4 | *
|
5 | * Licensed under the Amazon Software License (the "License").
|
6 | * You may not use this file except in compliance with the
|
7 | * License. A copy of the License is located at
|
8 | *
|
9 | * http://aws.amazon.com/asl/
|
10 | *
|
11 | * or in the "license" file accompanying this file. This file is
|
12 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
13 | * CONDITIONS OF ANY KIND, express or implied. See the License
|
14 | * for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | import Client from './Client';
|
18 | import CognitoUser from './CognitoUser';
|
19 | import StorageHelper from './StorageHelper';
|
20 | /** @class */
|
21 |
|
22 | var CognitoUserPool = /*#__PURE__*/function () {
|
23 | /**
|
24 | * Constructs a new CognitoUserPool object
|
25 | * @param {object} data Creation options.
|
26 | * @param {string} data.UserPoolId Cognito user pool id.
|
27 | * @param {string} data.ClientId User pool application client id.
|
28 | * @param {string} data.endpoint Optional custom service endpoint.
|
29 | * @param {object} data.fetchOptions Optional options for fetch API.
|
30 | * (only credentials option is supported)
|
31 | * @param {object} data.Storage Optional storage object.
|
32 | * @param {boolean} data.AdvancedSecurityDataCollectionFlag Optional:
|
33 | * boolean flag indicating if the data collection is enabled
|
34 | * to support cognito advanced security features. By default, this
|
35 | * flag is set to true.
|
36 | */
|
37 | function CognitoUserPool(data, wrapRefreshSessionCallback) {
|
38 | var _ref = data || {},
|
39 | UserPoolId = _ref.UserPoolId,
|
40 | ClientId = _ref.ClientId,
|
41 | endpoint = _ref.endpoint,
|
42 | fetchOptions = _ref.fetchOptions,
|
43 | AdvancedSecurityDataCollectionFlag = _ref.AdvancedSecurityDataCollectionFlag;
|
44 |
|
45 | if (!UserPoolId || !ClientId) {
|
46 | throw new Error('Both UserPoolId and ClientId are required.');
|
47 | }
|
48 |
|
49 | if (!/^[\w-]+_.+$/.test(UserPoolId)) {
|
50 | throw new Error('Invalid UserPoolId format.');
|
51 | }
|
52 |
|
53 | var region = UserPoolId.split('_')[0];
|
54 | this.userPoolId = UserPoolId;
|
55 | this.clientId = ClientId;
|
56 | this.client = new Client(region, endpoint, fetchOptions);
|
57 | /**
|
58 | * By default, AdvancedSecurityDataCollectionFlag is set to true,
|
59 | * if no input value is provided.
|
60 | */
|
61 |
|
62 | this.advancedSecurityDataCollectionFlag = AdvancedSecurityDataCollectionFlag !== false;
|
63 | this.storage = data.Storage || new StorageHelper().getStorage();
|
64 |
|
65 | if (wrapRefreshSessionCallback) {
|
66 | this.wrapRefreshSessionCallback = wrapRefreshSessionCallback;
|
67 | }
|
68 | }
|
69 | /**
|
70 | * @returns {string} the user pool id
|
71 | */
|
72 |
|
73 |
|
74 | var _proto = CognitoUserPool.prototype;
|
75 |
|
76 | _proto.getUserPoolId = function getUserPoolId() {
|
77 | return this.userPoolId;
|
78 | }
|
79 | /**
|
80 | * @returns {string} the client id
|
81 | */
|
82 | ;
|
83 |
|
84 | _proto.getClientId = function getClientId() {
|
85 | return this.clientId;
|
86 | }
|
87 | /**
|
88 | * @typedef {object} SignUpResult
|
89 | * @property {CognitoUser} user New user.
|
90 | * @property {bool} userConfirmed If the user is already confirmed.
|
91 | */
|
92 |
|
93 | /**
|
94 | * method for signing up a user
|
95 | * @param {string} username User's username.
|
96 | * @param {string} password Plain-text initial password entered by user.
|
97 | * @param {(AttributeArg[])=} userAttributes New user attributes.
|
98 | * @param {(AttributeArg[])=} validationData Application metadata.
|
99 | * @param {(AttributeArg[])=} clientMetadata Client metadata.
|
100 | * @param {nodeCallback<SignUpResult>} callback Called on error or with the new user.
|
101 | * @param {ClientMetadata} clientMetadata object which is passed from client to Cognito Lambda trigger
|
102 | * @returns {void}
|
103 | */
|
104 | ;
|
105 |
|
106 | _proto.signUp = function signUp(username, password, userAttributes, validationData, callback, clientMetadata) {
|
107 | var _this = this;
|
108 |
|
109 | var jsonReq = {
|
110 | ClientId: this.clientId,
|
111 | Username: username,
|
112 | Password: password,
|
113 | UserAttributes: userAttributes,
|
114 | ValidationData: validationData,
|
115 | ClientMetadata: clientMetadata
|
116 | };
|
117 |
|
118 | if (this.getUserContextData(username)) {
|
119 | jsonReq.UserContextData = this.getUserContextData(username);
|
120 | }
|
121 |
|
122 | this.client.request('SignUp', jsonReq, function (err, data) {
|
123 | if (err) {
|
124 | return callback(err, null);
|
125 | }
|
126 |
|
127 | var cognitoUser = {
|
128 | Username: username,
|
129 | Pool: _this,
|
130 | Storage: _this.storage
|
131 | };
|
132 | var returnData = {
|
133 | user: new CognitoUser(cognitoUser),
|
134 | userConfirmed: data.UserConfirmed,
|
135 | userSub: data.UserSub,
|
136 | codeDeliveryDetails: data.CodeDeliveryDetails
|
137 | };
|
138 | return callback(null, returnData);
|
139 | });
|
140 | }
|
141 | /**
|
142 | * method for getting the current user of the application from the local storage
|
143 | *
|
144 | * @returns {CognitoUser} the user retrieved from storage
|
145 | */
|
146 | ;
|
147 |
|
148 | _proto.getCurrentUser = function getCurrentUser() {
|
149 | var lastUserKey = "CognitoIdentityServiceProvider." + this.clientId + ".LastAuthUser";
|
150 | var lastAuthUser = this.storage.getItem(lastUserKey);
|
151 |
|
152 | if (lastAuthUser) {
|
153 | var cognitoUser = {
|
154 | Username: lastAuthUser,
|
155 | Pool: this,
|
156 | Storage: this.storage
|
157 | };
|
158 | return new CognitoUser(cognitoUser);
|
159 | }
|
160 |
|
161 | return null;
|
162 | }
|
163 | /**
|
164 | * This method returns the encoded data string used for cognito advanced security feature.
|
165 | * This would be generated only when developer has included the JS used for collecting the
|
166 | * data on their client. Please refer to documentation to know more about using AdvancedSecurity
|
167 | * features
|
168 | * @param {string} username the username for the context data
|
169 | * @returns {string} the user context data
|
170 | **/
|
171 | ;
|
172 |
|
173 | _proto.getUserContextData = function getUserContextData(username) {
|
174 | if (typeof AmazonCognitoAdvancedSecurityData === 'undefined') {
|
175 | return undefined;
|
176 | }
|
177 | /* eslint-disable */
|
178 |
|
179 |
|
180 | var amazonCognitoAdvancedSecurityDataConst = AmazonCognitoAdvancedSecurityData;
|
181 | /* eslint-enable */
|
182 |
|
183 | if (this.advancedSecurityDataCollectionFlag) {
|
184 | var advancedSecurityData = amazonCognitoAdvancedSecurityDataConst.getData(username, this.userPoolId, this.clientId);
|
185 |
|
186 | if (advancedSecurityData) {
|
187 | var userContextData = {
|
188 | EncodedData: advancedSecurityData
|
189 | };
|
190 | return userContextData;
|
191 | }
|
192 | }
|
193 |
|
194 | return {};
|
195 | };
|
196 |
|
197 | return CognitoUserPool;
|
198 | }();
|
199 |
|
200 | export { CognitoUserPool as default }; |
\ | No newline at end of file |