Persist username in dataPrefix/username

This commit is contained in:
Juan Cruz Viotti 2014-12-12 09:51:23 -04:00
parent 83d28a86aa
commit 40fc9fca4f
4 changed files with 133 additions and 14 deletions

View File

@ -1,11 +1,31 @@
async = require('async')
_ = require('lodash')
_ = require('lodash-contrib')
token = require('../token/token')
server = require('../server/server')
data = require('../data/data')
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)
# Authenticate with the server
#
# @private
@ -13,19 +33,21 @@ settings = require('../settings')
# @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)
# @param {Function} callback callback (error, token, username)
#
# @note You should use login() when possible, as it takes care of saving the token as well.
# @note You should use login() when possible, as it takes care of saving the token and username as well.
#
# @example Authenticate
# resin.auth.authenticate credentials, (error, token) ->
# resin.auth.authenticate credentials, (error, token, username) ->
# throw error if error?
# console.log(token)
# console.log("My username is: #{username}")
# console.log("My token is: #{token}")
#
exports.authenticate = (credentials, callback) ->
server.post settings.get('urls.authenticate'), credentials, (error, response) ->
return callback(error) if error?
savedToken = response?.body
return callback(error, savedToken)
return callback(null, savedToken, credentials.username)
# Login to Resin.io
#
@ -49,9 +71,13 @@ exports.login = (credentials, callback) ->
(callback) ->
exports.authenticate(credentials, callback)
(authToken, callback) ->
(authToken, username, callback) ->
token.saveToken(authToken, callback)
(callback) ->
usernameKey = settings.get('keys.username')
data.setText(usernameKey, credentials.username, callback)
], callback)
# Check if you're logged in
@ -72,7 +98,8 @@ exports.isLoggedIn = (callback) ->
#
# @param {Function} callback callback (error, isLoggedIn)
#
# @note This function simply delegates to resin.token.getToken() for convenience
# @note This function simply delegates to resin.token.getToken() for convenience.
# @note This will only work if you used login() to log in.
#
# @example Get curren token
# resin.auth.getToken (error, token) ->
@ -94,7 +121,16 @@ exports.getToken = (callback) ->
# @todo Maybe we should post to /logout or something to invalidate the token on the server?
#
exports.logout = (callback) ->
token.clearToken(callback)
async.parallel([
(callback) ->
token.clearToken(callback)
(callback) ->
usernameKey = settings.get('keys.username')
data.remove(usernameKey, callback)
], _.unary(callback))
# Parse colon separated credentials
#

View File

@ -34,10 +34,11 @@ describe 'Auth:', ->
describe '#authenticate()', ->
it 'should return a token string', (done) ->
auth.authenticate johnDoeFixture.credentials, (error, token) ->
auth.authenticate johnDoeFixture.credentials, (error, token, username) ->
return done(error) if error?
expect(token).to.be.a('string')
expect(token).to.equal(johnDoeFixture.token)
expect(username).to.equal(johnDoeFixture.credentials.username)
done()
describe '#login()', ->
@ -65,6 +66,27 @@ describe 'Auth:', ->
expect(error).to.not.exist
done()
it 'should save the username', (done) ->
async.waterfall [
(callback) ->
auth.whoami(callback)
(username, callback) ->
expect(username).to.be.undefined
auth.login(johnDoeFixture.credentials, callback)
(callback) ->
auth.whoami(callback)
(username, callback) ->
expect(username).to.equal(johnDoeFixture.credentials.username)
return callback()
], (error) ->
expect(error).to.not.exist
done()
describe 'given invalid credentials', ->
beforeEach ->
@ -75,10 +97,11 @@ describe 'Auth:', ->
describe '#authenticate()', ->
it 'should return an error', (done) ->
auth.authenticate johnDoeFixture.credentials, (error, token) ->
auth.authenticate johnDoeFixture.credentials, (error, token, username) ->
expect(error).to.exist
expect(error).to.be.an.instanceof(Error)
expect(token).to.be.undefined
expect(username).to.be.undefined
done()
describe '#login()', ->
@ -90,6 +113,16 @@ describe 'Auth:', ->
expect(token).to.be.undefined
done()
describe 'given a not logged in user', ->
describe '#whoami()', ->
it 'should return undefined', (done) ->
auth.whoami (error, username) ->
expect(error).to.not.exist
expect(username).to.be.undefined
done()
describe 'given a logged in user', ->
beforeEach (done) ->
@ -103,6 +136,14 @@ describe 'Auth:', ->
auth.login(johnDoeFixture.credentials, done)
describe '#whoami()', ->
it 'should return the username', (done) ->
auth.whoami (error, username) ->
expect(error).to.not.exist
expect(username).to.equal(johnDoeFixture.credentials.username)
done()
describe '#login()', ->
it 'should override the old user', (done) ->
@ -160,11 +201,49 @@ describe 'Auth:', ->
(isLoggedIn, callback) ->
expect(isLoggedIn).to.be.false
return callback()
], (error) ->
expect(error).to.not.exist
done()
it 'should clear the token', (done) ->
async.waterfall [
(callback) ->
auth.getToken(callback)
(token, callback) ->
expect(token).to.be.undefined
return callback(null)
(savedToken, callback) ->
expect(savedToken).to.be.a.string
auth.logout(callback)
(callback) ->
auth.getToken(callback)
(savedToken, callback) ->
expect(savedToken).to.be.undefined
return callback()
], (error) ->
expect(error).to.not.exist
done()
it 'should clear the username', (done) ->
async.waterfall [
(callback) ->
auth.whoami(callback)
(username, callback) ->
expect(username).to.be.a.string
auth.logout(callback)
(callback) ->
auth.whoami(callback)
(username, callback) ->
expect(username).to.be.undefined
return callback()
], (error) ->
expect(error).to.not.exist

View File

@ -19,6 +19,9 @@ settings =
localConfig: '.resinconf'
keys:
username: 'username'
files:
config: 'config'

View File

@ -1,6 +1,7 @@
data = require('../data/data')
# @nodoc
# TODO: Move to settings
TOKEN_KEY = 'token'
# Save token