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-11-14 13:51:59 +00:00
|
|
|
exports.authenticate = (credentials, callback) ->
|
2014-12-03 16:03:54 +00:00
|
|
|
server.post settings.urls.authenticate, credentials, (error, response) ->
|
2014-10-31 18:47:18 +00:00
|
|
|
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)
|