UNPKG

@quinck/aws-cognito-client

Version:

Provides a user attributes generic cognito client.

263 lines (262 loc) 9.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CognitoAdminService = void 0; require("@quinck/collections"); const user_1 = require("../models/utils/user"); const errors_1 = require("../utils/errors"); const basic_cognito_service_1 = require("./basic-cognito-service"); const COGNITO_LIST_LIMIT = 60; class CognitoAdminService extends basic_cognito_service_1.BasicCognitoService { async confirmSignUp(username) { await this.tryDo(async () => { await this.cognitoIdentityProvider.adminConfirmSignUp({ UserPoolId: this.userPoolId, Username: username, }); }); } async setUserPassword(username, password) { await this.tryDo(async () => { await this.cognitoIdentityProvider.adminSetUserPassword({ UserPoolId: this.userPoolId, Username: username, Password: password, Permanent: true, }); }); } async forceEmailVerification(username) { await this.forceAttributeVerification(username, 'email_verified'); } async forcePhoneNumberVerification(username) { await this.forceAttributeVerification(username, 'phone_number_verified'); } async createUser(params) { return this.tryDo(async () => { const { credentials, postSignupMessageConfig } = params; const { username, password } = credentials; const attributes = this.createUserAttributes(params); const { User } = await this.cognitoIdentityProvider.adminCreateUser({ UserPoolId: this.userPoolId, Username: username, TemporaryPassword: password, UserAttributes: attributes, DesiredDeliveryMediums: postSignupMessageConfig?.deliveryMediums, MessageAction: postSignupMessageConfig?.action, }); if (User?.Attributes && User.Username) { return this.parseUser(User); } throw new errors_1.UnknownInternalError(); }); } async updateUserPassword(username, password, permanent) { return this.tryDo(async () => { await this.cognitoIdentityProvider.adminSetUserPassword({ Username: username, Password: password, UserPoolId: this.userPoolId, Permanent: permanent, }); }); } async addUserToGroup(username, ...groups) { await this.tryDo(async () => { for (const group of groups) { await this.cognitoIdentityProvider.adminAddUserToGroup({ GroupName: group, UserPoolId: this.userPoolId, Username: username, }); } }); } async removeUserFromGroup(username, ...groups) { for (const group of groups) { await this.cognitoIdentityProvider.adminRemoveUserFromGroup({ GroupName: group, UserPoolId: this.userPoolId, Username: username, }); } } async searchUsers() { return this.tryDo(async () => { const users = await this.getAllUsersAllPages(); return this.parseUsersSearchResult(users); }); } searchUsersInGroup(group) { return this.tryDo(async () => { const users = await this.getAllUsersByGroupAllPages(group); return this.parseUsersSearchResult(users); }); } async getAllUsers() { return await this.searchUsers(); } async getUserByEmail(email) { const { Users } = await this.tryDo(() => this.cognitoIdentityProvider.listUsers({ UserPoolId: this.userPoolId, Filter: `email = "${email}"`, })); if (!Users || Users.length <= 0) throw new errors_1.UserNotFoundError(); const [user] = Users; if (user.Attributes && user.Username) { return this.parseUser(user); } throw new errors_1.UserNotFoundError(); } async getAllUsersAllPages(currentUsers = [], paginationToken) { const { PaginationToken, Users } = await this.cognitoIdentityProvider.listUsers({ UserPoolId: this.userPoolId, PaginationToken: paginationToken, Limit: COGNITO_LIST_LIMIT, }); const users = currentUsers.concat(Users ?? []); if (!PaginationToken) return users; else return this.getAllUsersAllPages(users, PaginationToken); } async getAllUsersByGroupAllPages(groupName, currentUsers = [], paginationToken) { const { NextToken, Users } = await this.cognitoIdentityProvider.listUsersInGroup({ UserPoolId: this.userPoolId, GroupName: groupName, NextToken: paginationToken, Limit: COGNITO_LIST_LIMIT, }); const users = currentUsers.concat(Users ?? []); if (!NextToken) return users; else return this.getAllUsersByGroupAllPages(groupName, users, NextToken); } async parseUsersSearchResult(usersFound) { if (usersFound) { const users = usersFound.singleCollect(user => !!user.Attributes && !!user.Username, user => this.parseUser(user)); return Promise.all(users); } return []; } parseUser(user) { const { Attributes, Username, UserCreateDate, UserLastModifiedDate, UserStatus, Enabled, } = user; const attributes = this.parseUserAttributes(Attributes); return { ...this.createUserInfo(Username, attributes), additionaInformation: { isEnabled: Enabled, createdDate: UserCreateDate, lastModifiedDate: UserLastModifiedDate, status: this.mapStatus(UserStatus), }, }; } async getUserGroups(username) { try { const { Groups } = await this.cognitoIdentityProvider.adminListGroupsForUser({ Username: username, UserPoolId: this.userPoolId, }); if (Groups) return Groups.singleCollect(x => x.GroupName != undefined, x => x.GroupName); return []; } catch { return []; } } async getUser(username) { try { const { Enabled, UserAttributes, UserCreateDate, UserLastModifiedDate, UserStatus, Username, } = await this.getUserByUsername(username); if (UserAttributes && Username) { return this.parseUser({ Username, Attributes: UserAttributes, Enabled, UserCreateDate, UserLastModifiedDate, UserStatus, }); } throw new errors_1.UserNotRetrievedError(); } catch (error) { throw this.createError(error); } } getUserByUsername(Username) { return this.cognitoIdentityProvider.adminGetUser({ UserPoolId: this.userPoolId, Username, }); } mapStatus(status) { switch (status) { case 'UNCONFIRMED': return user_1.UserStatus.UNCONFIRMED; case 'CONFIRMED': return user_1.UserStatus.CONFIRMED; case 'ARCHIVED': return user_1.UserStatus.ARCHIVED; case 'COMPROMISED': return user_1.UserStatus.COMPROMISED; case 'RESET_REQUIRED': return user_1.UserStatus.RESET_REQUIRED; case 'FORCE_CHANGE_PASSWORD': return user_1.UserStatus.FORCE_CHANGE_PASSWORD; case 'UNKNOWN': default: return user_1.UserStatus.UNKNOWN; } } async updateUser(username, user) { await this.tryDo(() => this.cognitoIdentityProvider.adminUpdateUserAttributes({ UserPoolId: this.userPoolId, Username: username, UserAttributes: this.createAttributesFromObject(this.fitUserUpdateInfo(user), false), })); } async deleteUser(username) { await this.tryDo(() => this.cognitoIdentityProvider.adminDeleteUser({ UserPoolId: this.userPoolId, Username: username, })); } async disableUser(username) { await this.tryDo(() => this.cognitoIdentityProvider.adminDisableUser({ UserPoolId: this.userPoolId, Username: username, })); } async enableUser(username) { await this.tryDo(() => this.cognitoIdentityProvider.adminEnableUser({ UserPoolId: this.userPoolId, Username: username, })); } async forceAttributeVerification(username, attribute) { await this.cognitoIdentityProvider.adminUpdateUserAttributes({ Username: username, UserPoolId: this.userPoolId, UserAttributes: [this.verifiedAttribute(attribute)], }); } verifiedAttribute(attribute) { return { Name: attribute, Value: 'true', }; } createUserAttributes(params) { const { userAttributes, forceEmailVerification, forcePhoneNumberVerification, } = params; const attributes = this.createAttributesFromObject(this.fitSignUpInfo(userAttributes), false); if (forceEmailVerification === true) attributes.push(this.verifiedAttribute('email_verified')); if (forcePhoneNumberVerification === true) attributes.push(this.verifiedAttribute('phone_number_verified')); return attributes; } } exports.CognitoAdminService = CognitoAdminService;