Skip to content

Commit

Permalink
feat(auth): support username:password encoded legacy _auth
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Jan 24, 2019
1 parent fc0e119 commit a91f90c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ function getAuth (registry, opts) {
if (AUTH.password) {
AUTH.password = Buffer.from(AUTH.password, 'base64').toString('utf8')
}
if (AUTH._auth && !(AUTH.username && AUTH.password)) {
let auth = Buffer.from(AUTH._auth, 'base64').toString()
auth = auth.split(':')
AUTH.username = auth.shift()
AUTH.password = auth.join(':')
}
AUTH.alwaysAuth = AUTH.alwaysAuth === 'false' ? false : !!AUTH.alwaysAuth
return AUTH
}
Expand Down
29 changes: 27 additions & 2 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ test('_auth auth', t => {
'_auth': 'deadbeef',
'//my.custom.registry/here/:_auth': 'c0ffee'
}
t.deepEqual(getAuth(config.registry, config), {
t.like(getAuth(config.registry, config), {
alwaysAuth: false,
_auth: 'c0ffee'
}, 'correct _auth picked out')
Expand All @@ -130,6 +130,31 @@ test('_auth auth', t => {
.then(res => t.equal(res, 'success', '_auth auth succeeded'))
})

test('_auth username:pass auth', t => {
const username = 'foo'
const password = 'bar'
const auth = Buffer.from(`${username}:${password}`, 'utf8').toString('base64')
const config = {
'registry': 'https://my.custom.registry/here/',
'_auth': 'foobarbaz',
'//my.custom.registry/here/:_auth': auth
}
t.like(getAuth(config.registry, config), {
alwaysAuth: false,
username,
password,
'_auth': auth
}, 'correct _auth picked out')

const opts = Object.assign({}, OPTS, config)
tnock(t, opts.registry)
.matchHeader('authorization', `Basic ${auth}`)
.get('/hello')
.reply(200, '"success"')
return fetch.json('/hello', opts)
.then(res => t.equal(res, 'success', '_auth auth succeeded'))
})

test('globally-configured auth', t => {
const basicConfig = {
'registry': 'https://different.registry/',
Expand Down Expand Up @@ -163,7 +188,7 @@ test('globally-configured auth', t => {
'_auth': 'deadbeef',
'//my.custom.registry/here/:_auth': 'c0ffee'
}
t.deepEqual(getAuth(_authConfig.registry, _authConfig), {
t.like(getAuth(_authConfig.registry, _authConfig), {
alwaysAuth: false,
_auth: 'deadbeef'
}, 'correct global _auth picked out')
Expand Down

0 comments on commit a91f90c

Please sign in to comment.