Skip to content

Commit

Permalink
Support for legacy auth token on the registry url (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavkj authored and rexxars committed Jan 17, 2020
1 parent 021fe4a commit 244b112
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions index.js
Expand Up @@ -5,6 +5,7 @@ var decodeBase64 = base64.decodeBase64
var encodeBase64 = base64.encodeBase64

var tokenKey = ':_authToken'
var legacyTokenKey = ':_auth'
var userKey = ':username'
var passwordKey = ':_password'

Expand Down Expand Up @@ -80,6 +81,11 @@ function getAuthInfoForUrl (regUrl, npmrc) {
return basicAuth
}

var basicAuthWithToken = getLegacyAuthToken(npmrc[regUrl + legacyTokenKey] || npmrc[regUrl + '/' + legacyTokenKey])
if (basicAuthWithToken) {
return basicAuthWithToken
}

return undefined
}

Expand Down Expand Up @@ -121,3 +127,14 @@ function getTokenForUsernameAndPassword (username, password) {
username: username
}
}

function getLegacyAuthToken (tok) {
if (!tok) {
return undefined
}

// check if legacy auth token is set as environment variable
var token = replaceEnvironmentVariable(tok)

return { token: token, type: 'Basic' }
}
19 changes: 19 additions & 0 deletions test/auth-token.test.js
Expand Up @@ -300,6 +300,25 @@ describe('auth-token', function () {
})
})

it('should return basic token if _auth is base64 encoded', function (done) {
var content = [
'registry=http://registry.foobar.eu/',
'//registry.foobar.eu/:_auth=' + encodeBase64('foobar:foobar')
].join('\n')

fs.writeFile(npmRcPath, content, function (err) {
var getAuthToken = requireUncached('../index')
assert(!err, err)
var token = getAuthToken()
assert.deepStrictEqual(token, {
token: 'Zm9vYmFyOmZvb2Jhcg==',
type: 'Basic'
})
assert.strictEqual(decodeBase64(token.token), 'foobar:foobar')
done()
})
})

it('should return basic token if registry url has port specified', function (done) {
var content = [
'registry=http://localhost:8770/',
Expand Down

0 comments on commit 244b112

Please sign in to comment.