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

42 lines
1.0 KiB
CoffeeScript
Raw Normal View History

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')
settings = require('../settings')
2014-11-14 13:51:59 +00:00
exports.authenticate = (credentials, callback) ->
server.post settings.urls.authenticate, credentials, (error, response) ->
return callback(error, response?.body)
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)
# Handy aliases
2014-11-26 17:02:22 +00:00
exports.isLoggedIn = token.hasToken
exports.getToken = token.getToken
2014-11-14 13:51:59 +00:00
# TODO: Maybe we should post to /logout or something
# like that to invalidate the token on the server?
2014-11-26 17:02:22 +00:00
exports.logout = token.clearToken
2014-11-17 18:40:32 +00:00
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)