1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00
verdaccio/lib/config.js

145 lines
4.5 KiB
JavaScript
Raw Normal View History

var assert = require('assert')
, crypto = require('crypto')
, minimatch = require('minimatch')
, utils = require('./utils')
2013-06-08 03:16:28 +02:00
// [[a, [b, c]], d] -> [a, b, c, d]
function flatten(array) {
2013-10-26 14:18:36 +02:00
var result = []
2013-06-08 03:16:28 +02:00
for (var i=0; i<array.length; i++) {
if (Array.isArray(array[i])) {
2013-10-26 14:18:36 +02:00
result.push.apply(result, flatten(array[i]))
2013-06-08 03:16:28 +02:00
} else {
2013-10-26 14:18:36 +02:00
result.push(array[i])
2013-06-08 03:16:28 +02:00
}
}
2013-10-26 14:18:36 +02:00
return result
2013-06-08 03:16:28 +02:00
}
function Config(config) {
2013-10-26 14:18:36 +02:00
if (!(this instanceof Config)) return new Config(config)
2013-06-08 03:16:28 +02:00
for (var i in config) {
2013-10-26 14:18:36 +02:00
if (this[i] == null) this[i] = config[i]
2013-09-25 11:29:39 +02:00
}
// some weird shell scripts are valid yaml files parsed as string
2013-10-26 14:18:36 +02:00
assert.equal(typeof(config), 'object', 'CONFIG: this doesn\'t look like a valid config file')
assert(this.storage, 'CONFIG: storage path not defined')
var users = {all:true, anonymous:true, 'undefined':true, owner:true, none:true}
2013-06-08 03:16:28 +02:00
var check_user_or_uplink = function(arg) {
2013-10-26 14:18:36 +02:00
assert(arg !== 'all' || arg !== 'owner' || arg !== 'anonymous' || arg !== 'undefined' || arg !== 'none', 'CONFIG: reserved user/uplink name: ' + arg)
assert(!arg.match(/\s/), 'CONFIG: invalid user name: ' + arg)
assert(users[arg] == null, 'CONFIG: duplicate user/uplink name: ' + arg)
users[arg] = true
}
2013-06-08 03:16:28 +02:00
2013-10-26 14:18:36 +02:00
;['users', 'uplinks', 'packages'].forEach(function(x) {
if (this[x] == null) this[x] = {}
assert(utils.is_object(this[x]), 'CONFIG: bad "'+x+'" value (object expected)')
})
2013-06-08 03:16:28 +02:00
2013-10-26 14:18:36 +02:00
for (var i in this.users) check_user_or_uplink(i)
for (var i in this.uplinks) check_user_or_uplink(i)
2013-06-08 03:16:28 +02:00
for (var i in this.users) {
2013-10-26 14:18:36 +02:00
assert(this.users[i].password, 'CONFIG: no password for user: ' + i)
2013-06-08 03:16:28 +02:00
assert(
typeof(this.users[i].password) === 'string' &&
this.users[i].password.match(/^[a-f0-9]{40}$/)
2013-10-26 14:18:36 +02:00
, 'CONFIG: wrong password format for user: ' + i + ', sha1 expected')
2013-06-08 03:16:28 +02:00
}
2013-10-26 14:18:36 +02:00
2013-06-08 03:16:28 +02:00
for (var i in this.uplinks) {
2013-10-26 14:18:36 +02:00
assert(this.uplinks[i].url, 'CONFIG: no url for uplink: ' + i)
2013-06-08 03:16:28 +02:00
assert(
typeof(this.uplinks[i].url) === 'string'
2013-10-26 14:18:36 +02:00
, 'CONFIG: wrong url format for uplink: ' + i)
this.uplinks[i].url = this.uplinks[i].url.replace(/\/$/, '')
2013-06-08 03:16:28 +02:00
}
2013-10-26 14:18:36 +02:00
2013-10-01 20:02:23 +02:00
function check_userlist(i, hash, action) {
2013-10-26 14:18:36 +02:00
if (hash[action] == null) hash[action] = []
2013-06-13 16:21:14 +02:00
2013-10-01 20:02:23 +02:00
// if it's a string, split it to array
if (typeof(hash[action]) === 'string') {
2013-10-26 14:18:36 +02:00
hash[action] = hash[action].split(/\s+/)
2013-10-01 20:02:23 +02:00
}
2013-06-13 16:21:14 +02:00
2013-10-01 20:02:23 +02:00
assert(
typeof(hash[action]) === 'object' &&
Array.isArray(hash[action])
2013-10-26 14:18:36 +02:00
, 'CONFIG: bad "'+i+'" package '+action+' description (array or string expected)')
hash[action] = flatten(hash[action])
2013-10-01 20:02:23 +02:00
hash[action].forEach(function(user) {
2013-06-08 03:16:28 +02:00
assert(
2013-10-01 20:02:23 +02:00
users[user] != null
2013-10-26 14:18:36 +02:00
, 'CONFIG: "'+i+'" package: user "'+user+'" doesn\'t exist')
})
2013-10-01 20:02:23 +02:00
}
2013-06-08 03:16:28 +02:00
2013-10-01 20:02:23 +02:00
for (var i in this.packages) {
2013-06-08 03:16:28 +02:00
assert(
typeof(this.packages[i]) === 'object' &&
!Array.isArray(this.packages[i])
2013-10-26 14:18:36 +02:00
, 'CONFIG: bad "'+i+'" package description (object expected)')
2013-09-24 08:27:27 +02:00
2013-10-26 14:18:36 +02:00
check_userlist(i, this.packages[i], 'allow_access')
check_userlist(i, this.packages[i], 'allow_publish')
check_userlist(i, this.packages[i], 'proxy_access')
check_userlist(i, this.packages[i], 'proxy_publish')
2013-09-24 08:27:27 +02:00
// deprecated
2013-10-26 14:18:36 +02:00
check_userlist(i, this.packages[i], 'access')
check_userlist(i, this.packages[i], 'proxy')
check_userlist(i, this.packages[i], 'publish')
2013-06-08 03:16:28 +02:00
}
// loading these from ENV if aren't in config
;['http_proxy', 'https_proxy', 'no_proxy'].forEach((function(v) {
if (!(v in this)) {
this[v] = process.env[v] || process.env[v.toUpperCase()]
}
}).bind(this))
2013-10-26 14:18:36 +02:00
return this
2013-06-08 03:16:28 +02:00
}
function allow_action(package, who, action) {
for (var i in this.packages) {
2013-06-14 11:27:08 +02:00
if (minimatch.makeRe(i).exec(package)) {
2013-06-08 03:16:28 +02:00
return this.packages[i][action].reduce(function(prev, curr) {
2013-10-26 14:18:36 +02:00
if (curr === String(who) || curr === 'all') return true
return prev
}, false)
2013-06-08 03:16:28 +02:00
}
}
2013-10-26 14:18:36 +02:00
return false
2013-06-08 03:16:28 +02:00
}
Config.prototype.allow_access = function(package, user) {
2013-10-26 14:18:36 +02:00
return allow_action.call(this, package, user, 'allow_access') || allow_action.call(this, package, user, 'access')
2013-06-08 03:16:28 +02:00
}
Config.prototype.allow_publish = function(package, user) {
2013-10-26 14:18:36 +02:00
return allow_action.call(this, package, user, 'allow_publish') || allow_action.call(this, package, user, 'publish')
2013-06-08 03:16:28 +02:00
}
2013-09-24 06:27:47 +02:00
Config.prototype.proxy_access = function(package, uplink) {
2013-10-26 14:18:36 +02:00
return allow_action.call(this, package, uplink, 'proxy_access') || allow_action.call(this, package, uplink, 'proxy')
2013-09-24 06:27:47 +02:00
}
Config.prototype.proxy_publish = function(package, uplink) {
2013-10-26 14:18:36 +02:00
return allow_action.call(this, package, uplink, 'proxy_publish')
2013-06-08 03:16:28 +02:00
}
Config.prototype.authenticate = function(user, password) {
2013-10-26 14:18:36 +02:00
if (this.users[user] == null) return false
return crypto.createHash('sha1').update(password).digest('hex') === this.users[user].password
2013-06-08 03:16:28 +02:00
}
2013-10-26 14:18:36 +02:00
module.exports = Config
2013-06-08 03:16:28 +02:00