Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
75 lines (68 loc) · 2.8 KB
/
Copy pathauth.ts
File metadata and controls
75 lines (68 loc) · 2.8 KB
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
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { loglevel, SfCommand } from '@salesforce/sf-plugins-core';
import { AuthInfo, Messages, OrgAuthorization } from '@salesforce/core';
type AuthListResult = Omit<OrgAuthorization, 'aliases'> & { alias: string };
export type AuthListResults = AuthListResult[];
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'list');
const secretsMessages = Messages.loadMessages('@salesforce/plugin-auth', 'secrets-redacted');
export default class ListAuth extends SfCommand<AuthListResults> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:auth:list', 'auth:list'];
public static readonly flags = {
loglevel,
};
public async run(): Promise<AuthListResults> {
await this.parse(ListAuth);
try {
const auths = await AuthInfo.listAllAuthorizations();
if (auths.length === 0) {
this.log(messages.getMessage('noResultsFound'));
return [];
}
const accessTokenRedacted = secretsMessages.getMessage('redacted.accessToken');
const mappedAuths: AuthListResults = auths.map((auth: OrgAuthorization) => {
const { aliases, ...rest } = auth;
// core3 moved to aliases as a string[], revert to alias as a string
return {
...rest,
alias: aliases ? aliases.join(',') : '',
accessToken: rest.accessToken ? accessTokenRedacted : undefined,
};
});
const hasErrors = auths.filter((auth) => !!auth.error).length > 0;
this.table({
data: mappedAuths.map((auth) => ({
ALIAS: auth.alias,
USERNAME: auth.username,
'ORG ID': auth.orgId,
'INSTANCE URL': auth.instanceUrl,
'AUTH METHOD': auth.oauthMethod,
...(hasErrors ? { error: { header: 'ERROR' } } : {}),
})),
title: 'authenticated orgs',
});
return mappedAuths;
} catch (_err) {
this.log(messages.getMessage('noResultsFound'));
return [];
}
}
}