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 |
|
18 | import Client from './Client';
|
19 | import CognitoUser from './CognitoUser';
|
20 | import StorageHelper from './StorageHelper';
|
21 |
|
22 | /** @class */
|
23 | export default class CognitoUserPool {
|
24 | /**
|
25 | * Constructs a new CognitoUserPool object
|
26 | * @param {object} data Creation options.
|
27 | * @param {string} data.UserPoolId Cognito user pool id.
|
28 | * @param {string} data.ClientId User pool application client id.
|
29 | * @param {object} data.Storage Optional storage object.
|
30 | * @param {boolean} data.AdvancedSecurityDataCollectionFlag Optional:
|
31 | * boolean flag indicating if the data collection is enabled
|
32 | * to support cognito advanced security features. By default, this
|
33 | * flag is set to true.
|
34 | */
|
35 | constructor(data) {
|
36 | const {
|
37 | UserPoolId,
|
38 | ClientId,
|
39 | endpoint,
|
40 | AdvancedSecurityDataCollectionFlag,
|
41 | } = data || {};
|
42 | if (!UserPoolId || !ClientId) {
|
43 | throw new Error('Both UserPoolId and ClientId are required.');
|
44 | }
|
45 | if (!/^[\w-]+_.+$/.test(UserPoolId)) {
|
46 | throw new Error('Invalid UserPoolId format.');
|
47 | }
|
48 | const region = UserPoolId.split('_')[0];
|
49 |
|
50 | this.userPoolId = UserPoolId;
|
51 | this.clientId = ClientId;
|
52 |
|
53 | this.client = new Client(region, endpoint);
|
54 |
|
55 | /**
|
56 | * By default, AdvancedSecurityDataCollectionFlag is set to true,
|
57 | * if no input value is provided.
|
58 | */
|
59 | this.advancedSecurityDataCollectionFlag =
|
60 | AdvancedSecurityDataCollectionFlag !== false;
|
61 |
|
62 | this.storage = data.Storage || new StorageHelper().getStorage();
|
63 | }
|
64 |
|
65 | /**
|
66 | * @returns {string} the user pool id
|
67 | */
|
68 | getUserPoolId() {
|
69 | return this.userPoolId;
|
70 | }
|
71 |
|
72 | /**
|
73 | * @returns {string} the client id
|
74 | */
|
75 | getClientId() {
|
76 | return this.clientId;
|
77 | }
|
78 |
|
79 | /**
|
80 | * @typedef {object} SignUpResult
|
81 | * @property {CognitoUser} user New user.
|
82 | * @property {bool} userConfirmed If the user is already confirmed.
|
83 | */
|
84 | /**
|
85 | * method for signing up a user
|
86 | * @param {string} username User's username.
|
87 | * @param {string} password Plain-text initial password entered by user.
|
88 | * @param {(AttributeArg[])=} userAttributes New user attributes.
|
89 | * @param {(AttributeArg[])=} validationData Application metadata.
|
90 | * @param {(AttributeArg[])=} clientMetadata Client metadata.
|
91 | * @param {nodeCallback<SignUpResult>} callback Called on error or with the new user.
|
92 | * @returns {void}
|
93 | */
|
94 | signUp(
|
95 | username,
|
96 | password,
|
97 | userAttributes,
|
98 | validationData,
|
99 | callback,
|
100 | clientMetadata
|
101 | ) {
|
102 | const jsonReq = {
|
103 | ClientId: this.clientId,
|
104 | Username: username,
|
105 | Password: password,
|
106 | UserAttributes: userAttributes,
|
107 | ValidationData: validationData,
|
108 | ClientMetadata: clientMetadata,
|
109 | };
|
110 | if (this.getUserContextData(username)) {
|
111 | jsonReq.UserContextData = this.getUserContextData(username);
|
112 | }
|
113 | this.client.request('SignUp', jsonReq, (err, data) => {
|
114 | if (err) {
|
115 | return callback(err, null);
|
116 | }
|
117 |
|
118 | const cognitoUser = {
|
119 | Username: username,
|
120 | Pool: this,
|
121 | Storage: this.storage,
|
122 | };
|
123 |
|
124 | const returnData = {
|
125 | user: new CognitoUser(cognitoUser),
|
126 | userConfirmed: data.UserConfirmed,
|
127 | userSub: data.UserSub,
|
128 | codeDeliveryDetails: data.CodeDeliveryDetails,
|
129 | };
|
130 |
|
131 | return callback(null, returnData);
|
132 | });
|
133 | }
|
134 |
|
135 | /**
|
136 | * method for getting the current user of the application from the local storage
|
137 | *
|
138 | * @returns {CognitoUser} the user retrieved from storage
|
139 | */
|
140 | getCurrentUser() {
|
141 | const lastUserKey = `CognitoIdentityServiceProvider.${this.clientId}.LastAuthUser`;
|
142 |
|
143 | const lastAuthUser = this.storage.getItem(lastUserKey);
|
144 | if (lastAuthUser) {
|
145 | const cognitoUser = {
|
146 | Username: lastAuthUser,
|
147 | Pool: this,
|
148 | Storage: this.storage,
|
149 | };
|
150 |
|
151 | return new CognitoUser(cognitoUser);
|
152 | }
|
153 |
|
154 | return null;
|
155 | }
|
156 |
|
157 | /**
|
158 | * This method returns the encoded data string used for cognito advanced security feature.
|
159 | * This would be generated only when developer has included the JS used for collecting the
|
160 | * data on their client. Please refer to documentation to know more about using AdvancedSecurity
|
161 | * features
|
162 | * @param {string} username the username for the context data
|
163 | * @returns {string} the user context data
|
164 | **/
|
165 | getUserContextData(username) {
|
166 | if (typeof AmazonCognitoAdvancedSecurityData === 'undefined') {
|
167 | return undefined;
|
168 | }
|
169 | /* eslint-disable */
|
170 | const amazonCognitoAdvancedSecurityDataConst = AmazonCognitoAdvancedSecurityData;
|
171 | /* eslint-enable */
|
172 |
|
173 | if (this.advancedSecurityDataCollectionFlag) {
|
174 | const advancedSecurityData = amazonCognitoAdvancedSecurityDataConst.getData(
|
175 | username,
|
176 | this.userPoolId,
|
177 | this.clientId
|
178 | );
|
179 | if (advancedSecurityData) {
|
180 | const userContextData = {
|
181 | EncodedData: advancedSecurityData,
|
182 | };
|
183 | return userContextData;
|
184 | }
|
185 | }
|
186 | return {};
|
187 | }
|
188 | }
|