0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-03 23:09:17 -05:00
verdaccio/src/lib/auth-utils.js

35 lines
983 B
JavaScript
Raw Normal View History

import {ErrorCode} from './utils';
import {API_ERROR} from './constants';
export function allow_action(action) {
2018-07-15 00:30:47 +02:00
return function(user, pkg, callback) {
const {name, groups} = user;
const hasPermission = pkg[action].some((group) => name === group || groups.includes(group));
2018-07-15 00:30:47 +02:00
if (hasPermission) {
return callback(null, true);
}
2018-07-15 00:30:47 +02:00
if (name) {
callback(ErrorCode.getForbidden(`user ${name} is not allowed to ${action} package ${pkg.name}`));
} else {
2018-07-15 00:30:47 +02:00
callback(ErrorCode.getForbidden(`unregistered users are not allowed to ${action} package ${pkg.name}`));
}
};
}
export function getDefaultPlugins() {
return {
2018-07-15 00:47:47 +02:00
authenticate(user, password, cb) {
cb(ErrorCode.getForbidden(API_ERROR.BAD_USERNAME_PASSWORD));
},
2018-07-15 00:47:47 +02:00
add_user(user, password, cb) {
return cb(ErrorCode.getConflict(API_ERROR.BAD_USERNAME_PASSWORD));
},
allow_access: allow_action('access'),
allow_publish: allow_action('publish'),
};
}