@contentstack/cli-auth 0.1.1-beta.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +68 -40
- package/lib/commands/auth/login.js +56 -0
- package/lib/commands/auth/logout.js +61 -0
- package/lib/commands/auth/tokens/add.js +152 -0
- package/lib/commands/auth/tokens/index.js +54 -0
- package/lib/commands/auth/tokens/remove.js +55 -0
- package/lib/commands/auth/whoami.js +27 -0
- package/lib/config/index.js +0 -0
- package/lib/interfaces/index.js +2 -0
- package/lib/utils/auth-handler.js +118 -0
- package/lib/utils/index.js +7 -0
- package/lib/utils/interactive.js +55 -0
- package/lib/utils/tokens-validation.js +114 -0
- package/oclif.manifest.json +1 -1
- package/package.json +55 -32
- package/messages/index.json +0 -43
- package/src/commands/auth/login.js +0 -50
- package/src/commands/auth/logout.js +0 -44
- package/src/commands/auth/tokens/add.js +0 -86
- package/src/commands/auth/tokens/index.js +0 -59
- package/src/commands/auth/tokens/remove.js +0 -60
- package/src/commands/auth/whoami.js +0 -19
- package/src/hooks/validate-authtoken.js +0 -19
- package/src/util/auth-handler.js +0 -63
- package/src/util/messages.js +0 -18
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
const {Command, flags} = require('@oclif/command')
|
|
2
|
-
const {cli} = require('cli-ux')
|
|
3
|
-
const chalk = require('chalk')
|
|
4
|
-
const debug = require('debug')('csdx:tokens:add')
|
|
5
|
-
const Configstore = require('configstore')
|
|
6
|
-
const Messages = require('../../../util/messages')
|
|
7
|
-
const messages = new Messages('add').msgs
|
|
8
|
-
|
|
9
|
-
const config = new Configstore('contentstack_cli')
|
|
10
|
-
|
|
11
|
-
class AddCommand extends Command {
|
|
12
|
-
async run() {
|
|
13
|
-
let isReplace = false
|
|
14
|
-
const {flags} = this.parse(AddCommand)
|
|
15
|
-
let forced = flags.force
|
|
16
|
-
let alias = flags.alias
|
|
17
|
-
let apiKey = flags['api-key']
|
|
18
|
-
let token = flags.token
|
|
19
|
-
let isDelivery = flags.delivery
|
|
20
|
-
let isManagement = flags.management
|
|
21
|
-
let environment = flags.environment
|
|
22
|
-
let configKeyTokens = 'tokens'
|
|
23
|
-
let type = (isDelivery || Boolean(environment)) ? 'delivery' : 'management'
|
|
24
|
-
debug(`Adding ${type} token`)
|
|
25
|
-
|
|
26
|
-
// eslint-disable-next-line no-warning-comments
|
|
27
|
-
// TODO: Handle condition where token and api combination matched
|
|
28
|
-
|
|
29
|
-
if (!alias)
|
|
30
|
-
alias = await cli.prompt(messages.promptTokenAlias)
|
|
31
|
-
isReplace = Boolean(config.get(`${configKeyTokens}.${alias}`))// get to Check if alias already present
|
|
32
|
-
|
|
33
|
-
if (isReplace && !forced) {
|
|
34
|
-
isReplace = true
|
|
35
|
-
const confirm = await cli.confirm(Messages.parse(messages.promptConfirmReplaceToken, alias))
|
|
36
|
-
if (!confirm)
|
|
37
|
-
this.exit()
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (!apiKey)
|
|
41
|
-
apiKey = await cli.prompt(messages.promptAPIKey)
|
|
42
|
-
if (!token)
|
|
43
|
-
token = await cli.prompt(Messages.parse(messages.promptToken, type))
|
|
44
|
-
if (isDelivery && !environment)
|
|
45
|
-
environment = await cli.prompt(messages.promptEnv)
|
|
46
|
-
|
|
47
|
-
if (isManagement)
|
|
48
|
-
config.set(`${configKeyTokens}.${alias}`, {token, apiKey, type})
|
|
49
|
-
else
|
|
50
|
-
config.set(`${configKeyTokens}.${alias}`, {token, apiKey, environment, type})
|
|
51
|
-
|
|
52
|
-
if (isReplace)
|
|
53
|
-
cli.log(chalk.green(Messages.parse(messages.msgReplacedTokenSuccess, alias)))
|
|
54
|
-
else
|
|
55
|
-
cli.log(chalk.green(Messages.parse(messages.msgAddTokenSuccess, alias)))
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
AddCommand.description = `${messages.commandDescription}
|
|
60
|
-
by default it adds management token if either of management or delivery flags are not set`
|
|
61
|
-
|
|
62
|
-
AddCommand.flags = {
|
|
63
|
-
alias: flags.string({char: 'a', description: messages.flagAliasDescription}),
|
|
64
|
-
delivery: flags.boolean({
|
|
65
|
-
char: 'd',
|
|
66
|
-
description: 'Set this while saving delivery token',
|
|
67
|
-
exclusive: ['management'],
|
|
68
|
-
}),
|
|
69
|
-
management: flags.boolean({
|
|
70
|
-
char: 'm',
|
|
71
|
-
description: 'Set this while saving management token',
|
|
72
|
-
exclusive: ['delivery', 'environment'],
|
|
73
|
-
}),
|
|
74
|
-
environment: flags.string({
|
|
75
|
-
char: 'e',
|
|
76
|
-
description: 'Environment name for delivery token',
|
|
77
|
-
exclusive: ['management'],
|
|
78
|
-
}),
|
|
79
|
-
'api-key': flags.string({char: 'k', description: messages.flagAPIKeyDescription}),
|
|
80
|
-
force: flags.boolean({char: 'f', description: messages.flagForceDescription}),
|
|
81
|
-
token: flags.string({char: 't', description: messages.flagTokenDescription, env: 'TOKEN'}),
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
AddCommand.aliases = ['tokens:add']
|
|
85
|
-
|
|
86
|
-
module.exports = AddCommand
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
const {Command} = require('@oclif/command')
|
|
2
|
-
const {cli} = require('cli-ux')
|
|
3
|
-
const Configstore = require('configstore')
|
|
4
|
-
// const chalk = require('chalk')
|
|
5
|
-
|
|
6
|
-
const config = new Configstore('contentstack_cli')
|
|
7
|
-
|
|
8
|
-
class TokenIndex extends Command {
|
|
9
|
-
async run() {
|
|
10
|
-
let managementTokens = config.get('tokens')
|
|
11
|
-
let tokenOptions = []
|
|
12
|
-
if (managementTokens && Object.keys(managementTokens).length > 0) {
|
|
13
|
-
Object.keys(managementTokens).forEach(function (item) {
|
|
14
|
-
tokenOptions.push({
|
|
15
|
-
alias: item,
|
|
16
|
-
token: managementTokens[item].token,
|
|
17
|
-
apiKey: managementTokens[item].apiKey,
|
|
18
|
-
environment: managementTokens[item].environment ? managementTokens[item].environment : '-',
|
|
19
|
-
type: managementTokens[item].type,
|
|
20
|
-
})
|
|
21
|
-
})
|
|
22
|
-
const {flags} = this.parse(TokenIndex)
|
|
23
|
-
|
|
24
|
-
cli.table(tokenOptions,
|
|
25
|
-
{
|
|
26
|
-
alias: {
|
|
27
|
-
minWidth: 7,
|
|
28
|
-
},
|
|
29
|
-
token: {
|
|
30
|
-
minWidth: 7,
|
|
31
|
-
},
|
|
32
|
-
apiKey: {
|
|
33
|
-
minWidth: 7,
|
|
34
|
-
},
|
|
35
|
-
environment: {
|
|
36
|
-
minWidth: 7,
|
|
37
|
-
},
|
|
38
|
-
type: {
|
|
39
|
-
minWidth: 7,
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
printLine: this.log,
|
|
44
|
-
...flags, // parsed flags
|
|
45
|
-
},
|
|
46
|
-
)
|
|
47
|
-
} else {
|
|
48
|
-
cli.log('No tokens are added. Use auth:tokens:add command to add tokens.')
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
TokenIndex.description = `Lists all existing tokens added to the session
|
|
54
|
-
`
|
|
55
|
-
TokenIndex.aliases = ['tokens']
|
|
56
|
-
|
|
57
|
-
TokenIndex.flags = cli.table.flags()
|
|
58
|
-
|
|
59
|
-
module.exports = TokenIndex
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
const {Command, flags} = require('@oclif/command')
|
|
2
|
-
const {cli} = require('cli-ux')
|
|
3
|
-
const Configstore = require('configstore')
|
|
4
|
-
const inquirer = require('inquirer')
|
|
5
|
-
const Messages = require('../../../util/messages')
|
|
6
|
-
const messages = new Messages('remove').msgs
|
|
7
|
-
|
|
8
|
-
const config = new Configstore('contentstack_cli')
|
|
9
|
-
|
|
10
|
-
class RemoveCommand extends Command {
|
|
11
|
-
async run() {
|
|
12
|
-
const {flags} = this.parse(RemoveCommand)
|
|
13
|
-
let alias = flags.alias
|
|
14
|
-
let ignore = flags.ignore
|
|
15
|
-
|
|
16
|
-
let token = config.get(`tokens.${alias}`)
|
|
17
|
-
const tokens = config.get('tokens')
|
|
18
|
-
let tokenOptions = []
|
|
19
|
-
if (token || ignore) {
|
|
20
|
-
config.delete(`tokens.${alias}`)
|
|
21
|
-
return cli.log(`"${alias}" token removed successfully!`)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (tokens && Object.keys(tokens).length > 0) {
|
|
25
|
-
Object.keys(tokens).forEach(function (item) {
|
|
26
|
-
tokenOptions.push(`${item}: ${tokens[item].token} : ${tokens[item].apiKey}${tokens[item].environment ? ' : ' + tokens[item].environment + ' ' : ''}: ${tokens[item].type}`)
|
|
27
|
-
})
|
|
28
|
-
} else {
|
|
29
|
-
return cli.log('No tokens are added yet.')
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let responses = await inquirer.prompt([{
|
|
33
|
-
name: 'selectedTokens',
|
|
34
|
-
message: 'Select tokens to remove.',
|
|
35
|
-
type: 'checkbox',
|
|
36
|
-
choices: tokenOptions,
|
|
37
|
-
}])
|
|
38
|
-
|
|
39
|
-
let selectedTokens = responses.selectedTokens
|
|
40
|
-
if (selectedTokens.length === 0) {
|
|
41
|
-
return
|
|
42
|
-
}
|
|
43
|
-
selectedTokens.forEach(element => {
|
|
44
|
-
let selectedToken = element.split(':')[0]
|
|
45
|
-
config.delete(`tokens.${selectedToken}`)
|
|
46
|
-
cli.log(Messages.parse(messages.msgRemoveTokenSuccess, selectedToken))
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
RemoveCommand.description = messages.commandDescription
|
|
52
|
-
|
|
53
|
-
RemoveCommand.flags = {
|
|
54
|
-
alias: flags.string({char: 'a', description: messages.flagAliasDescription}),
|
|
55
|
-
ignore: flags.boolean({char: 'i', description: messages.flagIgnoreDescription}),
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
RemoveCommand.aliases = ['tokens:remove']
|
|
59
|
-
|
|
60
|
-
module.exports = RemoveCommand
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const {Command} = require('@contentstack/cli-command')
|
|
2
|
-
const chalk = require('chalk')
|
|
3
|
-
|
|
4
|
-
class WhoamiCommand extends Command {
|
|
5
|
-
async run() {
|
|
6
|
-
try {
|
|
7
|
-
this.log(`You are currently logged in with email: ${chalk.yellow(this.email)}`)
|
|
8
|
-
} catch (error) {
|
|
9
|
-
this.log(chalk.red(error.message))
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
WhoamiCommand.description = `Display current users email address
|
|
15
|
-
`
|
|
16
|
-
|
|
17
|
-
WhoamiCommand.aliases = ['whoami']
|
|
18
|
-
|
|
19
|
-
module.exports = WhoamiCommand
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const {AuthHandler} = require('../util/auth-handler')
|
|
2
|
-
const LoginCommand = require('../commands/auth/login')
|
|
3
|
-
const {cli} = require('cli-ux')
|
|
4
|
-
|
|
5
|
-
module.exports = async function () {
|
|
6
|
-
try {
|
|
7
|
-
let authtoken = this.authToken
|
|
8
|
-
const opts = {
|
|
9
|
-
contentstackClient: this.contentstackClient,
|
|
10
|
-
}
|
|
11
|
-
const authHandler = new AuthHandler(opts)
|
|
12
|
-
await authHandler.validateAuthtoken(authtoken)
|
|
13
|
-
} catch (error) {
|
|
14
|
-
let confirm = await cli.confirm('Your authtoken is expired or not valid. Do you want to login again?')
|
|
15
|
-
if (confirm) {
|
|
16
|
-
await LoginCommand.run()
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
package/src/util/auth-handler.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
let client = {}
|
|
2
|
-
|
|
3
|
-
class AuthHandler {
|
|
4
|
-
constructor(opts) {
|
|
5
|
-
client = opts.contentstackClient
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
*
|
|
10
|
-
* Login into Contentstack
|
|
11
|
-
* @param {string} email Contentstack email address
|
|
12
|
-
* @param {string} password User's password for contentstack account
|
|
13
|
-
* @returns {Promise} Promise object returns authtoken on success
|
|
14
|
-
*/
|
|
15
|
-
async login(email, password) {
|
|
16
|
-
return new Promise(function (resolve, reject) {
|
|
17
|
-
if (email && password) {
|
|
18
|
-
client.login({email: email, password: password}).then(result => {
|
|
19
|
-
return resolve(result.user)
|
|
20
|
-
}).catch(error => {
|
|
21
|
-
return reject(error)
|
|
22
|
-
})
|
|
23
|
-
}
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
*
|
|
29
|
-
* Logout from Contentstack
|
|
30
|
-
* @param {string} authtoken authtoken that needs to invalidated when logging out
|
|
31
|
-
* @returns {Promise} Promise object returns response object from Contentstack
|
|
32
|
-
*/
|
|
33
|
-
async logout(authtoken) {
|
|
34
|
-
return new Promise(function (resolve, reject) {
|
|
35
|
-
if (authtoken) {
|
|
36
|
-
client.logout(authtoken).then(function (response) {
|
|
37
|
-
return resolve(response)
|
|
38
|
-
}).catch(error => {
|
|
39
|
-
reject(error)
|
|
40
|
-
})
|
|
41
|
-
} else {
|
|
42
|
-
return resolve()
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async validateAuthtoken(authtoken) {
|
|
48
|
-
return new Promise(function (resolve, reject) {
|
|
49
|
-
if (authtoken) {
|
|
50
|
-
client.login({token: authtoken})
|
|
51
|
-
client.getUser()
|
|
52
|
-
.then(user => {
|
|
53
|
-
return resolve(user)
|
|
54
|
-
}).catch(error => reject(error))
|
|
55
|
-
}
|
|
56
|
-
return resolve()
|
|
57
|
-
})
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
module.exports = {
|
|
62
|
-
AuthHandler, client,
|
|
63
|
-
}
|
package/src/util/messages.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const messages = require('../../messages/index.json')
|
|
2
|
-
|
|
3
|
-
class Messages {
|
|
4
|
-
constructor(type) {
|
|
5
|
-
this.msgs = messages[type]
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
static parse(msg, ...substitutions) {
|
|
9
|
-
if (substitutions.length > 0) {
|
|
10
|
-
let callSite = msg.split('%s')
|
|
11
|
-
callSite.push('')
|
|
12
|
-
return String.raw({raw: callSite}, ...substitutions)
|
|
13
|
-
}
|
|
14
|
-
return msg
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
module.exports = Messages
|