Part of the alepha package. Import from alepha/security.
npm install alepha
Provides comprehensive authentication and authorization capabilities with JWT tokens, role-based access control, and user management.
The security module enables building secure applications using primitives like $issuer, $role, and $permission
on class properties. It offers JWT-based authentication, fine-grained permissions, service accounts, and seamless
integration with various authentication providers and user management systems.
When used with AlephaServer, this module automatically registers ServerSecurityProvider and ServerBasicAuthProvider
to protect HTTP routes and actions with JWT and Basic Auth.
Primitives are functions that define and configure various aspects of your application. They follow the convention of starting with $ and return configured primitive instances.
For more details, see the Primitives documentation.
Declares HTTP Basic Authentication for server routes. This primitive provides methods to protect routes with username/password authentication.
Create a new issuer.
An issuer is responsible for creating and verifying JWT tokens. It can be internal (with a secret) or external (with a JWKS).
Create a new permission.
Create a new role.
Allow to get an access token for a service account.
You have some options to configure the service account:
1import { $serviceAccount } from "alepha/security"; 2 3class MyService { 4 serviceAccount = $serviceAccount({ 5 oauth2: { 6 url: "https://example.com/oauth2/token", 7 clientId: "your-client-id", 8 clientSecret: "your-client-secret", 9 }10 });11 12 async fetchData() {13 const token = await this.serviceAccount.token();14 // or15 const response = await this.serviceAccount.fetch("https://api.example.com/data");16 }17}
Providers are classes that encapsulate specific functionality and can be injected into your application. They handle initialization, configuration, and lifecycle management.
For more details, see the Providers documentation.
Provides utilities for working with JSON Web Tokens (JWT).