-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.ts
128 lines (100 loc) · 3.49 KB
/
get.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
118
119
120
121
122
123
124
125
126
127
128
import Command, { Flags } from '../../base'
import {type AppAuth, clColor, clConfig, clCommand } from '@commercelayer/cli-core'
import { decodeAccessToken, getAccessToken } from '../../token'
import type { ArgOutput, FlagOutput, Input } from '@oclif/core/lib/interfaces/parser'
export default class TokenGet extends Command {
static description = 'get a new access token'
static examples = [
'$ commercelayer token:get -o <organizationSlug> -i <clientId> -s <clientSecret>',
'$ cl token:get -o <organizationSlug> -i <clientId> -S <scope> --info',
'$ cl token:get -i <clientId> -s <clientSecret>',
]
static flags = {
organization: Flags.string({
char: 'o',
description: 'the slug of your organization',
required: false,
env: 'CL_CLI_ORGANIZATION',
}),
domain: Flags.string({
char: 'd',
required: false,
hidden: true,
env: 'CL_CLI_DOMAIN',
}),
clientId: Flags.string({
name: 'clientId',
char: 'i',
description: 'application client_id',
required: true,
}),
clientSecret: Flags.string({
char: 's',
description: 'application client_secret',
required: false,
dependsOn: ['clientId'],
exclusive: ['email', 'password']
}),
scope: Flags.string({
char: 'S',
description: 'access token scope (market, stock location)',
required: false,
multiple: true,
dependsOn: ['clientId'],
}),
email: Flags.string({
char: 'e',
description: 'customer email',
dependsOn: ['password'],
}),
password: Flags.string({
char: 'p',
description: 'customer secret password',
dependsOn: ['email'],
}),
info: Flags.boolean({
description: 'show access token info',
})
}
async parse(c: any): Promise<any> {
clCommand.fixDashedFlagValue(this.argv, c.flags.clientId)
const parsed = await super.parse(c as Input<FlagOutput, FlagOutput, ArgOutput>)
clCommand.fixDashedFlagValue(this.argv, c.flags.clientId, 'i', parsed)
return parsed
}
async run(): Promise<any> {
const { flags } = await this.parse(TokenGet)
if (!flags.clientSecret && !flags.scope)
this.error(`You must provide one of the arguments ${clColor.cli.arg('clientSecret')} and ${clColor.cli.arg('scope')}`)
const scope = this.checkScope(flags.scope as string[])
const slug = flags.organization || clConfig.provisioning.default_subdomain
const config: AppAuth = {
clientId: flags.clientId,
clientSecret: flags.clientSecret,
slug,
domain: flags.domain,
scope,
email: flags.email,
password: flags.password
}
const token = await getAccessToken(config).catch(error => this.error(error.message as string))
if (token) {
try {
const accessToken = token.accessToken
const decodedAccessToken = decodeAccessToken(accessToken)
let msg = `Access token for ${clColor.api.kind(decodedAccessToken.application.kind)} application`
if (decodedAccessToken.organization) msg += ` of organization ${clColor.api.slug(decodedAccessToken.organization.slug)}`
if (flags.provisioning) msg += ' [provisioning]'
this.log(`\n${msg}:`)
this.printAccessToken(accessToken)
if (flags.info) {
const tokenInfo = this.printAccessTokenInfo(decodedAccessToken)
return `${accessToken}\n${tokenInfo}`
}
return accessToken
} catch (error: any) {
this.error(error.message as string)
}
}
}
}