Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
feat: basic auth for scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Feb 2, 2020
1 parent df7f99a commit 5c3613f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 96 deletions.
74 changes: 34 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,56 @@ function getCredentialsByURI (uri, config) {
const nerfed = toNerfDart(uri)
const defnerf = toNerfDart(config.registry)

const creds = getScopedCredentials(nerfed, `${nerfed}:`, config)
if (nerfed !== defnerf) return creds

return {
...getScopedCredentials(nerfed, '', config),
...creds
}
}

function getScopedCredentials (nerfed, scope, config) {
// hidden class micro-optimization
const c = {
scope: nerfed,
token: undefined,
password: undefined,
username: undefined,
email: undefined,
auth: undefined,
alwaysAuth: undefined
scope: nerfed
}

// used to override scope matching for tokens as well as legacy auth
if (config[`${nerfed}:always-auth`] !== undefined) {
const val = config[`${nerfed}:always-auth`]
if (config[`${scope}always-auth`] !== undefined) {
const val = config[`${scope}always-auth`]
c.alwaysAuth = val === 'false' ? false : !!val
} else if (config['always-auth'] !== undefined) {
c.alwaysAuth = config['always-auth'] === 'false'
? false : Boolean(config['always-auth'])
}

if (config[`${nerfed}:_authToken`]) {
c.token = config[`${nerfed}:_authToken`]
// the bearer token is enough, don't confuse things
// Check for bearer token
if (config[`${scope}_authToken`]) {
c.token = config[`${scope}_authToken`]
return c
}

// Handle the old-style _auth=<base64> style for the default
// registry, if set.
let {
_auth: authDef,
username: userDef,
_password: passDef
} = config
if (authDef && !(userDef && passDef)) {
authDef = Buffer.from(authDef, 'base64').toString()
// Check for basic auth token
if (config[`${scope}_auth`]) {
c.auth = config[`${scope}_auth`]
let authDef = Buffer.from(c.auth, 'base64').toString()
authDef = authDef.split(':')
userDef = authDef.shift()
passDef = authDef.join(':')
c.username = authDef.shift()
c.password = authDef.join(':')
return c
}

if (config[`${nerfed}:_password`]) {
c.password = Buffer.from(config[`${nerfed}:_password`], 'base64').toString('utf8')
} else if (nerfed === defnerf && passDef) {
c.password = passDef
// Check for username/password auth
if (config[`${scope}username`]) {
c.username = config[`${scope}username`]
}

if (config[`${nerfed}:username`]) {
c.username = config[`${nerfed}:username`]
} else if (nerfed === defnerf && userDef) {
c.username = userDef
if (config[`${scope}_password`]) {
if (scope === '') {
c.password = config[`${scope}_password`]
} else {
c.password = Buffer.from(config[`${scope}_password`], 'base64').toString('utf8')
}
}

if (config[`${nerfed}:email`]) {
c.email = config[`${nerfed}:email`]
} else if (config.email) {
c.email = config.email
if (config[`${scope}email`]) {
c.email = config[`${scope}email`]
}

if (c.username && c.password) {
Expand Down
81 changes: 25 additions & 56 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ test('credentialsByUri()', t => {
'//registry.foobar.eu/:_authToken': 'simple-token'
}), {
scope: '//registry.foobar.eu/',
token: 'simple-token',
username: undefined,
password: undefined,
email: undefined,
auth: undefined,
alwaysAuth: undefined
token: 'simple-token'
})

t.deepEqual(credentialsByUri('http://registry.foobar.eu/', {
Expand All @@ -26,12 +21,9 @@ test('credentialsByUri()', t => {
'//registry.foobar.eu/:username': 'foobar'
}), {
scope: '//registry.foobar.eu/',
token: undefined,
password: 'foobar',
username: 'foobar',
email: undefined,
auth: 'Zm9vYmFyOmZvb2Jhcg==',
alwaysAuth: undefined
auth: 'Zm9vYmFyOmZvb2Jhcg=='
})

t.end()
Expand All @@ -43,47 +35,26 @@ test('reading always-auth', t => {
'//registry.foobar.eu/:always-auth': 'true'
}), {
scope: '//registry.foobar.eu/',
token: undefined,
username: undefined,
password: undefined,
email: undefined,
auth: undefined,
alwaysAuth: true
})
t.deepEqual(credentialsByUri('http://registry.foobar.eu/', {
registry: 'http://registry.foobar.eu/',
'//registry.foobar.eu/:always-auth': 'false'
}), {
scope: '//registry.foobar.eu/',
token: undefined,
username: undefined,
password: undefined,
email: undefined,
auth: undefined,
alwaysAuth: false
})
t.deepEqual(credentialsByUri('http://registry.foobar.eu/', {
t.deepEqual(credentialsByUri('http://registry.hu/', {
registry: 'http://registry.foobar.eu/',
'always-auth': 'true'
}), {
scope: '//registry.foobar.eu/',
token: undefined,
username: undefined,
password: undefined,
email: undefined,
auth: undefined,
alwaysAuth: true
scope: '//registry.hu/'
})
t.deepEqual(credentialsByUri('http://registry.foobar.eu/', {
registry: 'http://registry.foobar.eu/',
'always-auth': 'false'
}), {
scope: '//registry.foobar.eu/',
token: undefined,
username: undefined,
password: undefined,
email: undefined,
auth: undefined,
alwaysAuth: false
})
t.end()
Expand All @@ -97,24 +68,31 @@ test('old-style _auth', t => {
_auth: auth
}), {
scope: '//registry.foobar.eu/',
token: undefined,
username: 'foo',
password: 'bar',
email: undefined,
auth,
alwaysAuth: undefined
auth
})
t.deepEqual(credentialsByUri('http://registry.hu/', {
registry: 'http://registry.foobar.eu/',
_auth: auth
}), {
scope: '//registry.hu/',
token: undefined,
username: undefined,
password: undefined,
email: undefined,
auth: undefined,
alwaysAuth: undefined
scope: '//registry.hu/'
}, 'the default old-style auth token should not be returned for non-default registry')

t.deepEqual(credentialsByUri('http://registry.foobar.eu/', {
registry: 'http://registry.foobar.eu/',
'//registry.foobar.eu/:_auth': auth
}), {
scope: '//registry.foobar.eu/',
username: 'foo',
password: 'bar',
auth
})
t.deepEqual(credentialsByUri('http://registry.hu/', {
registry: 'http://registry.foobar.eu/',
'//registry.foobar.eu/:_auth': auth
}), {
scope: '//registry.hu/'
}, 'the default old-style auth token should not be returned for non-default registry')
t.end()
})
Expand All @@ -127,30 +105,21 @@ test('username/password for the default registry', t => {
_password: 'bar'
}), {
scope: '//registry.foobar.eu/',
token: undefined,
username: 'foo',
password: 'bar',
email: undefined,
auth,
alwaysAuth: undefined
auth
})
t.deepEqual(credentialsByUri('http://registry.hu/', {
registry: 'http://registry.foobar.eu/',
username: 'foo',
_password: 'bar'
}), {
scope: '//registry.hu/',
token: undefined,
username: undefined,
password: undefined,
email: undefined,
auth: undefined,
alwaysAuth: undefined
scope: '//registry.hu/'
}, 'username/password should not be returned for non-default registry')
t.end()
})

test('email', t => {
test.skip('email', t => {
t.deepEqual(credentialsByUri('http://registry.foobar.eu/', {
registry: 'http://registry.foobar.eu/',
email: 'foo@bar.com'
Expand Down

0 comments on commit 5c3613f

Please sign in to comment.