/
grant.ts
117 lines (105 loc) · 3.88 KB
/
grant.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as os from 'os';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { AuthFields, AuthInfo, AuthRemover, Messages, SfdxError } from '@salesforce/core';
import { get, getString, Optional } from '@salesforce/ts-types';
import { Prompts } from '../../../prompts';
import { Common } from '../../../common';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'jwt.grant');
const commonMessages = Messages.loadMessages('@salesforce/plugin-auth', 'messages');
export default class Grant extends SfdxCommand {
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessage('examples').split(os.EOL);
public static aliases = ['force:auth:jwt:grant'];
public static readonly flagsConfig: FlagsConfig = {
username: flags.string({
char: 'u',
description: messages.getMessage('username'),
required: true,
}),
jwtkeyfile: flags.filepath({
char: 'f',
description: messages.getMessage('key'),
required: true,
}),
clientid: flags.string({
char: 'i',
description: commonMessages.getMessage('clientId'),
required: true,
}),
instanceurl: flags.url({
char: 'r',
description: commonMessages.getMessage('instanceUrl'),
}),
setdefaultdevhubusername: flags.boolean({
char: 'd',
description: commonMessages.getMessage('setDefaultDevHub'),
}),
setdefaultusername: flags.boolean({
char: 's',
description: commonMessages.getMessage('setDefaultUsername'),
}),
setalias: flags.string({
char: 'a',
description: commonMessages.getMessage('setAlias'),
}),
noprompt: flags.boolean({
char: 'p',
description: commonMessages.getMessage('noPromptAuth'),
required: false,
hidden: true,
}),
};
public async run(): Promise<AuthFields> {
let result: AuthFields = {};
if (await Prompts.shouldExitCommand(this.ux, this.flags.noprompt)) return {};
try {
const authInfo = await this.initAuthInfo();
await Common.handleSideEffects(authInfo, this.flags);
result = authInfo.getFields(true);
await Common.identifyPossibleScratchOrgs(result, authInfo);
} catch (err) {
const msg = getString(err, 'message');
throw SfdxError.create('@salesforce/plugin-auth', 'jwt.grant', 'JwtGrantError', [msg]);
}
const successMsg = commonMessages.getMessage('authorizeCommandSuccess', [result.username, result.orgId]);
this.ux.log(successMsg);
return result;
}
private async initAuthInfo(): Promise<AuthInfo> {
const oauth2OptionsBase = {
clientId: this.flags.clientid as string,
privateKeyFile: this.flags.jwtkeyfile as string,
};
const loginUrl = await Common.resolveLoginUrl(get(this.flags.instanceurl, 'href', null) as Optional<string>);
const oauth2Options = loginUrl ? Object.assign(oauth2OptionsBase, { loginUrl }) : oauth2OptionsBase;
let authInfo: AuthInfo;
try {
authInfo = await AuthInfo.create({
username: this.flags.username as string,
oauth2Options,
});
} catch (error) {
const err = error as SfdxError;
if (err.name === 'AuthInfoOverwriteError') {
this.logger.debug('Auth file already exists. Removing and starting fresh.');
const remover = await AuthRemover.create();
await remover.removeAuth(this.flags.username);
authInfo = await AuthInfo.create({
username: this.flags.username as string,
oauth2Options,
});
} else {
throw err;
}
}
await authInfo.save();
return authInfo;
}
}