Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom user agent InternalCognitoUserPool #11716

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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 @@ -6,6 +6,7 @@ describe('import * keys', () => {
Array [
"addAuthCategoryToCognitoUserAgent",
"addFrameworkToCognitoUserAgent",
"InternalCognitoUserPool",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I know the only diff here is arity and exposing the extended interface for internals. Is it worth adding test coverage for the internals surface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I originally added this test as a part of the cognitoUserAgent changes, I was attempting to reflect the other test class on main auth exports. However, it does seem rather redundant and we probably don't need it. I will remove the test.

]
`);
});
Expand Down
16 changes: 2 additions & 14 deletions packages/amazon-cognito-identity-js/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions packages/amazon-cognito-identity-js/internals/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/amazon-cognito-identity-js/src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ export default class Client {
* @param {function} callback Callback called when a response is returned
erinleigh90 marked this conversation as resolved.
Show resolved Hide resolved
* @returns {void}
*/
request(operation, params, callback) {
request(operation, params, callback, userAgentValue) {
const headers = {
'Content-Type': 'application/x-amz-json-1.1',
'X-Amz-Target': `AWSCognitoIdentityProviderService.${operation}`,
'X-Amz-User-Agent': getAmplifyUserAgent(),
'X-Amz-User-Agent': userAgentValue || getAmplifyUserAgent(),
'Cache-Control': 'no-store',
};

Expand Down
167 changes: 10 additions & 157 deletions packages/amazon-cognito-identity-js/src/CognitoUserPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

import Client from './Client';
import CognitoUser from './CognitoUser';
import StorageHelper from './StorageHelper';
import { InternalCognitoUserPool } from './internals';

const USER_POOL_ID_MAX_LENGTH = 55;

/** @class */
export default class CognitoUserPool {
/**
* 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 {string} data.endpoint Optional custom service endpoint.
* @param {object} data.fetchOptions Optional options for fetch API.
* (only credentials option is supported)
* @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.
*/
constructor(data, wrapRefreshSessionCallback) {
const {
UserPoolId,
ClientId,
endpoint,
fetchOptions,
AdvancedSecurityDataCollectionFlag,
} = data || {};
if (!UserPoolId || !ClientId) {
throw new Error('Both UserPoolId and ClientId are required.');
}
if (UserPoolId.length > USER_POOL_ID_MAX_LENGTH || !/^[\w-]+_[0-9a-zA-Z]+$/.test(UserPoolId)) {
throw new Error('Invalid UserPoolId format.');
}
const region = UserPoolId.split('_')[0];

this.userPoolId = UserPoolId;
this.clientId = ClientId;

this.client = new Client(region, endpoint, fetchOptions);

/**
* By default, AdvancedSecurityDataCollectionFlag is set to true,
* if no input value is provided.
*/
this.advancedSecurityDataCollectionFlag =
AdvancedSecurityDataCollectionFlag !== false;

this.storage = data.Storage || new StorageHelper().getStorage();

if (wrapRefreshSessionCallback) {
this.wrapRefreshSessionCallback = wrapRefreshSessionCallback;
}
}

/**
* @returns {string} the user pool id
*/
getUserPoolId() {
return this.userPoolId;
}

/**
* @returns {string} the user pool name
*/
getUserPoolName() {
return this.getUserPoolId().split('_')[1];
}

/**
* @returns {string} the client id
*/
getClientId() {
return this.clientId;
}
export default class CognitoUserPool extends InternalCognitoUserPool {

/**
* @typedef {object} SignUpResult
Expand All @@ -105,90 +35,13 @@ export default class CognitoUserPool {
callback,
clientMetadata
) {
const jsonReq = {
ClientId: this.clientId,
Username: username,
Password: password,
UserAttributes: userAttributes,
ValidationData: validationData,
ClientMetadata: clientMetadata,
};
if (this.getUserContextData(username)) {
jsonReq.UserContextData = this.getUserContextData(username);
}
this.client.request('SignUp', jsonReq, (err, data) => {
if (err) {
return callback(err, null);
}

const cognitoUser = {
Username: username,
Pool: this,
Storage: this.storage,
};

const returnData = {
user: new CognitoUser(cognitoUser),
userConfirmed: data.UserConfirmed,
userSub: data.UserSub,
codeDeliveryDetails: data.CodeDeliveryDetails,
};

return callback(null, returnData);
});
}

/**
* method for getting the current user of the application from the local storage
*
* @returns {CognitoUser} the user retrieved from storage
*/
getCurrentUser() {
const lastUserKey = `CognitoIdentityServiceProvider.${this.clientId}.LastAuthUser`;

const lastAuthUser = this.storage.getItem(lastUserKey);
if (lastAuthUser) {
const 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
**/
getUserContextData(username) {
if (typeof AmazonCognitoAdvancedSecurityData === 'undefined') {
return undefined;
}
/* eslint-disable */
const amazonCognitoAdvancedSecurityDataConst = AmazonCognitoAdvancedSecurityData;
/* eslint-enable */

if (this.advancedSecurityDataCollectionFlag) {
const advancedSecurityData = amazonCognitoAdvancedSecurityDataConst.getData(
username,
this.userPoolId,
this.clientId
);
if (advancedSecurityData) {
const userContextData = {
EncodedData: advancedSecurityData,
};
return userContextData;
}
}
return {};
return super.signUp(
username,
password,
userAttributes,
validationData,
callback,
clientMetadata
);
}
}
Loading
Loading