balena-cli/lib/actions/auth.coffee

211 lines
5.0 KiB
CoffeeScript
Raw Normal View History

2016-01-04 03:58:51 +00:00
###
Copyright 2016 Resin.io
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
###
exports.login =
signature: 'login'
description: 'login to resin.io'
help: '''
Use this command to login to your resin.io account.
This command will prompt you to login using the following login types:
- Web authorization: open your web browser and prompt you to authorize the CLI
from the dashboard.
- Credentials: using email/password and 2FA.
- Token: using the authentication token from the preferences page.
Examples:
$ resin login
$ resin login --web
$ resin login --token "..."
$ resin login --credentials
$ resin login --credentials --email johndoe@gmail.com --password secret
'''
options: [
{
signature: 'token'
description: 'auth token'
parameter: 'token'
alias: 't'
}
{
signature: 'web'
description: 'web-based login'
boolean: true
alias: 'w'
}
{
signature: 'credentials'
description: 'credential-based login'
boolean: true
alias: 'c'
}
{
signature: 'email'
parameter: 'email'
description: 'email'
alias: [ 'e', 'u' ]
}
{
signature: 'password'
parameter: 'password'
description: 'password'
alias: 'p'
}
]
primary: true
action: (params, options, done) ->
_ = require('lodash')
Promise = require('bluebird')
2017-01-25 18:32:07 +00:00
resin = require('resin-sdk-preconfigured')
auth = require('resin-cli-auth')
form = require('resin-cli-form')
patterns = require('../utils/patterns')
2016-01-12 14:44:11 +00:00
messages = require('../utils/messages')
login = (options) ->
if options.token?
return Promise.try ->
return options.token if _.isString(options.token)
return form.ask
message: 'Token (from the preferences page)'
name: 'token'
type: 'input'
.then(resin.auth.loginWithToken)
else if options.credentials
return patterns.authenticate(options)
else if options.web
console.info('Connecting to the web dashboard')
return auth.login()
return patterns.askLoginType().then (loginType) ->
if loginType is 'register'
capitanoRunAsync = Promise.promisify(require('capitano').run)
return capitanoRunAsync('signup')
options[loginType] = true
return login(options)
resin.settings.get('resinUrl').then (resinUrl) ->
2016-01-12 14:44:11 +00:00
console.log(messages.resinAsciiArt)
2016-01-12 14:23:46 +00:00
console.log("\nLogging in to #{resinUrl}")
return login(options)
2015-08-13 18:50:36 +00:00
.then(resin.auth.whoami)
.tap (username) ->
console.info("Successfully logged in as: #{username}")
2016-01-12 14:44:11 +00:00
console.info """
Now what?
2016-01-12 14:44:11 +00:00
#{messages.gettingStarted}
Find out about more super powers by running:
$ resin help
2016-01-12 14:44:11 +00:00
#{messages.reachingOut}
"""
2015-08-13 18:50:36 +00:00
.nodeify(done)
2014-12-24 15:14:30 +00:00
exports.logout =
signature: 'logout'
description: 'logout from resin.io'
help: '''
Use this command to logout from your resin.io account.o
2014-12-24 15:14:30 +00:00
Examples:
$ resin logout
'''
permission: 'user'
action: (params, options, done) ->
2017-01-25 18:32:07 +00:00
resin = require('resin-sdk-preconfigured')
2016-02-12 18:34:16 +00:00
resin.auth.logout().nodeify(done)
2014-12-24 15:14:30 +00:00
exports.signup =
signature: 'signup'
description: 'signup to resin.io'
help: '''
Use this command to signup for a resin.io account.
2014-12-24 15:14:30 +00:00
If signup is successful, you'll be logged in to your new user automatically.
2014-12-12 14:25:32 +00:00
Examples:
$ resin signup
Email: johndoe@acme.com
Password: ***********
2014-12-12 14:25:32 +00:00
$ resin whoami
johndoe
'''
action: (params, options, done) ->
2017-01-25 18:32:07 +00:00
resin = require('resin-sdk-preconfigured')
form = require('resin-cli-form')
validation = require('../utils/validation')
resin.settings.get('resinUrl').then (resinUrl) ->
console.log("\nRegistering to #{resinUrl}")
form.run [
message: 'Email:'
name: 'email'
type: 'input'
validate: validation.validateEmail
,
message: 'Password:'
name: 'password'
type: 'password',
validate: validation.validatePassword
]
2015-08-13 18:50:36 +00:00
.then(resin.auth.register)
.then(resin.auth.loginWithToken)
.nodeify(done)
exports.whoami =
signature: 'whoami'
2015-08-13 18:50:36 +00:00
description: 'get current username and email address'
help: '''
2015-08-13 18:50:36 +00:00
Use this command to find out the current logged in username and email address.
Examples:
$ resin whoami
'''
permission: 'user'
action: (params, options, done) ->
Promise = require('bluebird')
2017-01-25 18:32:07 +00:00
resin = require('resin-sdk-preconfigured')
visuals = require('resin-cli-visuals')
2015-08-13 18:50:36 +00:00
Promise.props
username: resin.auth.whoami()
email: resin.auth.getEmail()
url: resin.settings.get('resinUrl')
2015-08-13 18:50:36 +00:00
.then (results) ->
console.log visuals.table.vertical results, [
'$account information$'
'username'
'email'
'url'
2015-08-13 18:50:36 +00:00
]
.nodeify(done)