
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
@iamjs/core
Advanced tools
This package contains the core logic and interfaces for the authorization library with End-to-end typesafety
@iamjs/core
iamjs is a powerful and flexible library for implementing role-based access control (RBAC) in JavaScript applications. The core package (@iamjs/core) provides essential functionality for managing roles, permissions, and authorization in your application. With iamjs, you can easily define complex permission structures, manage user roles, and enforce access control throughout your application.
You can install the iamjs core library using your preferred package manager:
npm install @iamjs/core
# or
yarn add @iamjs/core
# or
pnpm add @iamjs/core
# or
bun add @iamjs/core
Create new roles using the Role class. You can define base permissions using CRUD notation and add custom permissions as needed.
import { Role } from '@iamjs/core';
// Create a basic user role
const userRole = new Role({
name: 'user',
description: 'Standard user with limited permissions',
config: {
user: {
base: '-r--l', // Can only read and list users
custom: {
updateOwnProfile: true
}
},
post: {
base: 'crud-', // Can create, read, update, but not delete posts
custom: {
publish: true,
unpublish: true
}
},
comment: {
base: 'crudl', // Full access to comments
custom: {
report: true
}
}
}
});
// Create an admin role with full permissions
const adminRole = new Role({
name: 'admin',
description: 'Administrator with full access',
config: {
user: {
base: 'crudl',
custom: {
ban: true,
unban: true
}
},
post: {
base: 'crudl',
custom: {
publish: true,
unpublish: true,
feature: true
}
},
comment: {
base: 'crudl',
custom: {
delete: true,
restore: true
}
}
}
});
In these examples, we've created two roles: a user role with limited permissions and an admin role with full access. The base permissions use CRUD notation (Create, Read, Update, Delete, List), where a dash (-) indicates the absence of that permission.
You can extend or update role permissions using the add and update methods:
// Add a new resource to the user role
const extendedUserRole = userRole.add({
resource: 'category',
permissions: {
base: '-r--l', // Users can only read and list categories
custom: {
subscribe: true,
unsubscribe: true
}
}
});
// Update existing permissions for the admin role
const updatedAdminRole = adminRole.update({
resource: 'user',
permissions: {
base: 'crudl',
custom: {
ban: true,
unban: true,
promoteToModerator: true // New custom permission
}
}
});
// Remove a resource from a role
const reducedUserRole = userRole.remove({
resource: 'comment'
});
These methods allow you to dynamically adjust permissions as your application evolves.
The Role class provides several methods to check permissions:
// Check if a role can perform a specific action on a resource
console.log(userRole.can('post', 'create')); // true
console.log(userRole.can('user', 'delete')); // false
// Check if a role cannot perform a specific action
console.log(userRole.cannot('user', 'ban')); // true
// Check if a role can perform any of the specified actions
console.log(userRole.canAny('post', ['create', 'delete'])); // true
// Check if a role can perform all of the specified actions
console.log(userRole.canAll('post', ['create', 'read', 'update', 'delete'])); // false
These methods make it easy to implement fine-grained access control in your application logic.
Use the AuthManager class for centralized authorization management:
import { AuthManager, Schema } from '@iamjs/core';
// Create a schema with multiple roles
const schema = new Schema({
roles: {
user: userRole,
admin: adminRole,
moderator: moderatorRole // Assuming you've defined this role
}
});
// Initialize the AuthManager with the schema
const auth = new AuthManager(schema);
// Check authorization for different scenarios
console.log(auth.authorize({
role: 'user',
actions: ['read', 'create'],
resources: 'post'
})); // true
console.log(auth.authorize({
role: 'user',
actions: ['delete'],
resources: 'post'
})); // false
console.log(auth.authorize({
role: 'admin',
actions: ['ban'],
resources: 'user'
})); // true
// Check multiple resources at once
console.log(auth.authorize({
role: 'moderator',
actions: ['update', 'delete'],
resources: ['post', 'comment'],
strict: true // Requires permission for all specified resources
})); // Depends on moderator role definition
The AuthManager provides a convenient way to check permissions across different roles and resources in your application.
constructor(options: RoleOptions)add(options: AddOptions): Roleupdate(options: UpdateOptions): Roleremove(options: RemoveOptions): Rolecan(resource: string, action: string): booleancannot(resource: string, action: string): booleancanAny(resource: string, actions: string[]): booleancanAll(resource: string, actions: string[]): booleantoObject(): objecttoJSON(): stringstatic fromObject(obj: object): Rolestatic fromJSON(json: string): Rolestatic from(data: any, transform: Function): Roleconstructor(schema: Schema)authorize(request: AuthorizationRequest): booleanconstructor(options: SchemaOptions)getRole(name: string): Role | undefinedhasRole(name: string): booleanaddRole(role: Role): voidremoveRole(name: string): voidConvert roles to and from different formats for storage or transmission:
// Convert a role to JSON
const jsonString = userRole.toJSON();
console.log(jsonString);
// {"name":"user","description":"Standard user with limited permissions","config":{"user":{"base":"-r--l","custom":{"updateOwnProfile":true}},...}}
// Create a role from JSON
const recreatedUserRole = Role.fromJSON(jsonString);
// Convert a role to a plain object
const objectRepresentation = adminRole.toObject();
console.log(objectRepresentation);
// {name: "admin", description: "Administrator with full access", config: {...}}
// Create a role from an object
const recreatedAdminRole = Role.fromObject(objectRepresentation);
Use the from method for creating roles from custom data sources, such as encrypted data:
import crypto from 'crypto';
import { Role, GetRoleConfig } from '@iamjs/core';
// Assume we have these encryption functions defined
const encrypt = (data: string) => { /* ... */ };
const decrypt = (data: string) => { /* ... */ };
// Encrypt role data for storage
const encryptedRoleData = encrypt(userRole.toJSON());
// Later, recreate the role from encrypted data
const decryptedRole = Role.from(encryptedRoleData, (data) => {
const decrypted = decrypt(data);
return Role.fromJSON(decrypted).toObject() as GetRoleConfig<typeof userRole>;
});
console.log(decryptedRole.can('post', 'create')); // true
This approach allows you to securely store and transmit role data while maintaining the ability to recreate functional Role instances.
We welcome contributions to iamjs! If you'd like to contribute, please follow these steps:
Please see our Contributing Guide for more detailed information.
iamjs is released under the MIT License. See the LICENSE file for more details.
FAQs
This package contains the core logic and interfaces for the authorization library with End-to-end typesafety
We found that @iamjs/core demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.