Skip to content

Commit

Permalink
token: use figgy-config to make sure extra opts are there (#149)
Browse files Browse the repository at this point in the history
PR-URL: #149

Credit: @zkat
Reviewed-By: @aeschright
  • Loading branch information
zkat authored and aeschright committed Jan 30, 2019
1 parent 3663cde commit ce2c4bd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
46 changes: 35 additions & 11 deletions lib/token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'

const profile = require('libnpm/profile')
const npm = require('./npm.js')
const figgyPudding = require('figgy-pudding')
const npmConfig = require('./config/figgy-config.js')
const output = require('./utils/output.js')
const Table = require('cli-table3')
const Bluebird = require('bluebird')
Expand Down Expand Up @@ -76,22 +79,43 @@ function generateTokenIds (tokens, minLength) {
return byId
}

const TokenConfig = figgyPudding({
registry: {},
otp: {},
cidr: {},
'read-only': {},
json: {},
parseable: {}
})

function config () {
const conf = {
json: npm.config.get('json'),
parseable: npm.config.get('parseable'),
registry: npm.config.get('registry'),
otp: npm.config.get('otp')
}
let conf = TokenConfig(npmConfig())
const creds = npm.config.getCredentialsByURI(conf.registry)
if (creds.token) {
conf.auth = {token: creds.token}
conf = conf.concat({
auth: { token: creds.token }
})
} else if (creds.username) {
conf.auth = {basic: {username: creds.username, password: creds.password}}
conf = conf.concat({
auth: {
basic: {
username: creds.username,
password: creds.password
}
}
})
} else if (creds.auth) {
const auth = Buffer.from(creds.auth, 'base64').toString().split(':', 2)
conf.auth = {basic: {username: auth[0], password: auth[1]}}
conf = conf.concat({
auth: {
basic: {
username: auth[0],
password: auth[1]
}
}
})
} else {
conf = conf.concat({ auth: {} })
conf.auth = {}
}
if (conf.otp) conf.auth.otp = conf.otp
Expand Down Expand Up @@ -183,8 +207,8 @@ function rm (args) {

function create (args) {
const conf = config()
const cidr = npm.config.get('cidr')
const readonly = npm.config.get('read-only')
const cidr = conf.cidr
const readonly = conf['read-only']

const validCIDR = validateCIDRList(cidr)
return readUserInfo.password().then((password) => {
Expand Down
3 changes: 1 addition & 2 deletions test/tap/unit-token-validate-cidr.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'
const test = require('tap').test
const requireInject = require('require-inject')
const validateCIDRList = requireInject('../../lib/token.js', {'../../lib/npm.js': {}})._validateCIDRList
const validateCIDRList = require('../../lib/token.js')._validateCIDRList

test('validateCIDRList', (t) => {
t.plan(10)
Expand Down

0 comments on commit ce2c4bd

Please sign in to comment.