diff --git a/lib/cli-modules/auth/auth.coffee b/lib/cli-modules/auth/auth.coffee index 6209742a..73d2fd8a 100644 --- a/lib/cli-modules/auth/auth.coffee +++ b/lib/cli-modules/auth/auth.coffee @@ -1,4 +1,5 @@ async = require('async') +_ = require('lodash') server = require('../../server/server') token = require('../../token/token') @@ -25,3 +26,14 @@ exports.getToken = token.getToken # TODO: Maybe we should post to /logout or something # like that to invalidate the token on the server? exports.logout = token.clearToken + +exports.parseCredentials = (credentials) -> + result = credentials.split(':') + + if result.length isnt 2 + throw new Error('Invalid credentials') + + return { + username: _.first(result) + password: _.last(result) + } diff --git a/lib/cli-modules/auth/auth.spec.coffee b/lib/cli-modules/auth/auth.spec.coffee index 9b2bb855..63ef7637 100644 --- a/lib/cli-modules/auth/auth.spec.coffee +++ b/lib/cli-modules/auth/auth.spec.coffee @@ -1,5 +1,6 @@ expect = require('chai').expect nock = require('nock') +_ = require('lodash') async = require('async') auth = require('./auth') config = require('../../config') @@ -160,3 +161,30 @@ describe 'Auth:', -> ], (error) -> expect(error).to.not.exist done() + + describe '#parseCredentials', -> + + describe 'given colon separated credentials', -> + + username = null + password = null + + beforeEach -> + username = 'johndoe' + password = 'mysecret' + + it 'should parse the credentials correctly', -> + parsedCredentials = auth.parseCredentials("#{username}:#{password}") + expect(parsedCredentials.username).to.equal(username) + expect(parsedCredentials.password).to.equal(password) + + it 'should throw an error if it has two or more colons', -> + parseFunction = _.partial(auth.parseCredentials, "#{username}:#{password}:#{username}") + expect(parseFunction).to.throw(Error) + + parseFunction = _.partial(auth.parseCredentials, "#{username}:#{password}:#{username}:#{password}") + expect(parseFunction).to.throw(Error) + + it 'should throw an error if only the username is passed', -> + parseFunction = _.partial(auth.parseCredentials, username) + expect(parseFunction).to.throw(Error)