/
token.ts
77 lines (67 loc) · 2.55 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
import {CLICommand} from '@coveo/cli-commons/command/cliCommand';
import {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';
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(),
token: Flags.string({
char: 't',
description:
'The API-Key that shall be used to authenticate you to the organization. See <https://github.com/coveo/cli/wiki/Using-the-CLI-using-an-API-Key>.',
required: true,
helpValue: 'xxx-api-key',
}),
};
@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);
this.configuration.set('accessToken', flags.token);
this.configuration.set('anonymous', true);
}
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;
}
}