2014-11-14 13:51:59 +00:00
|
|
|
async = require('async')
|
2014-11-17 18:40:32 +00:00
|
|
|
_ = require('lodash')
|
2014-11-14 13:51:59 +00:00
|
|
|
|
2014-11-26 17:02:22 +00:00
|
|
|
token = require('../token/token')
|
|
|
|
server = require('../server/server')
|
2014-12-01 14:18:39 +00:00
|
|
|
errors = require('../errors/errors')
|
2014-12-03 16:03:54 +00:00
|
|
|
settings = require('../settings')
|
2014-10-31 18:47:18 +00:00
|
|
|
|
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
|
2014-12-05 18:08:51 +00:00
|
|
|
# @param {Function} callback callback (error, token)
|
2014-12-05 17:49:33 +00:00
|
|
|
#
|
|
|
|
# @note You should use login() when possible, as it takes care of saving the token as well.
|
|
|
|
#
|
|
|
|
# @example Authenticate
|
|
|
|
# resin.auth.authenticate credentials, (error, token) ->
|
|
|
|
# throw error if error?
|
|
|
|
# console.log(token)
|
|
|
|
#
|
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) ->
|
2014-10-31 18:47:18 +00:00
|
|
|
return callback(error, response?.body)
|
|
|
|
|
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!')
|
|
|
|
#
|
2014-10-31 18:47:18 +00:00
|
|
|
exports.login = (credentials, callback) ->
|
2014-11-14 13:51:59 +00:00
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
exports.authenticate(credentials, callback)
|
|
|
|
|
|
|
|
(authToken, callback) ->
|
2014-11-26 17:02:22 +00:00
|
|
|
token.saveToken(authToken, 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
|
|
|
|
#
|
|
|
|
# @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?
|
|
|
|
#
|
|
|
|
exports.logout = (callback) ->
|
|
|
|
token.clearToken(callback)
|
|
|
|
|
|
|
|
# Parse colon separated credentials
|
|
|
|
#
|
|
|
|
# @private
|
|
|
|
#
|
|
|
|
# @param {String} colon separated credentials (username:password)
|
2014-12-05 18:08:51 +00:00
|
|
|
# @param {Function} callback callback (error, credentials)
|
2014-12-05 17:49:33 +00:00
|
|
|
#
|
|
|
|
# @example Parse credentials
|
|
|
|
# resin.auth.parseCredentials 'johndoe:secret', (error, credentials) ->
|
|
|
|
# throw error if error?
|
|
|
|
# console.log(credentials.username)
|
|
|
|
# console.log(credentials.password)
|
|
|
|
#
|
2014-11-18 16:11:20 +00:00
|
|
|
exports.parseCredentials = (credentials, callback) ->
|
2014-11-17 18:40:32 +00:00
|
|
|
result = credentials.split(':')
|
|
|
|
|
|
|
|
if result.length isnt 2
|
2014-12-01 14:18:39 +00:00
|
|
|
error = new errors.InvalidCredentials()
|
2014-11-18 16:11:20 +00:00
|
|
|
return callback?(error)
|
2014-11-17 18:40:32 +00:00
|
|
|
|
2014-11-18 16:11:20 +00:00
|
|
|
callback? null,
|
2014-11-17 18:40:32 +00:00
|
|
|
username: _.first(result)
|
|
|
|
password: _.last(result)
|