UNPKG

@sungly/cognito-cli

Version:

CLI for all Cognito user life cycle management

714 lines (587 loc) 26 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.userService = undefined; var _regenerator = require('babel-runtime/regenerator'); var _regenerator2 = _interopRequireDefault(_regenerator); var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator'); var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _cognitoClient = require('../util/cognitoClient'); var _config = require('../config'); var _config2 = _interopRequireDefault(_config); var _hash = require('../util/hash'); var _util = require('../util'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var UserService = function () { function UserService(_ref) { var userPoolId = _ref.userPoolId, clientId = _ref.clientId, clientSecret = _ref.clientSecret; (0, _classCallCheck3.default)(this, UserService); this.userPoolId = userPoolId; this.clientId = clientId; this.clientSecret = clientSecret; } (0, _createClass3.default)(UserService, [{ key: '_addSecretHash', value: function _addSecretHash(_ref2) { var username = _ref2.username, options = _ref2.options; if (this.clientSecret) { options.SecretHash = (0, _hash.hash)({ username: username, clientId: this.clientId, clientSecret: this.clientSecret }); } return options; } /** * Create a user profile * * @param {*} param0 */ }, { key: 'createUser', value: function () { var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(_ref3) { var username = _ref3.username, password = _ref3.password, attributes = _ref3.attributes; var params, res; return _regenerator2.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: params = this._addSecretHash({ username: username, options: { ClientId: this.clientId, Password: password, Username: username, UserAttributes: attributes } }); _context.prev = 1; _context.next = 4; return _cognitoClient.cognitoClient.signUp(params).promise(); case 4: res = _context.sent; _util.logger.info(res); _context.next = 11; break; case 8: _context.prev = 8; _context.t0 = _context['catch'](1); _util.logger.error(_context.t0.message); case 11: case 'end': return _context.stop(); } } }, _callee, this, [[1, 8]]); })); function createUser(_x) { return _ref4.apply(this, arguments); } return createUser; }() /** * Confirm user signup * * @param {*} param0 */ }, { key: 'confirmSignUp', value: function () { var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(_ref5) { var username = _ref5.username, code = _ref5.code; var params, res; return _regenerator2.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: params = this._addSecretHash({ username: username, options: { ClientId: this.clientId, Username: username, ConfirmationCode: code } }); _context2.prev = 1; _context2.next = 4; return _cognitoClient.cognitoClient.confirmSignUp(params).promise(); case 4: res = _context2.sent; _util.logger.info(res); _context2.next = 11; break; case 8: _context2.prev = 8; _context2.t0 = _context2['catch'](1); _util.logger.error(_context2.t0.message); case 11: case 'end': return _context2.stop(); } } }, _callee2, this, [[1, 8]]); })); function confirmSignUp(_x2) { return _ref6.apply(this, arguments); } return confirmSignUp; }() /** * Send user reset password email * @param {*} param0 */ }, { key: 'forgotPassword', value: function () { var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(_ref7) { var username = _ref7.username; var params, res; return _regenerator2.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: params = this._addSecretHash({ username: username, options: { ClientId: this.clientId, Username: username } }); _context3.prev = 1; _context3.next = 4; return _cognitoClient.cognitoClient.forgotPassword(params).promise(); case 4: res = _context3.sent; _util.logger.info(res); _context3.next = 11; break; case 8: _context3.prev = 8; _context3.t0 = _context3['catch'](1); _util.logger.error(_context3.t0.message); case 11: case 'end': return _context3.stop(); } } }, _callee3, this, [[1, 8]]); })); function forgotPassword(_x3) { return _ref8.apply(this, arguments); } return forgotPassword; }() /** * Resend activation/forgot password email to user * * @param {*} param0 */ }, { key: 'resendConfirmationCode', value: function () { var _ref10 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(_ref9) { var username = _ref9.username; var params; return _regenerator2.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: params = this._addSecretHash({ username: username, options: { ClientId: this.clientId, Username: username } }); _context4.prev = 1; _context4.next = 4; return _cognitoClient.cognitoClient.resendConfirmationCode(params).promise(); case 4: return _context4.abrupt('return', _context4.sent); case 7: _context4.prev = 7; _context4.t0 = _context4['catch'](1); _util.logger.error(_context4.t0.message); case 10: case 'end': return _context4.stop(); } } }, _callee4, this, [[1, 7]]); })); function resendConfirmationCode(_x4) { return _ref10.apply(this, arguments); } return resendConfirmationCode; }() /** * Set a new password for the user * * * Requires admin permission * * @param {*} param0 */ }, { key: 'setUserPassword', value: function () { var _ref12 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(_ref11) { var username = _ref11.username, newPassword = _ref11.newPassword; var params, res; return _regenerator2.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: params = { Password: newPassword, UserPoolId: this.userPoolId, Username: username, Permanent: true }; _context5.prev = 1; _context5.next = 4; return _cognitoClient.cognitoClient.adminSetUserPassword(params).promise(); case 4: res = _context5.sent; _util.logger.info(res); _context5.next = 11; break; case 8: _context5.prev = 8; _context5.t0 = _context5['catch'](1); _util.logger.error(_context5.t0.message); case 11: case 'end': return _context5.stop(); } } }, _callee5, this, [[1, 8]]); })); function setUserPassword(_x5) { return _ref12.apply(this, arguments); } return setUserPassword; }() /** * Get user profile from user pool * * @param {*} param0 */ }, { key: 'getUserProfile', value: function () { var _ref14 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(_ref13) { var username = _ref13.username; var params, res; return _regenerator2.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: params = { UserPoolId: this.userPoolId, Username: username }; _context6.prev = 1; _context6.next = 4; return _cognitoClient.cognitoClient.adminGetUser(params).promise(); case 4: res = _context6.sent; _util.logger.info(res); _context6.next = 11; break; case 8: _context6.prev = 8; _context6.t0 = _context6['catch'](1); _util.logger.error(_context6.t0.message); case 11: case 'end': return _context6.stop(); } } }, _callee6, this, [[1, 8]]); })); function getUserProfile(_x6) { return _ref14.apply(this, arguments); } return getUserProfile; }() /** * Auto verify user email * * @param {*} param0 */ }, { key: 'verifyUserEmail', value: function () { var _ref16 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(_ref15) { var username = _ref15.username; var params, res; return _regenerator2.default.wrap(function _callee7$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: params = { UserAttributes: [{ Name: 'email_verified', Value: 'true' }], UserPoolId: this.userPoolId, Username: username }; _context7.prev = 1; _context7.next = 4; return _cognitoClient.cognitoClient.adminUpdateUserAttributes(params).promise(); case 4: res = _context7.sent; _util.logger.info(res); _context7.next = 11; break; case 8: _context7.prev = 8; _context7.t0 = _context7['catch'](1); _util.logger.error(_context7.t0.message); case 11: case 'end': return _context7.stop(); } } }, _callee7, this, [[1, 8]]); })); function verifyUserEmail(_x7) { return _ref16.apply(this, arguments); } return verifyUserEmail; }() /** * Disable user from logging in * * @param {*} param0 */ }, { key: 'disableUser', value: function () { var _ref18 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8(_ref17) { var username = _ref17.username; var params, res; return _regenerator2.default.wrap(function _callee8$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: params = { UserPoolId: this.userPoolId, Username: username }; _context8.prev = 1; _context8.next = 4; return _cognitoClient.cognitoClient.adminDisableUser(params).promise(); case 4: res = _context8.sent; if (!res) _util.logger.info('User is disabled.');else _util.logger.info(res); _context8.next = 11; break; case 8: _context8.prev = 8; _context8.t0 = _context8['catch'](1); _util.logger.error(_context8.t0.message); case 11: case 'end': return _context8.stop(); } } }, _callee8, this, [[1, 8]]); })); function disableUser(_x8) { return _ref18.apply(this, arguments); } return disableUser; }() /** * Enable user * * @param {*} param0 */ }, { key: 'enableUser', value: function () { var _ref20 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9(_ref19) { var username = _ref19.username; var params, res; return _regenerator2.default.wrap(function _callee9$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: params = { UserPoolId: this.userPoolId, Username: username }; _context9.prev = 1; _context9.next = 4; return _cognitoClient.cognitoClient.adminEnableUser(params).promise(); case 4: res = _context9.sent; if (!res) _util.logger.info('User is enabled.');else _util.logger.info(res); _context9.next = 11; break; case 8: _context9.prev = 8; _context9.t0 = _context9['catch'](1); _util.logger.error(_context9.t0.message); case 11: case 'end': return _context9.stop(); } } }, _callee9, this, [[1, 8]]); })); function enableUser(_x9) { return _ref20.apply(this, arguments); } return enableUser; }() /** * Update user attribute * * @param {*} param0 */ }, { key: 'updateUserAttribute', value: function () { var _ref22 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee10(_ref21) { var username = _ref21.username, attributeName = _ref21.attributeName, attributeValue = _ref21.attributeValue; var params; return _regenerator2.default.wrap(function _callee10$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: params = { UserAttributes: [{ Name: attributeName, Value: attributeValue }], UserPoolId: this.userPoolId, Username: username }; _context10.prev = 1; _context10.next = 4; return _cognitoClient.cognitoClient.adminUpdateUserAttributes(params).promise(); case 4: return _context10.abrupt('return', _context10.sent); case 7: _context10.prev = 7; _context10.t0 = _context10['catch'](1); _util.logger.error(_context10.t0.message); case 10: case 'end': return _context10.stop(); } } }, _callee10, this, [[1, 7]]); })); function updateUserAttribute(_x10) { return _ref22.apply(this, arguments); } return updateUserAttribute; }() /** * List users * * @param token * @returns {Promise<*>} */ }, { key: 'listUsers', value: function () { var _ref23 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee11(token) { var params; return _regenerator2.default.wrap(function _callee11$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: params = { UserPoolId: this.userPoolId, PaginationToken: token }; _context11.prev = 1; _context11.next = 4; return _cognitoClient.cognitoClient.listUsers(params).promise(); case 4: return _context11.abrupt('return', _context11.sent); case 7: _context11.prev = 7; _context11.t0 = _context11['catch'](1); _util.logger.error(_context11.t0.message); case 10: case 'end': return _context11.stop(); } } }, _callee11, this, [[1, 7]]); })); function listUsers(_x11) { return _ref23.apply(this, arguments); } return listUsers; }() /** * Delete Users * * @param username * @returns {Promise<*>} */ }, { key: 'deleteUsers', value: function () { var _ref24 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee12(username) { var params; return _regenerator2.default.wrap(function _callee12$(_context12) { while (1) { switch (_context12.prev = _context12.next) { case 0: params = { UserPoolId: this.userPoolId, Username: username }; _context12.prev = 1; _context12.next = 4; return _cognitoClient.cognitoClient.adminDeleteUser(params).promise(); case 4: return _context12.abrupt('return', _context12.sent); case 7: _context12.prev = 7; _context12.t0 = _context12['catch'](1); _util.logger.error(_context12.t0.message); case 10: case 'end': return _context12.stop(); } } }, _callee12, this, [[1, 7]]); })); function deleteUsers(_x12) { return _ref24.apply(this, arguments); } return deleteUsers; }() }]); return UserService; }(); var userService = exports.userService = new UserService({ userPoolId: _config2.default.userPoolId, clientId: _config2.default.clientId, clientSecret: _config2.default.clientSecret });