mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-18 21:27:51 +00:00
Implement auth.parseCredentials()
This commit is contained in:
parent
8e2217b9d5
commit
b0fe4f41c1
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user