-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtoken.ts
More file actions
48 lines (38 loc) · 1.35 KB
/
token.ts
File metadata and controls
48 lines (38 loc) · 1.35 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
import {ux} from '@oclif/core';
import {OAuthCommand} from '@salesforce/b2c-tooling-sdk/cli';
import type {AccessTokenResponse} from '@salesforce/b2c-tooling-sdk/auth';
import {t} from '../../i18n/index.js';
/**
* JSON output structure for the token command
*/
interface TokenJsonOutput {
accessToken: string;
expires: string;
scopes: string[];
}
export default class AuthToken extends OAuthCommand<typeof AuthToken> {
static description = t('commands.auth.token.description', 'Get an OAuth access token');
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> --scope sfcc.orders --scope sfcc.products',
'<%= config.bin %> <%= command.id %> --json',
];
async run(): Promise<TokenJsonOutput> {
this.requireOAuthCredentials();
const strategy = this.getOAuthStrategy();
const tokenResponse: AccessTokenResponse = await strategy.getTokenResponse();
const output: TokenJsonOutput = {
accessToken: tokenResponse.accessToken,
expires: tokenResponse.expires.toISOString(),
scopes: tokenResponse.scopes,
};
// In JSON mode, return the full token response
if (this.jsonEnabled()) {
return output;
}
// In normal mode, output just the raw token to stdout
ux.stdout(tokenResponse.accessToken);
return output;
}
}