|
| 1 | +const crypto = require('crypto'); |
| 2 | +const inquirer = require('inquirer'); |
| 3 | +const OTPAuth = require('otpauth'); |
| 4 | +const { copy } = require('./utils.js'); |
| 5 | +const phrases = { |
| 6 | + enterPassword: 'Enter a password:', |
| 7 | + enterUsername: 'Enter a username:', |
| 8 | + enterAppName: 'Enter the app name:', |
| 9 | +} |
| 10 | +const getAlgorithm = async () => { |
| 11 | + let { algorithm } = await inquirer.prompt([ |
| 12 | + { |
| 13 | + type: 'list', |
| 14 | + name: 'algorithm', |
| 15 | + message: 'Which hashing algorithm do you want to use?', |
| 16 | + default: 'SHA1', |
| 17 | + choices: [ |
| 18 | + 'SHA1', |
| 19 | + 'SHA224', |
| 20 | + 'SHA256', |
| 21 | + 'SHA384', |
| 22 | + 'SHA512', |
| 23 | + 'SHA3-224', |
| 24 | + 'SHA3-256', |
| 25 | + 'SHA3-384', |
| 26 | + 'SHA3-512', |
| 27 | + 'Other' |
| 28 | + ] |
| 29 | + } |
| 30 | + ]); |
| 31 | + if (algorithm === 'Other') { |
| 32 | + const result = await inquirer.prompt([ |
| 33 | + { |
| 34 | + type: 'input', |
| 35 | + name: 'algorithm', |
| 36 | + message: 'Enter the hashing algorithm you want to use:' |
| 37 | + } |
| 38 | + ]); |
| 39 | + algorithm = result.algorithm; |
| 40 | + } |
| 41 | + const { digits, period } = await inquirer.prompt([ |
| 42 | + { |
| 43 | + type: 'number', |
| 44 | + name: 'digits', |
| 45 | + default: 6, |
| 46 | + message: 'Enter the number of digits the one-time password should have:' |
| 47 | + }, |
| 48 | + { |
| 49 | + type: 'number', |
| 50 | + name: 'period', |
| 51 | + default: 30, |
| 52 | + message: 'Enter how long the one-time password should be valid (in seconds):' |
| 53 | + } |
| 54 | + ]) |
| 55 | + return { algorithm, digits, period}; |
| 56 | +}; |
| 57 | +const generateSecret = ({ app, username, algorithm, digits, period }) => { |
| 58 | + const secret = new OTPAuth.Secret(); |
| 59 | + const totp = new OTPAuth.TOTP({ |
| 60 | + issuer: app, |
| 61 | + label: username, |
| 62 | + algorithm, |
| 63 | + digits, |
| 64 | + period, |
| 65 | + secret |
| 66 | + }); |
| 67 | + const url = totp.toString(); |
| 68 | + return { secret: secret.base32, url }; |
| 69 | +}; |
| 70 | +const showQR = text => { |
| 71 | + const QRCode = require('qrcode'); |
| 72 | + QRCode.toString(text, { type: 'terminal' }, (err, url) => { |
| 73 | + console.log( |
| 74 | + '\n------------------------------------------------------------------------------' + |
| 75 | + `\n\n${url}` |
| 76 | + ); |
| 77 | + }); |
| 78 | +}; |
| 79 | + |
| 80 | +const showInstructions = ({ app, username, passwordCopied, secret, url, encrypt, config }) => { |
| 81 | + let orderCounter = 0; |
| 82 | + const getOrder = () => { |
| 83 | + orderCounter++; |
| 84 | + return orderCounter; |
| 85 | + } |
| 86 | + console.log( |
| 87 | + '------------------------------------------------------------------------------' + |
| 88 | + '\n\nFollow these steps to complete the set-up:' |
| 89 | + ); |
| 90 | + |
| 91 | + console.log( |
| 92 | + `\n${getOrder()}. Add the following settings for user "${username}" ${app ? `in app "${app}" ` : '' }to the Parse Dashboard configuration.` + |
| 93 | + `\n\n ${JSON.stringify(config)}` |
| 94 | + ); |
| 95 | + |
| 96 | + if (passwordCopied) { |
| 97 | + console.log( |
| 98 | + `\n${getOrder()}. Securely store the generated login password that has been copied to your clipboard.` |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + if (secret) { |
| 103 | + console.log( |
| 104 | + `\n${getOrder()}. Open the authenticator app to scan the QR code above or enter this secret code:` + |
| 105 | + `\n\n ${secret}` + |
| 106 | + '\n\n If the secret code generates incorrect one-time passwords, try this alternative:' + |
| 107 | + `\n\n ${url}` + |
| 108 | + `\n\n${getOrder()}. Destroy any records of the QR code and the secret code to secure the account.` |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + if (encrypt) { |
| 113 | + console.log( |
| 114 | + `\n${getOrder()}. Make sure that "useEncryptedPasswords" is set to "true" in your dashboard configuration.` + |
| 115 | + '\n You chose to generate an encrypted password for this user.' + |
| 116 | + '\n Any existing users with non-encrypted passwords will require newly created, encrypted passwords.' |
| 117 | + ); |
| 118 | + } |
| 119 | + console.log( |
| 120 | + '\n------------------------------------------------------------------------------\n' |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +module.exports = { |
| 125 | + async createUser() { |
| 126 | + const data = {}; |
| 127 | + |
| 128 | + console.log(''); |
| 129 | + const { username, password } = await inquirer.prompt([ |
| 130 | + { |
| 131 | + type: 'input', |
| 132 | + name: 'username', |
| 133 | + message: phrases.enterUsername |
| 134 | + }, |
| 135 | + { |
| 136 | + type: 'confirm', |
| 137 | + name: 'password', |
| 138 | + message: 'Do you want to auto-generate a password?' |
| 139 | + } |
| 140 | + ]); |
| 141 | + data.user = username; |
| 142 | + if (!password) { |
| 143 | + const { password } = await inquirer.prompt([ |
| 144 | + { |
| 145 | + type: 'password', |
| 146 | + name: 'password', |
| 147 | + message: phrases.enterPassword |
| 148 | + } |
| 149 | + ]); |
| 150 | + data.pass = password; |
| 151 | + } else { |
| 152 | + const password = crypto.randomBytes(20).toString('base64'); |
| 153 | + data.pass = password; |
| 154 | + } |
| 155 | + const { mfa, encrypt } = await inquirer.prompt([ |
| 156 | + { |
| 157 | + type: 'confirm', |
| 158 | + name: 'encrypt', |
| 159 | + message: 'Should the password be encrypted? (strongly recommended, otherwise it is stored in clear-text)' |
| 160 | + }, |
| 161 | + { |
| 162 | + type: 'confirm', |
| 163 | + name: 'mfa', |
| 164 | + message: 'Do you want to enable multi-factor authentication?' |
| 165 | + } |
| 166 | + ]); |
| 167 | + if (encrypt) { |
| 168 | + // Copy the raw password to clipboard |
| 169 | + copy(data.pass); |
| 170 | + |
| 171 | + // Encrypt password |
| 172 | + const bcrypt = require('bcryptjs'); |
| 173 | + const salt = bcrypt.genSaltSync(10); |
| 174 | + data.pass = bcrypt.hashSync(data.pass, salt); |
| 175 | + } |
| 176 | + if (mfa) { |
| 177 | + const { app } = await inquirer.prompt([ |
| 178 | + { |
| 179 | + type: 'input', |
| 180 | + name: 'app', |
| 181 | + message: phrases.enterAppName |
| 182 | + } |
| 183 | + ]); |
| 184 | + const { algorithm, digits, period } = await getAlgorithm(); |
| 185 | + const { secret, url } = generateSecret({ app, username, algorithm, digits, period }); |
| 186 | + data.mfa = secret; |
| 187 | + data.app = app; |
| 188 | + data.url = url; |
| 189 | + if (algorithm !== 'SHA1') { |
| 190 | + data.mfaAlgorithm = algorithm; |
| 191 | + } |
| 192 | + showQR(data.url); |
| 193 | + } |
| 194 | + |
| 195 | + const config = { mfa: data.mfa, user: data.user, pass: data.pass }; |
| 196 | + showInstructions({ app: data.app, username, passwordCopied: true, secret: data.mfa, url: data.url, encrypt, config }); |
| 197 | + }, |
| 198 | + async createMFA() { |
| 199 | + console.log(''); |
| 200 | + const { username, app } = await inquirer.prompt([ |
| 201 | + { |
| 202 | + type: 'input', |
| 203 | + name: 'username', |
| 204 | + message: |
| 205 | + 'Enter the username for which you want to enable multi-factor authentication:' |
| 206 | + }, |
| 207 | + { |
| 208 | + type: 'input', |
| 209 | + name: 'app', |
| 210 | + message: phrases.enterAppName |
| 211 | + } |
| 212 | + ]); |
| 213 | + const { algorithm, digits, period } = await getAlgorithm(); |
| 214 | + |
| 215 | + const { url, secret } = generateSecret({ app, username, algorithm, digits, period }); |
| 216 | + showQR(url); |
| 217 | + |
| 218 | + // Compose config |
| 219 | + const config = { mfa: secret }; |
| 220 | + if (algorithm !== 'SHA1') { |
| 221 | + config.mfaAlgorithm = algorithm; |
| 222 | + } |
| 223 | + showInstructions({ app, username, secret, url, config }); |
| 224 | + } |
| 225 | +}; |
0 commit comments