|
| 1 | +import { Command, Flags } from '@oclif/core'; |
| 2 | +import { EmptyPasswordError, NotValidEmailError, NotValidTwoFactorCodeError } from '../types/command.types'; |
| 3 | +import { AuthService } from '../services/auth.service'; |
| 4 | +import { ConfigService } from '../services/config.service'; |
| 5 | +import { ValidationService } from '../services/validation.service'; |
| 6 | +import { CLIUtils } from '../utils/cli.utils'; |
| 7 | +import { SdkManager } from '../services/sdk-manager.service'; |
| 8 | +import * as OTPAuth from 'otpauth'; |
| 9 | + |
| 10 | +export default class LoginLegacy extends Command { |
| 11 | + static readonly args = {}; |
| 12 | + static readonly description = |
| 13 | + '[Legacy] Logs into an Internxt account using user and password. ' + |
| 14 | + 'If the account is two-factor protected, then an extra code will be required.'; |
| 15 | + static readonly aliases = []; |
| 16 | + static readonly examples = ['<%= config.bin %> <%= command.id %>']; |
| 17 | + static readonly flags = { |
| 18 | + ...CLIUtils.CommonFlags, |
| 19 | + email: Flags.string({ |
| 20 | + char: 'e', |
| 21 | + aliases: ['mail'], |
| 22 | + env: 'INXT_USER', |
| 23 | + description: 'The email to log in', |
| 24 | + required: false, |
| 25 | + }), |
| 26 | + password: Flags.string({ |
| 27 | + char: 'p', |
| 28 | + aliases: ['pass'], |
| 29 | + env: 'INXT_PASSWORD', |
| 30 | + description: 'The plain password to log in', |
| 31 | + required: false, |
| 32 | + }), |
| 33 | + twofactor: Flags.string({ |
| 34 | + char: 'w', |
| 35 | + aliases: ['two', 'two-factor'], |
| 36 | + env: 'INXT_TWOFACTORCODE', |
| 37 | + description: 'The two factor auth code (TOTP). ', |
| 38 | + required: false, |
| 39 | + helpValue: '123456', |
| 40 | + }), |
| 41 | + twofactortoken: Flags.string({ |
| 42 | + char: 't', |
| 43 | + aliases: ['otp', 'otp-token'], |
| 44 | + env: 'INXT_OTPTOKEN', |
| 45 | + description: |
| 46 | + 'The TOTP secret token. It is used to generate a TOTP code if needed.' + |
| 47 | + ' It has prority over the two factor code flag.', |
| 48 | + required: false, |
| 49 | + helpValue: 'token', |
| 50 | + }), |
| 51 | + }; |
| 52 | + static readonly enableJsonFlag = true; |
| 53 | + |
| 54 | + public run = async () => { |
| 55 | + const { flags } = await this.parse(LoginLegacy); |
| 56 | + |
| 57 | + const nonInteractive = flags['non-interactive']; |
| 58 | + const email = await this.getEmail(flags['email'], nonInteractive); |
| 59 | + const password = await this.getPassword(flags['password'], nonInteractive); |
| 60 | + |
| 61 | + const is2FANeeded = await AuthService.instance.is2FANeeded(email); |
| 62 | + let twoFactorCode: string | undefined; |
| 63 | + if (is2FANeeded) { |
| 64 | + const twoFactorToken = flags['twofactortoken']; |
| 65 | + if (twoFactorToken && twoFactorToken.trim().length > 0) { |
| 66 | + const totp = new OTPAuth.TOTP({ |
| 67 | + secret: twoFactorToken, |
| 68 | + digits: 6, |
| 69 | + }); |
| 70 | + twoFactorCode = totp.generate(); |
| 71 | + } else { |
| 72 | + twoFactorCode = await this.getTwoFactorCode(flags['twofactor'], nonInteractive); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const loginCredentials = await AuthService.instance.doLogin(email, password, twoFactorCode); |
| 77 | + |
| 78 | + SdkManager.init({ token: loginCredentials.token }); |
| 79 | + |
| 80 | + await ConfigService.instance.saveUser(loginCredentials); |
| 81 | + const message = `Succesfully logged in to: ${loginCredentials.user.email}`; |
| 82 | + CLIUtils.success(this.log.bind(this), message); |
| 83 | + return { |
| 84 | + success: true, |
| 85 | + message, |
| 86 | + login: loginCredentials, |
| 87 | + }; |
| 88 | + }; |
| 89 | + |
| 90 | + public catch = async (error: Error) => { |
| 91 | + const { flags } = await this.parse(LoginLegacy); |
| 92 | + CLIUtils.catchError({ |
| 93 | + error, |
| 94 | + command: this.id, |
| 95 | + logReporter: this.log.bind(this), |
| 96 | + jsonFlag: flags['json'], |
| 97 | + }); |
| 98 | + this.exit(1); |
| 99 | + }; |
| 100 | + |
| 101 | + private getEmail = async (emailFlag: string | undefined, nonInteractive: boolean): Promise<string> => { |
| 102 | + const email = await CLIUtils.getValueFromFlag( |
| 103 | + { |
| 104 | + value: emailFlag, |
| 105 | + name: LoginLegacy.flags['email'].name, |
| 106 | + }, |
| 107 | + { |
| 108 | + nonInteractive, |
| 109 | + prompt: { |
| 110 | + message: 'What is your email?', |
| 111 | + options: { type: 'input' }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + validate: ValidationService.instance.validateEmail, |
| 116 | + error: new NotValidEmailError(), |
| 117 | + }, |
| 118 | + this.log.bind(this), |
| 119 | + ); |
| 120 | + return email; |
| 121 | + }; |
| 122 | + |
| 123 | + private getPassword = async (passwordFlag: string | undefined, nonInteractive: boolean): Promise<string> => { |
| 124 | + const password = await CLIUtils.getValueFromFlag( |
| 125 | + { |
| 126 | + value: passwordFlag, |
| 127 | + name: LoginLegacy.flags['password'].name, |
| 128 | + }, |
| 129 | + { |
| 130 | + nonInteractive, |
| 131 | + prompt: { |
| 132 | + message: 'What is your password?', |
| 133 | + options: { type: 'password' }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + { |
| 137 | + validate: ValidationService.instance.validateStringIsNotEmpty, |
| 138 | + error: new EmptyPasswordError(), |
| 139 | + }, |
| 140 | + this.log.bind(this), |
| 141 | + ); |
| 142 | + return password; |
| 143 | + }; |
| 144 | + |
| 145 | + private getTwoFactorCode = async (twoFactorFlag: string | undefined, nonInteractive: boolean): Promise<string> => { |
| 146 | + const twoFactor = await CLIUtils.getValueFromFlag( |
| 147 | + { |
| 148 | + value: twoFactorFlag, |
| 149 | + name: LoginLegacy.flags['twofactor'].name, |
| 150 | + }, |
| 151 | + { |
| 152 | + nonInteractive, |
| 153 | + prompt: { |
| 154 | + message: 'What is your two-factor code?', |
| 155 | + options: { type: 'mask' }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + validate: ValidationService.instance.validate2FA, |
| 160 | + error: new NotValidTwoFactorCodeError(), |
| 161 | + }, |
| 162 | + this.log.bind(this), |
| 163 | + ); |
| 164 | + return twoFactor; |
| 165 | + }; |
| 166 | +} |
0 commit comments