-
Notifications
You must be signed in to change notification settings - Fork 6
/
token.ts
89 lines (76 loc) · 2.85 KB
/
token.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import {CLICommand} from '@coveo/cli-commons/command/cliCommand';
import {CliUx, Flags} from '@oclif/core';
import {Config} from '@coveo/cli-commons/config/config';
import {AuthenticatedClient} from '@coveo/cli-commons/platform/authenticatedClient';
import {PlatformEnvironment} from '@coveo/cli-commons/platform/environment';
import {Region} from '@coveo/platform-client';
import {withEnvironment, withRegion} from '../../lib/flags/platformCommonFlags';
import {Trackable} from '@coveo/cli-commons/preconditions/trackable';
import {formatOrgId} from '@coveo/cli-commons/utils/ux';
import {readFromStdinWithTimeout} from '../../lib/utils/process';
export default class Token extends CLICommand {
private configuration!: Config;
public static description =
'Log in to the Coveo Platform using an access token.';
public static examples = ['$ coveo auth:token'];
public static flags = {
...withRegion(),
...withEnvironment(),
stdin: Flags.boolean({
char: 's',
description:
'Whether to read the token from stdin. Default to true when running in a CI environment.',
default: process.env.CI === 'true',
allowNo: true,
}),
};
@Trackable({eventName: 'auth login token'})
public async run() {
this.configuration = new Config(this.config.configDir);
await this.saveToken();
await this.saveRegionAndEnvironment();
await this.fetchAndSaveOrgId();
this.feedbackOnSuccessfulLogin();
}
private feedbackOnSuccessfulLogin() {
const cfg = this.configuration.get();
this.log(`
Successfully logged in!
Close your browser to continue.
You are currently logged in:
Organization: ${formatOrgId(cfg.organization)}
Region: ${cfg.region}
Environment: ${cfg.environment}
Run auth:login --help to see the available options to log in to a different organization, region, or environment.
`);
}
private async saveToken() {
const {flags} = await this.parse(Token);
const tok = flags.stdin
? await readFromStdinWithTimeout(1000)
: await CliUx.ux.prompt('Enter your access token: ', {
type: 'hide',
});
this.configuration.set('accessToken', this.sanitizeToken(tok));
this.configuration.set('anonymous', true);
}
private sanitizeToken(token: string) {
return token.replace(/\n/gi, '').trim();
}
private async saveRegionAndEnvironment() {
const {flags} = await this.parse(Token);
const cfg = this.configuration;
cfg.set('environment', flags.environment as PlatformEnvironment);
cfg.set('region', flags.region as Region);
}
private async fetchAndSaveOrgId() {
this.configuration.set(
'organization',
await this.pickFirstAvailableOrganization()
);
}
private async pickFirstAvailableOrganization() {
const orgs = await new AuthenticatedClient().getAllOrgsUserHasAccessTo();
return orgs[0]?.id;
}
}