Skip to content

Commit 4524d17

Browse files
committed
feat(auth): add forceAuth option to force a specific auth mechanism
1 parent 0308f54 commit 4524d17

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

auth.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ function getAuth (registry, opts) {
99
opts = config(opts)
1010
let AUTH = {}
1111
const regKey = registry && registryKey(registry)
12+
if (opts.forceAuth) {
13+
opts = opts.forceAuth
14+
}
1215
const doKey = (key, alias) => addKey(opts, AUTH, regKey, key, alias)
1316
doKey('token')
1417
doKey('_authToken', 'token')

config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module.exports = figgyPudding({
1717
'fetch-retry-factor': {},
1818
'fetch-retry-maxtimeout': {},
1919
'fetch-retry-mintimeout': {},
20+
'force-auth': {},
21+
forceAuth: 'force-auth',
2022
'gid': {},
2123
'gzip': {},
2224
'headers': {},

test/auth.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,40 @@ test('token auth', t => {
7676
.then(res => t.equal(res, 'success', 'token auth succeeded'))
7777
})
7878

79+
test('forceAuth', t => {
80+
const config = {
81+
'registry': 'https://my.custom.registry/here/',
82+
'token': 'deadbeef',
83+
'always-auth': false,
84+
'//my.custom.registry/here/:_authToken': 'c0ffee',
85+
'//my.custom.registry/here/:token': 'nope',
86+
'forceAuth': {
87+
'username': 'user',
88+
'password': Buffer.from('pass', 'utf8').toString('base64'),
89+
'email': 'e@ma.il',
90+
'always-auth': true
91+
}
92+
}
93+
t.deepEqual(getAuth(config.registry, config), {
94+
alwaysAuth: true,
95+
username: 'user',
96+
password: 'pass',
97+
email: 'e@ma.il'
98+
}, 'only forceAuth details included')
99+
100+
const opts = Object.assign({}, OPTS, config)
101+
const encoded = Buffer.from(`user:pass`, 'utf8').toString('base64')
102+
tnock(t, opts.registry)
103+
.matchHeader('authorization', auth => {
104+
t.equal(auth[0], `Basic ${encoded}`, 'got encoded basic auth')
105+
return auth[0] === `Basic ${encoded}`
106+
})
107+
.get('/hello')
108+
.reply(200, '"success"')
109+
return fetch.json('/hello', opts)
110+
.then(res => t.equal(res, 'success', 'used forced auth details'))
111+
})
112+
79113
test('_auth auth', t => {
80114
const config = {
81115
'registry': 'https://my.custom.registry/here/',

0 commit comments

Comments
 (0)