Move parseCredentials() to helpers

This commit is contained in:
Juan Cruz Viotti 2014-12-22 15:48:11 -04:00
parent 68a6da9ffe
commit 8d08affcd8
5 changed files with 43 additions and 57 deletions

View File

@ -6,13 +6,14 @@ ui = require('../ui')
log = require('../log/log')
errors = require('../errors/errors')
permissions = require('../permissions/permissions')
helpers = require('../helpers/helpers')
exports.login = (params) ->
async.waterfall [
(callback) ->
if params.credentials?
return resin.auth.parseCredentials(params.credentials, callback)
return helpers.parseCredentials(params.credentials, callback)
else
return ui.widgets.login(callback)

View File

@ -24,3 +24,14 @@ exports.readStdin = (callback) ->
stdin.on 'end', ->
result = result.join()
return callback(null, result)
exports.parseCredentials = (credentials, callback) ->
result = credentials.split(':')
if result.length isnt 2
error = new Error('Invalid credentials')
return callback?(error)
callback? null,
username: _.first(result)
password: _.last(result)

View File

@ -33,3 +33,33 @@ describe 'Helpers:', ->
expect(error).to.not.exist
expect(isValid).to.be.false
done()
describe '#parseCredentials', ->
describe 'given colon separated credentials', ->
username = null
password = null
beforeEach ->
username = 'johndoe'
password = 'mysecret'
it 'should parse the credentials correctly', (done) ->
helpers.parseCredentials "#{username}:#{password}", (error, credentials) ->
expect(error).to.not.exist
expect(credentials.username).to.equal(username)
expect(credentials.password).to.equal(password)
done()
it 'should throw an error if it has two or more colons', (done) ->
helpers.parseCredentials "#{username}:#{password}:#{username}", (error, credentials) ->
expect(error).to.be.an.instanceof(Error)
expect(credentials).to.not.exist
done()
it 'should throw an error if only the username is passed', (done) ->
helpers.parseCredentials username, (error, credentials) ->
expect(error).to.be.an.instanceof(Error)
expect(credentials).to.not.exist
done()

View File

@ -131,29 +131,3 @@ exports.logout = (callback = _.noop) ->
data.remove(usernameKey, callback)
], _.unary(callback))
# Parse colon separated credentials
#
# @private
#
# @param {String} colon separated credentials (username:password)
# @param {Function} callback callback (error, credentials)
#
# @todo This should be moved somewhere else, as it only used by the auth actions
#
# @example Parse credentials
# resin.auth.parseCredentials 'johndoe:secret', (error, credentials) ->
# throw error if error?
# console.log(credentials.username)
# console.log(credentials.password)
#
exports.parseCredentials = (credentials, callback) ->
result = credentials.split(':')
if result.length isnt 2
error = new errors.InvalidCredentials()
return callback?(error)
callback? null,
username: _.first(result)
password: _.last(result)

View File

@ -251,33 +251,3 @@ describe 'Auth:', ->
it 'should not throw an error if callback is not passed', ->
expect(auth.logout).to.not.throw(Error)
describe '#parseCredentials', ->
describe 'given colon separated credentials', ->
username = null
password = null
beforeEach ->
username = 'johndoe'
password = 'mysecret'
it 'should parse the credentials correctly', (done) ->
auth.parseCredentials "#{username}:#{password}", (error, credentials) ->
expect(error).to.not.exist
expect(credentials.username).to.equal(username)
expect(credentials.password).to.equal(password)
done()
it 'should throw an error if it has two or more colons', (done) ->
auth.parseCredentials "#{username}:#{password}:#{username}", (error, credentials) ->
expect(error).to.be.an.instanceof(Error)
expect(credentials).to.not.exist
done()
it 'should throw an error if only the username is passed', (done) ->
auth.parseCredentials username, (error, credentials) ->
expect(error).to.be.an.instanceof(Error)
expect(credentials).to.not.exist
done()