balena-cli/lib/resin/auth/auth.coffee

134 lines
3.6 KiB
CoffeeScript
Raw Normal View History

2014-11-14 13:51:59 +00:00
async = require('async')
_ = require('lodash-contrib')
2014-11-14 13:51:59 +00:00
2014-12-22 18:43:38 +00:00
token = require('./token/token')
2014-12-22 19:42:39 +00:00
server = require('../_server/server')
data = require('../data/data')
2014-12-22 19:31:11 +00:00
errors = require('../_errors/errors')
settings = require('../settings')
# Return current logged in username
#
# @param {Function} callback callback (error, username)
#
# @note This will only work if you used login() to log in.
#
# @example Who am I?
# resin.auth.whoami (error, username) ->
# throw error if error?
#
# if not username?
# console.log('I\'m not logged in!')
# else
# console.log("My username is: #{username}")
#
exports.whoami = (callback) ->
usernameKey = settings.get('keys.username')
data.getText(usernameKey, callback)
2014-12-05 17:49:33 +00:00
# Authenticate with the server
#
# @private
#
# @param {Object} credentials in the form of username, password
# @option credentials {String} username the username
# @option credentials {String} password user password
# @param {Function} callback callback (error, token, username)
2014-12-05 17:49:33 +00:00
#
# @note You should use login() when possible, as it takes care of saving the token and username as well.
2014-12-05 17:49:33 +00:00
#
# @example Authenticate
# resin.auth.authenticate credentials, (error, token, username) ->
2014-12-05 17:49:33 +00:00
# throw error if error?
# console.log("My username is: #{username}")
# console.log("My token is: #{token}")
2014-12-05 17:49:33 +00:00
#
2014-11-14 13:51:59 +00:00
exports.authenticate = (credentials, callback) ->
2014-12-05 14:53:59 +00:00
server.post settings.get('urls.authenticate'), credentials, (error, response) ->
return callback(error) if error?
2014-12-08 14:16:37 +00:00
savedToken = response?.body
return callback(null, savedToken, credentials.username)
2014-12-05 17:49:33 +00:00
# Login to Resin.io
#
# Is the login is successful, the token is persisted between sessions.
#
# @param {Object} credentials in the form of username, password
# @option credentials {String} username the username
# @option credentials {String} password user password
2014-12-05 18:08:51 +00:00
# @param {Function} callback callback (error)
2014-12-05 17:49:33 +00:00
#
# @note This function saves the token to the directory configured in dataPrefix
#
# @example Login to Resin.io
# resin.auth.login credentials, (error) ->
# throw error if error?
# console.log('I\'m logged in!')
#
exports.login = (credentials, callback) ->
2014-11-14 13:51:59 +00:00
async.waterfall([
(callback) ->
exports.authenticate(credentials, callback)
(authToken, username, callback) ->
2014-11-26 17:02:22 +00:00
token.saveToken(authToken, callback)
2014-11-14 13:51:59 +00:00
(callback) ->
usernameKey = settings.get('keys.username')
data.setText(usernameKey, credentials.username, callback)
2014-11-14 13:51:59 +00:00
], callback)
2014-12-05 17:49:33 +00:00
# Check if you're logged in
#
2014-12-05 18:08:51 +00:00
# @param {Function} callback callback (isLoggedIn)
2014-12-05 17:49:33 +00:00
#
# @example Check if logged in
# resin.auth.isLoggedIn (isLoggedIn) ->
# if isLoggedIn
# console.log('I\'m in!')
# else
# console.log('Too bad!')
#
exports.isLoggedIn = (callback) ->
token.hasToken(callback)
2014-11-14 13:51:59 +00:00
2014-12-05 17:49:33 +00:00
# Get current logged in user's token
#
2014-12-05 18:08:51 +00:00
# @param {Function} callback callback (error, isLoggedIn)
2014-12-05 17:49:33 +00:00
#
# @note This function simply delegates to resin.token.getToken() for convenience.
# @note This will only work if you used login() to log in.
2014-12-05 17:49:33 +00:00
#
# @example Get curren token
# resin.auth.getToken (error, token) ->
# throw error if error?
# console.log(token)
#
exports.getToken = (callback) ->
token.getToken(callback)
2014-11-17 18:40:32 +00:00
2014-12-05 17:49:33 +00:00
# Logout from Resin.io
#
2014-12-05 18:08:51 +00:00
# @param {Function} callback callback (error)
2014-12-05 17:49:33 +00:00
#
# @example Logout from Resin.io
# resin.auth.logout (error) ->
# throw error if error?
# console.log('I\'m out!')
#
# @todo Maybe we should post to /logout or something to invalidate the token on the server?
#
2014-12-12 14:25:32 +00:00
exports.logout = (callback = _.noop) ->
async.parallel([
(callback) ->
token.clearToken(callback)
(callback) ->
usernameKey = settings.get('keys.username')
data.remove(usernameKey, callback)
], _.unary(callback))