From e2a004a19fc748e7c84624bc62d4490f6dcc1f9e Mon Sep 17 00:00:00 2001 From: Ly Sung Date: Fri, 1 May 2020 10:10:06 -0400 Subject: [PATCH] feat(user-cmds): Added more user service commands (#5) * added more user service commands * updated authors * moved user cmds into a class --- README.md | 3 +- package.json | 2 +- src/cmds/index.js | 56 +++++++++- src/cmds/user/create.js | 41 -------- src/cmds/user/index.js | 205 +++++++++++++++++++++++++++++++++++++ src/util/CognitoService.js | 91 ++++++++-------- 6 files changed, 310 insertions(+), 88 deletions(-) delete mode 100644 src/cmds/user/create.js create mode 100644 src/cmds/user/index.js diff --git a/README.md b/README.md index 5c76092..6038d76 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ Running npm link in the root of your project will symlink your binary file to th ### Commands -node bin/index.js version - return the current status of package.json -node bin/index.js help - TODO +npm run cognito ### Add config to ~/.cognito diff --git a/package.json b/package.json index af297d2..8c07854 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "bin": { "cognito": "bin/index" }, - "author": "Ly Sung", + "author": "Ly Sung, Rocella Jimenez", "license": "ISC", "bugs": { "url": "https://github.com/sungly/cognito-cli/issues" diff --git a/src/cmds/index.js b/src/cmds/index.js index 6f02bf8..9ae45d1 100644 --- a/src/cmds/index.js +++ b/src/cmds/index.js @@ -2,7 +2,9 @@ import minimist from 'minimist'; import init from './init'; import login from './login'; -import createUser from './user/create'; +import UserCmd from './user'; + +const userCmd = new UserCmd(); const cli = () => { const args = minimist(process.argv.slice(2)); @@ -20,11 +22,59 @@ const cli = () => { console.log('help todo'); break; case 'login': - console.log('@NOTE: Only user password auth is supported atm'); + console.log('@NOTE: Only user password auth is supported ATM.'); login(); break; case 'create-user': - createUser(); + console.log('Creating user'); + + userCmd.createUser(); + break; + case 'confirm-user': + console.log('Confirming user registration with confirmation code'); + + userCmd.confirmUserSignup(); + break; + case 'resend-confirmation-code': + console.log('Resending registration confirmation code'); + + userCmd.resendConfirmationCode(); + break; + case 'forgot-password': + console.log('Send forgot password'); + + userCmd.forgotPassword(); + break; + case 'set-user-password': + console.log('Setting user password'); + console.log('*** Requires admin permission ***'); + + userCmd.setUserPassword(); + break; + + case 'get-user-profile': + console.log('Getting user profile'); + console.log('*** Requires admin permission ***'); + + userCmd.getUserProfile(); + break; + case 'verify-user-email': + console.log('Verifying user email'); + console.log('*** Requires admin permission ***'); + + userCmd.verifyUserEmail(); + break; + case 'disable-user': + console.log('Disabling User'); + console.log('*** Requires admin permission ***'); + + userCmd.disableUser(); + break; + case 'enable-user': + console.log('Enabling User'); + console.log('*** Requires admin permission ***'); + + userCmd.enableUser(); break; default: console.error(`"${cmd}" is not a valid command!`); diff --git a/src/cmds/user/create.js b/src/cmds/user/create.js deleted file mode 100644 index e5846fa..0000000 --- a/src/cmds/user/create.js +++ /dev/null @@ -1,41 +0,0 @@ -import prompt from 'prompt'; - -import config from '../../config'; -import util from '../../util'; - -function createUser() { - config.requiredAttributeList.push('password'); - - const requiredAttributes = config.requiredAttributeList.map(attr => { - return { - name: attr, - required: true, - }; - }); - - prompt.get(requiredAttributes, async (err, result) => { - console.log('Creating user...'); - - let username = result.username ? result.username : result.email; - const password = result.password; - - delete result.password; - - const attributes = Object.keys(result).map(key => { - return { - Name: key, - Value: result[key], - }; - }); - - const res = await util.cognitoService.createUser({ - username, - password, - attributes, - }); - - console.log(res); - }); -} - -export default createUser; diff --git a/src/cmds/user/index.js b/src/cmds/user/index.js new file mode 100644 index 0000000..ce47b37 --- /dev/null +++ b/src/cmds/user/index.js @@ -0,0 +1,205 @@ +import prompt from 'prompt'; + +import config from '../../config'; +import util from '../../util'; + +class UserCmd { + createUser() { + config.requiredAttributeList.push('password'); + + const requiredAttributes = config.requiredAttributeList.map((attr) => { + return { + name: attr, + required: true, + }; + }); + + prompt.get(requiredAttributes, async (err, result) => { + let username = result.username ? result.username : result.email; + const password = result.password; + + delete result.password; + + const attributes = Object.keys(result).map((key) => { + return { + Name: key, + Value: result[key], + }; + }); + + const res = await util.cognitoService.createUser({ + username, + password, + attributes, + }); + + console.log(res); + }); + } + + confirmUserSignup() { + const requiredAttributes = [ + { + name: 'username', + required: true, + }, + { + name: 'code', + required: true, + }, + ]; + + prompt.get(requiredAttributes, async (err, result) => { + const { username, code } = result; + + const res = await util.cognitoService.confirmSignUp({ + username, + code, + }); + + console.log(res); + }); + } + + resendConfirmationCode() { + const requiredAttributes = [ + { + name: 'username', + required: true, + }, + ]; + + prompt.get(requiredAttributes, async (err, result) => { + const { username } = result; + + const res = await util.cognitoService.resendConfirmationCode({ + username, + }); + + console.log(res); + }); + } + + forgotPassword() { + const requiredAttributes = [ + { + name: 'username', + required: true, + }, + ]; + + prompt.get(requiredAttributes, async (err, result) => { + const { username } = result; + + const res = await util.cognitoService.forgotPassword({ + username, + }); + + console.log(res); + }); + } + + setUserPassword() { + const requiredAttributes = [ + { + name: 'username', + required: true, + }, + { + name: 'password', + required: true, + }, + ]; + + prompt.get(requiredAttributes, async (err, result) => { + const { username, password } = result; + + const res = await util.cognitoService.setUserPassword({ + username, + newPassword: password, + }); + + console.log(res); + }); + } + + getUserProfile() { + const requiredAttributes = [ + { + name: 'username', + required: true, + }, + ]; + + prompt.get(requiredAttributes, async (err, result) => { + const { username } = result; + + const res = await util.cognitoService.getUserProfile({ + username, + }); + + console.log(res); + }); + } + + verifyUserEmail() { + const requiredAttributes = [ + { + name: 'email', + required: true, + }, + ]; + + prompt.get(requiredAttributes, async (err, result) => { + const { email } = result; + + const res = await util.cognitoService.verifyUserEmail({ + username: email, + }); + + console.log(res); + }); + } + + disableUser() { + const requiredAttributes = [ + { + name: 'username', + required: true, + }, + ]; + + prompt.get(requiredAttributes, async (err, result) => { + const { username } = result; + + const res = await util.cognitoService.disableUser({ + username, + }); + + if (!res) console.log(`User is disabled.`); + else console.log(res); + }); + } + + enableUser() { + const requiredAttributes = [ + { + name: 'username', + required: true, + }, + ]; + + prompt.get(requiredAttributes, async (err, result) => { + const { username } = result; + + const res = await util.cognitoService.enableUser({ + username, + }); + + if (!res) console.log(`User is enabled.`); + else console.log(res); + }); + } +} + +export default UserCmd; diff --git a/src/util/CognitoService.js b/src/util/CognitoService.js index 66a2b85..6c7d17f 100644 --- a/src/util/CognitoService.js +++ b/src/util/CognitoService.js @@ -9,16 +9,16 @@ class UserService { this.clientSecret = clientSecret; } - _addSecretHash({ username, params }) { + _addSecretHash({ username, options }) { if (this.clientSecret) { - params.SecretHash = hash({ + options.SecretHash = hash({ username, clientId: this.clientId, clientSecret: this.clientSecret, }); } - return params; + return options; } /** @@ -27,12 +27,15 @@ class UserService { * @param {*} param0 */ async createUser({ username, password, attributes }) { - const params = { - ClientId: this.clientId, - Password: password, - Username: username, - UserAttributes: attributes, - }; + const params = this._addSecretHash({ + username, + options: { + ClientId: this.clientId, + Password: password, + Username: username, + UserAttributes: attributes, + }, + }); return cognitoClient.signUp(params).promise(); } @@ -42,13 +45,17 @@ class UserService { * * @param {*} param0 */ - async confirmSignUp({ username }) { - const params = { - UserPoolId: this.userPoolId, - Username: username, - }; + async confirmSignUp({ username, code }) { + const params = this._addSecretHash({ + username, + options: { + ClientId: this.clientId, + Username: username, + ConfirmationCode: code, + }, + }); - return cognitoClient.adminConfirmSignUp(params).promise(); + return cognitoClient.confirmSignUp(params).promise(); } /** @@ -56,24 +63,39 @@ class UserService { * @param {*} param0 */ async forgotPassword({ username }) { - let params = { - ClientId: this.clientId, - Username: username, - }; - - params = _addSecretHash({ + const params = this._addSecretHash({ username, - clientId: this.clientId, - clientSecret: this.clientSecret, - params, + options: { + ClientId: this.clientId, + Username: username, + }, }); return cognitoClient.forgotPassword(params).promise(); } + /** + * Resend activation/forgot password email to user + * + * @param {*} param0 + */ + async resendConfirmationCode({ username }) { + const params = this._addSecretHash({ + username, + options: { + ClientId: this.clientId, + Username: username, + }, + }); + + return cognitoClient.resendConfirmationCode(params).promise(); + } + /** * Set a new password for the user * + * * Requires admin permission + * * @param {*} param0 */ async setUserPassword({ username, newPassword }) { @@ -86,21 +108,6 @@ class UserService { return cognitoClient.adminSetUserPassword(params).promise(); } - /** - * Resend activation/forgot password email to user - * - * @param {*} param0 - */ - async resendConfirmationCode({ username }) { - let params = { - ClientId: this.clientId, - Username: username, - }; - - params = this._addSecretHash({ username, params }); - return cognitoClient.resendConfirmationCode(params).promise(); - } - /** * Get user profile from user pool * @@ -124,14 +131,16 @@ class UserService { const params = { UserAttributes: [ { - Name: 'email', - Value: true, + Name: 'email_verified', + Value: 'true', }, ], UserPoolId: this.userPoolId, Username: username, }; + console.log(params); + return cognitoClient.adminUpdateUserAttributes(params).promise(); }