|
1 | | -import crypto from 'node:crypto'; |
2 | | -import util from 'node:util'; |
3 | | - |
4 | 1 | import colors from 'colors/safe.js'; |
5 | | -import open from 'open'; |
6 | | -import superagent from 'superagent'; |
7 | | - |
8 | | -import { Logger } from '../logger.js'; |
9 | 2 | import * as User from '../models/user.js'; |
10 | | -import { conf, writeOAuthConf } from '../models/configuration.js'; |
11 | | - |
12 | | -import { getPackageJson } from '../load-package-json.cjs'; |
13 | | - |
14 | | -const delay = util.promisify(setTimeout); |
15 | | -const pkg = getPackageJson(); |
16 | 3 |
|
17 | | -// 20 random bytes as Base64URL |
18 | | -function randomToken () { |
19 | | - return crypto.randomBytes(20).toString('base64').replace(/\//g, '-').replace(/\+/g, '_').replace(/=/g, ''); |
20 | | -} |
21 | | - |
22 | | -const POLLING_INTERVAL = 2000; |
23 | | -const POLLING_MAX_TRY_COUNT = 60; |
24 | | - |
25 | | -function pollOauthData (url, tryCount = 0) { |
26 | | - |
27 | | - if (tryCount >= POLLING_MAX_TRY_COUNT) { |
28 | | - throw new Error('Something went wrong while trying to log you in.'); |
29 | | - } |
30 | | - if (tryCount > 1 && tryCount % 10 === 0) { |
31 | | - Logger.println("We're still waiting for the login process (in your browser) to be completed…"); |
32 | | - } |
33 | | - |
34 | | - return superagent |
35 | | - .get(url) |
36 | | - .send() |
37 | | - .then(({ body }) => body) |
38 | | - .catch(async (e) => { |
39 | | - if (e.status === 404) { |
40 | | - await delay(POLLING_INTERVAL); |
41 | | - return pollOauthData(url, tryCount + 1); |
42 | | - } |
43 | | - throw new Error('Something went wrong while trying to log you in.'); |
44 | | - }); |
45 | | -} |
| 4 | +import { Logger } from '../logger.js'; |
| 5 | +import { promptEmail, promptPassword } from '../prompt.js'; |
| 6 | +import { sendToAuthBridge } from '../models/send-to-api.js'; |
| 7 | +import { writeOAuthConf } from '../models/configuration.js'; |
| 8 | +import { createApiToken } from '../clever-client/auth-bridge.js'; |
46 | 9 |
|
47 | 10 | async function loginViaConsole () { |
48 | | - |
49 | | - const cliToken = randomToken(); |
50 | | - |
51 | | - const consoleUrl = new URL(conf.CONSOLE_TOKEN_URL); |
52 | | - consoleUrl.searchParams.set('cli_version', pkg.version); |
53 | | - consoleUrl.searchParams.set('cli_token', cliToken); |
54 | | - |
55 | | - const cliPollUrl = new URL(conf.API_HOST); |
56 | | - cliPollUrl.pathname = '/v2/self/cli_tokens'; |
57 | | - cliPollUrl.searchParams.set('cli_token', cliToken); |
58 | | - |
59 | | - Logger.debug('Try to login to Clever Cloud…'); |
60 | | - Logger.println(`Opening ${colors.green(consoleUrl.toString())} in your browser to log you in…`); |
61 | | - await open(consoleUrl.toString(), { wait: false }); |
62 | | - |
63 | | - return pollOauthData(cliPollUrl.toString()); |
| 11 | + const dateObject = new Date(); |
| 12 | + dateObject.setFullYear(dateObject.getFullYear() + 1); |
| 13 | + const expirationDate = dateObject; |
| 14 | + |
| 15 | + const name = `Clever Tools - ${dateObject.getTime()}`; |
| 16 | + const email = await promptEmail('Enter your email:'); |
| 17 | + const password = await promptPassword('Enter your password:'); |
| 18 | + const mfaCode = await promptPassword('Enter your 2FA code (press Enter if none):'); |
| 19 | + |
| 20 | + const tokenData = { |
| 21 | + email, |
| 22 | + password, |
| 23 | + mfaCode, |
| 24 | + name, |
| 25 | + expirationDate: expirationDate.toISOString(), |
| 26 | + }; |
| 27 | + |
| 28 | + return createApiToken(tokenData).then(sendToAuthBridge).catch((error) => { |
| 29 | + const errorCode = error?.cause?.responseBody?.code; |
| 30 | + if (errorCode === 'invalid-credential') { |
| 31 | + throw new Error('Invalid credentials, check your password'); |
| 32 | + } |
| 33 | + if (errorCode === 'invalid-mfa-code') { |
| 34 | + throw new Error('Invalid credentials, check your 2FA code'); |
| 35 | + } |
| 36 | + throw error; |
| 37 | + }); |
64 | 38 | } |
65 | 39 |
|
66 | 40 | export async function login (params) { |
|
0 commit comments