Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token: use figgy-config to make sure extra opts are there #149

Merged
merged 1 commit into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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