
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
hapi-authorization-feature
Advanced tools
hapi-authorization-feature only supports hapi 17+
ACL plugin along with features check in hapijs
You can use this plugin to add ACL and protect your routes. you can configure required roles/functions along with features/subfeatures and allow access to certain endpoints only to specific users.
Note: To use hapi-authorization-feature you must have an authentication strategy defined.
There are 2 ways to use hapi-authorization-feature:
let plugins = [
{
plugin: require('hapi-auth-basic')
},
{
plugin: require('hapi-authorization-feature')
options: {
roles: false // By setting to false, you are not using an authorization hierarchy and you do not need to specify all the potential roles here
}
}
];
await server.register(plugins);
let plugins = [
{
plugin: require('hapi-auth-basic')
},
{
plugin: require('hapi-authorization-feature'),
options: {
roles: ['OWNER', 'MANAGER', 'EMPLOYEE'],
functions:['CREATEMANAGER','CREATEEMPLOYEE','UPDATEMANAGER','UPDATEEMPLOYEE','DELETEMANAGER','DELETEEMPLOYEE'],
features:['OWNERMANAGEMENT','MANAGERMANAGEMENT','EMPLOYEEMANAGEMENT'],
subfeatures:['OWNERCREATION','OWNERUPDATION','OWNERDELETION']
}
}
];
await server.register(plugins);
If you want no routes require authorization except for the ones you specify in the route config, add hapiAuthorization instructions with the role(s) that should have access to the route configuration.
Example:
Authorize a single role or function without feature check
server.route({ method: 'GET', path: '/', options: {
plugins: {'hapiAuthorizationFeature': {role: 'ADMIN',validateFeature:false, function: 'CREATEMANAGER'}}, // Only ADMIN role
handler: (request, h) => { return "Great!"; }
}});
Authorize multiple roles with one feature
server.route({ method: 'GET', path: '/', options: {
plugins: {'hapiAuthorizationFeature': {roles: ['USER', 'ADMIN'],feature:'OWNERMANAGEMENT' ,function: 'CREATEMANAGER' }},
handler: (request, h) => { return "Great!"; }
}});
If you want all routes to require authorization except for the ones you specify that should not, add hapiAuthorization instructions with the role(s) that should have access to the server.connection options. Note that these can be overridden on each route individually as well.
Example:
let server = new Hapi.server({
routes: {
plugins: {
hapiAuthorization: { role: 'ADMIN', feature:'OWNERMANAGEMENT' ,function: 'CREATEMANAGER' ,subfeature:'OWNERCREATION' }
}
}
});
Override the authorization to require alternate roles
server.route({ method: 'GET', path: '/', options: {
plugins: {'hapiAuthorizationFeature': {role: 'USER', feature:'USERMANAGEMENT' ,function: 'CREATEUSER' ,subfeature:'USERCREATION'}}, // Only USER role
handler: (request, h) => { return "Great!" ;}
}});
Override the authorization to not require any authorization
server.route({ method: 'GET', path: '/', options: {
plugins: {'hapiAuthorizationFeature': false},
handler: (request, h) => { return "Great!"; }
}});
Note: Every route that uses hapiAuthorization must be protected by an authentication schema either via auth.strategy.default('someAuthStrategy') or by specifying the auth on the route itself.
const Hapi = require('hapi');
const modules = require('./modules');
// Instantiate the server
let server = new Hapi.Server();
/**
* The hapijs plugins that we want to use and their configs
*/
let plugins = [
{
register: require('hapi-auth-basic')
},
{
register: require('hapi-authorization-feature'),
options: {
role: 'EMPLOYEE'
}
}
];
let validate = (username, password) => {
// Perform authentication and respond with object that contains a role or an array of roles
return {username: username, role: 'EMPLOYEE'};
}
/**
* Setup the server with plugins
*/
await server.register(plugins);
server.start().then(() => {
server.auth.strategy('simple', 'basic', {validateFunc: validate});
server.auth.default('simple');
/**
* Add all the modules within the modules folder
*/
for(let route in modules) {
server.route(modules[route]);
}
/**
* Starts the server
*/
server.start()
.then(() => {
console.log('Hapi server started @', server.info.uri);
})
.catch((err) => {
console.log(err);
});
})
.catch((err) => {
// If there is an error on server startup
throw err;
});
You must define your auth strategy before defining your routes, otherwise the route validation will fail.
roles - Array|false: All the possible roles. Defaults to SUPER_ADMIN, ADMIN, USER, GUEST.functions - Array: All the possible functions of the each rolesfeatures - Array: All the possible featuressubfeatures - Array: All the possible subfeatures of each featuresrole - String: enforces that only users that have this role can access the route orfunction - String: enforces that only users that have this function can access the route andfeature - String: enforce that only users that have this feature can access the route andsubfeature - String: enforce that only users that have this subfeature can access the routeThanks [toymachiner62]:https://github.com/toymachiner62/hapi-authorization
FAQs
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.