/
client-token.js
71 lines (56 loc) 路 2.18 KB
/
client-token.js
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
'use strict'
const consola = require('consola')
const request = require('request')
const inquirer = require('inquirer')
const { Command } = require('@oclif/command')
const config = require('../config')
const { coppyToClipboard } = require('../utils')
class ClientTokenCommand extends Command {
async run () {
const user = config.get('user')
const accessToken = config.get('accessToken')
const clientId = config.get('clientId')
const clientSecret = config.get('clientSecret')
if (!clientId || !clientSecret) {
consola.error('You must provide a valid client-keys pair in order to create a client token')
consola.info('To create a new client-keys pair run: bcl client-keys create')
return
}
consola.info(`Retrieving new client accessToken for ${user}`)
const { save } = await inquirer.prompt([
{ name: 'save', message: 'Do you want bcl to store your tokens locally for future usage?', type: 'confirm' } // eslint-disable-line
])
const env = config.get('env') || 'prod'
const url = `${config.get(`services.${env}.accounts.url`)}/auth/token`
request.post(url, {
headers: { Authorization: `Bearer ${accessToken}` },
json: {
grantType: 'clientCredentials',
clientId,
clientSecret
}
}, function (err, data) {
if (err) {
return consola.error(`Error generating client accessToken: ${err}.`)
}
if (data.statusCode === 401 || data.statusCode === 403) {
return consola.error('Your client keys are invalid')
}
if (!data.body.accessToken || !data.body.refreshToken) {
return consola.error('Error generating client accessToken.')
}
consola.success(
'Generated new tokens: \n\n' +
`\t* clientAccessToken: ${data.body.accessToken} \n` +
`\t* refreshToken: ${data.body.refreshToken}`
)
if (save) {
config.set('clientAccessToken', data.body.accessToken)
config.set('refreshToken', data.body.refreshToken)
}
coppyToClipboard(data.body.accessToken, 'Client access token')
})
}
}
ClientTokenCommand.description = 'Generate new client token(s)'
module.exports = ClientTokenCommand