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

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-07-17 20:33:51 +02:00
// @flow
import {ErrorCode} from './utils';
import {API_ERROR} from './constants';
2018-07-17 20:33:51 +02:00
import type {RemoteUser, Package, Callback} from '@verdaccio/types';
export function allow_action(action: string) {
return function(user: RemoteUser, pkg: Package, callback: Callback) {
2018-07-15 00:30:47 +02:00
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-17 20:33:51 +02:00
authenticate(user: string, password: string, cb: Callback) {
cb(ErrorCode.getForbidden(API_ERROR.BAD_USERNAME_PASSWORD));
},
2018-07-17 20:33:51 +02:00
add_user(user: string, password: string, cb: Callback) {
return cb(ErrorCode.getConflict(API_ERROR.BAD_USERNAME_PASSWORD));
},
allow_access: allow_action('access'),
allow_publish: allow_action('publish'),
};
}