Registry / security / authorized

authorized

JSON →
library1.0.0jsnpmunverified

Action-based authorization middleware for Express/Connect applications. Version 1.0.0 is stable with no recent releases. It provides role and entity getters to define fine-grained access control using a simple declarative syntax, and generates middleware to protect routes. Key differentiators: uses 'entity.relation' role syntax for resource-specific permissions, supports view helper for checking multiple actions, and integrates seamlessly with Express error handling.

npm install authorized
INSTALL
IMPORT
SIG · AUTHORIZED
A
authorized
securityjavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

auth (default)
const auth = require('authorized');
import auth from 'authorized'; // ESM not supported; use require()
The package does not ship ESM; use CommonJS require(). No named exports.
UnauthorizedError
const UnauthorizedError = require('authorized').UnauthorizedError;
import { UnauthorizedError } from 'authorized'; // ESM not supported
Access the error constructor via the returned module.
auth.role()
auth.role('admin', function(req, done) { done(null, !!req.user.admin); });
Role getters receive (req, done) for global roles, (entity, req, done) for entity.relation roles.
auth.entity()
auth.entity('organization', function(req, done) { done(null, org); });
Entity getters fetch the entity object based on the request (e.g., from URL params).
auth.can()
auth.can('add members to organization', 'delete organization');
Returns middleware that passes if user has at least one of the listed roles.
auth.view()
const view = auth.view(req); view.has('admin'); view.can('add members');
View object attached to request after auth.can() middleware passes. Provides has(), can(), and actions property.

Complete example of setting up roles, entities, actions, and middleware in an Express app.

const auth = require('authorized'); // Define roles auth.role('admin', function(req, done) { done(null, req.user && req.user.admin); }); auth.role('organization.owner', function(org, req, done) { if (!req.user) { done(); } else { done(null, !!~org.owners.indexOf(req.user.id)); } }); // Define entity getter auth.entity('organization', function(req, done) { const orgId = req.params.orgId; process.nextTick(function() { done(null, {id: orgId, owners: ['user.1']}); }); }); // Define actions auth.action('add members to organization', ['admin', 'organization.owner']); auth.action('delete organization', ['admin']); // Create middleware const canAddMembers = auth.can('add members to organization'); // Use in Express const express = require('express'); const app = express(); app.post('/organizations/:orgId/members', canAddMembers, function(req, res) { res.json({ message: 'member added' }); }); app.listen(3000);
Debug
Known issues
gotchaActions configuration must occur after all role and entity getters have been added.
fix
Ensure auth.role() and auth.entity() calls come before auth.action().
affects: >=1.0.0
gotchaUnauthorized actions return an UnauthorizedError; you must add Express error-handling middleware to catch it.
fix
app.use(function(err, req, res, next) { if (err instanceof UnauthorizedError) { res.status(403).send('Forbidden'); } else { next(err); } });
affects: >=1.0.0
gotchaEntity.relation role getters receive the entity as first argument; forgetting this will cause incorrect role resolution.
fix
auth.role('organization.owner', function(org, req, done) { ... });
affects: >=1.0.0
gotchaEntity getter's callback expects (error, entity); passing null for error is correct when successful.
fix
done(null, entity);
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'authorized'
Missing npm install or incorrect import path.
fix
Run `npm install authorized` in the project directory and use require('authorized').
TypeError: auth.can is not a function
auth.action() was called before all roles/entities are defined, or auth object is not initialized.
fix
Ensure auth.role() and auth.entity() are called before auth.action(), and verify require('authorized') returns the auth object.
UnauthorizedError: Not authorized
User does not have any required roles for the action.
fix
Check role getters: ensure they call done(null, true) for authorized users and done(null, false) otherwise. Also verify entity getters load the correct entity.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
42 hits · last 30 days
node
36
Resources
authorized — npm install authorized · libregistry