Skip to content

Commit a91f90c

Browse files
committedJan 24, 2019
feat(auth): support username:password encoded legacy _auth
1 parent fc0e119 commit a91f90c

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed
 

‎auth.js

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ function getAuth (registry, opts) {
2525
if (AUTH.password) {
2626
AUTH.password = Buffer.from(AUTH.password, 'base64').toString('utf8')
2727
}
28+
if (AUTH._auth && !(AUTH.username && AUTH.password)) {
29+
let auth = Buffer.from(AUTH._auth, 'base64').toString()
30+
auth = auth.split(':')
31+
AUTH.username = auth.shift()
32+
AUTH.password = auth.join(':')
33+
}
2834
AUTH.alwaysAuth = AUTH.alwaysAuth === 'false' ? false : !!AUTH.alwaysAuth
2935
return AUTH
3036
}

‎test/auth.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ test('_auth auth', t => {
116116
'_auth': 'deadbeef',
117117
'//my.custom.registry/here/:_auth': 'c0ffee'
118118
}
119-
t.deepEqual(getAuth(config.registry, config), {
119+
t.like(getAuth(config.registry, config), {
120120
alwaysAuth: false,
121121
_auth: 'c0ffee'
122122
}, 'correct _auth picked out')
@@ -130,6 +130,31 @@ test('_auth auth', t => {
130130
.then(res => t.equal(res, 'success', '_auth auth succeeded'))
131131
})
132132

133+
test('_auth username:pass auth', t => {
134+
const username = 'foo'
135+
const password = 'bar'
136+
const auth = Buffer.from(`${username}:${password}`, 'utf8').toString('base64')
137+
const config = {
138+
'registry': 'https://my.custom.registry/here/',
139+
'_auth': 'foobarbaz',
140+
'//my.custom.registry/here/:_auth': auth
141+
}
142+
t.like(getAuth(config.registry, config), {
143+
alwaysAuth: false,
144+
username,
145+
password,
146+
'_auth': auth
147+
}, 'correct _auth picked out')
148+
149+
const opts = Object.assign({}, OPTS, config)
150+
tnock(t, opts.registry)
151+
.matchHeader('authorization', `Basic ${auth}`)
152+
.get('/hello')
153+
.reply(200, '"success"')
154+
return fetch.json('/hello', opts)
155+
.then(res => t.equal(res, 'success', '_auth auth succeeded'))
156+
})
157+
133158
test('globally-configured auth', t => {
134159
const basicConfig = {
135160
'registry': 'https://different.registry/',
@@ -163,7 +188,7 @@ test('globally-configured auth', t => {
163188
'_auth': 'deadbeef',
164189
'//my.custom.registry/here/:_auth': 'c0ffee'
165190
}
166-
t.deepEqual(getAuth(_authConfig.registry, _authConfig), {
191+
t.like(getAuth(_authConfig.registry, _authConfig), {
167192
alwaysAuth: false,
168193
_auth: 'deadbeef'
169194
}, 'correct global _auth picked out')

0 commit comments

Comments
 (0)
Please sign in to comment.