Skip to content

Commit

Permalink
feat(auth): add forceAuth option to force a specific auth mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 22, 2018
1 parent 0308f54 commit 4524d17
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ function getAuth (registry, opts) {
opts = config(opts)
let AUTH = {}
const regKey = registry && registryKey(registry)
if (opts.forceAuth) {
opts = opts.forceAuth
}
const doKey = (key, alias) => addKey(opts, AUTH, regKey, key, alias)
doKey('token')
doKey('_authToken', 'token')
Expand Down
2 changes: 2 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = figgyPudding({
'fetch-retry-factor': {},
'fetch-retry-maxtimeout': {},
'fetch-retry-mintimeout': {},
'force-auth': {},
forceAuth: 'force-auth',
'gid': {},
'gzip': {},
'headers': {},
Expand Down
34 changes: 34 additions & 0 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,40 @@ test('token auth', t => {
.then(res => t.equal(res, 'success', 'token auth succeeded'))
})

test('forceAuth', t => {
const config = {
'registry': 'https://my.custom.registry/here/',
'token': 'deadbeef',
'always-auth': false,
'//my.custom.registry/here/:_authToken': 'c0ffee',
'//my.custom.registry/here/:token': 'nope',
'forceAuth': {
'username': 'user',
'password': Buffer.from('pass', 'utf8').toString('base64'),
'email': 'e@ma.il',
'always-auth': true
}
}
t.deepEqual(getAuth(config.registry, config), {
alwaysAuth: true,
username: 'user',
password: 'pass',
email: 'e@ma.il'
}, 'only forceAuth details included')

const opts = Object.assign({}, OPTS, config)
const encoded = Buffer.from(`user:pass`, 'utf8').toString('base64')
tnock(t, opts.registry)
.matchHeader('authorization', auth => {
t.equal(auth[0], `Basic ${encoded}`, 'got encoded basic auth')
return auth[0] === `Basic ${encoded}`
})
.get('/hello')
.reply(200, '"success"')
return fetch.json('/hello', opts)
.then(res => t.equal(res, 'success', 'used forced auth details'))
})

test('_auth auth', t => {
const config = {
'registry': 'https://my.custom.registry/here/',
Expand Down

0 comments on commit 4524d17

Please sign in to comment.