/
revoke.ts
106 lines (87 loc) · 3.05 KB
/
revoke.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
import { clColor, clCommand } from '@commercelayer/cli-core'
import Command, { Flags, Args, cliux } from '../../base'
import { revokeAccessToken } from '../../token'
import type { ArgOutput, FlagOutput, Input } from '@oclif/core/lib/interfaces/parser'
export default class TokenRevoke extends Command {
static description = 'revoke a Commerce Layer access token'
static examples = [
'$ commercelayer token:revoke -o <organizationSlug> <accessToken> -i <clientId>',
'$ cl token:revoke -o <organizationSlug> <accessToken> -i <clientId> -s <clientSecret>',
]
static flags = {
organization: Flags.string({
char: 'o',
description: 'the slug of your organization',
required: false,
exactlyOne: ['organization', 'provisioning'],
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'],
}),
scope: Flags.string({
char: 'S',
description: 'access token scope',
required: false,
multiple: true,
dependsOn: ['clientId'],
}),
provisioning: Flags.boolean({
char: 'P',
description: 'execute login to Provisioning API',
required: false,
exclusive: ['scope', 'organization', 'email', 'password', 'api'],
dependsOn: ['clientId', 'clientSecret']
})
}
static args = {
token: Args.string({ name: 'token', description: 'access token to revoke', required: true })
}
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 { args, flags } = await this.parse(TokenRevoke)
if (!flags.clientSecret && !flags.scope)
this.error(`You must provide one of the arguments ${clColor.cli.flag('clientSecret')} and ${clColor.cli.flag('scope')}`)
const accessToken: string = args.token
const scope = this.checkScope(flags.scope as string[], flags.provisioning as boolean)
const slug = flags.provisioning? undefined : flags.organization
const api = flags.provisioning? 'provisioning' : 'core'
this.log()
cliux.action.start(`Revoking ${api} access token`)
await revokeAccessToken({
domain: flags.domain,
slug,
clientId: flags.clientId,
clientSecret: flags.clientSecret,
scope,
}, accessToken)
.then(() => {
cliux.action.stop(`done ${clColor.msg.success('\u2714')}`)
this.log('\nThe access token has been successfully revoked\n')
})
.catch((error) => {
cliux.action.stop()
this.error(error.message as string)
})
}
}