UNPKG

@cn-shell/aws-utils

Version:
166 lines (165 loc) 4.21 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; }, }); } : function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; }); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); } : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function(mod) { return mod && mod.__esModule ? mod : { default: mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CognitoUserPool = void 0; // imports here const Aws = __importStar(require("./aws-base")); const cognitoidentityserviceprovider_1 = __importDefault( require("aws-sdk/clients/cognitoidentityserviceprovider"), ); // Class CognitoUserPool here class CognitoUserPool extends Aws.Base { // Constructor here constructor(name, opts) { super(name, opts); this._poolId = opts.poolId; this._userPool = new cognitoidentityserviceprovider_1.default({ region: this._region, apiVersion: "2016-04-18", }); } // Public and Private methods here async start() { return true; } async stop() {} async healthCheck() { return true; } async getUsers(paginationToken) { let res = await this._userPool .listUsers({ UserPoolId: this._poolId, PaginationToken: paginationToken, }) .promise() .catch(e => { this.error( "getUsers - PoolId (%s) Error: (%s: %s)", this._poolId, e.code, e, ); }); if (res === undefined || res.Users === undefined) { return []; } let users = res.Users; // If there are more results to get then go get them if (res.PaginationToken !== undefined) { let others = await this.getUsers(res.PaginationToken); users = [...users, ...others]; } return users; } async createUser(userName, userAtribs) { let attribs = userAtribs.map(el => { return { Name: el.attribute, Value: el.value }; }); let res = await this._userPool .adminCreateUser({ UserPoolId: this._poolId, Username: userName, UserAttributes: attribs, ForceAliasCreation: true, DesiredDeliveryMediums: ["EMAIL"], }) .promise() .catch(e => { this.error( "createUser - PoolId: (%s) Error: (%s: %s)", this._poolId, e.code, e, ); }); if (res === undefined) { return false; } return true; } async deleteUser(userName) { let res = await this._userPool .adminDeleteUser({ UserPoolId: this._poolId, Username: userName, }) .promise() .catch(e => { this.error( "deleteUser - PoolId: (%s) Error: (%s: %s)", this._poolId, e.code, e, ); }); if (res === undefined) { return false; } return true; } async changeUserPassword(userName, password) { let res = await this._userPool .adminSetUserPassword({ UserPoolId: this._poolId, Username: userName, Password: password, Permanent: true, }) .promise() .catch(e => { this.error( "changeUserPassword - PoolId: (%s) Error: (%s: %s)", this._poolId, e.code, e, ); }); if (res === undefined) { return false; } return true; } } exports.CognitoUserPool = CognitoUserPool;