Security Decorators
Define TypeScript authentication, authorization, permissions, policies, rate limits, CSRF protection, input sanitization, IP restrictions, and identity requirements with AxilJS.
Security Decorators
AxilJS security decorators declare access-control and security requirements as semantic metadata.
Instead of implementing authorization logic directly inside application methods, you describe the security requirement and let the appropriate security consumer enforce it.
Security decorators cover:
- Authentication
- Roles and permissions
- Authorization policies
- Rate limiting
- CSRF protection
- Input sanitization
- IP allowlists
- Verified identities
@tAuth
tAuth declares that an operation requires an authenticated caller.
Authentication requirements can be configured for specific authentication schemes or stronger authentication requirements:
Options
| Option | Type | Description |
|---|---|---|
required | boolean | Whether authentication is required |
schemes | string[] | Accepted authentication schemes |
mfa | boolean | Require multi-factor authentication |
By default, required is true.
@tRole
tRole restricts an operation to callers with the specified role.
Multiple roles can be declared:
Use roles for coarse-grained access control.
For resource-level or relationship-based authorization, use @tPolicy.
@tPermission
tPermission defines fine-grained permissions required to execute an operation.
Multiple permissions can be required:
Permissions are useful when role-based access control is too broad for the application's authorization model.
@tPolicy
tPolicy references a named authorization policy.
Use policies when authorization depends on application context rather than a simple role or permission.
Another example:
The policy consumer evaluates the named policy against the current execution context.
Typical policy inputs may include:
- Current user
- Resource being accessed
- Tenant
- User roles
- User permissions
- Resource ownership
- Organization membership
The policy determines whether the operation is allowed.
@tRateLimit
tRateLimit constrains how frequently an operation can execute.
The rate limit can be scoped to a specific identity or request attribute:
Options
| Option | Type | Description |
|---|---|---|
limit | number | Maximum number of executions |
window | string | Time window such as 10s, 1m, or 1h |
key | string | Rate-limit grouping key such as ip, user, or tenant |
Rate limiting can be particularly useful for:
- Authentication endpoints
- Public APIs
- Search endpoints
- Expensive operations
- Tenant-specific API limits
@tCsrf
tCsrf declares that an operation requires a valid Cross-Site Request Forgery (CSRF) token.
Use this for state-changing operations where CSRF protection is required.
The security consumer is responsible for validating the token according to the application's configured CSRF mechanism.
@tSanitize
tSanitize declares input sanitization requirements.
Options
| Option | Type | Description |
|---|---|---|
stripHtml | boolean | Remove HTML markup |
trim | boolean | Trim leading and trailing whitespace |
normalizeWhitespace | boolean | Collapse repeated whitespace |
Sanitization should complement validation rather than replace it.
For schema validation, use the validation decorators.
@tIpAllowList
tIpAllowList restricts access to configured IP addresses or CIDR ranges.
This is useful for endpoints intended for:
- Internal services
- Private infrastructure
- Administrative operations
- Trusted network environments
IP-based restrictions should generally be treated as an additional security boundary, not the sole authorization mechanism.
@tVerifiedEmail
tVerifiedEmail requires the authenticated user's email address to be verified.
This is useful when an operation requires a verified account identity.
Composing Security Requirements
Security decorators are designed to be composed.
For example:
Each declaration represents a separate security or operational requirement:
| Decorator | Responsibility |
|---|---|
tAuth | Authentication |
tRole | Role-based authorization |
tRateLimit | Request frequency control |
tIpAllowList | Network restriction |
tAudit | Security and compliance auditing |
The decorators remain independent. The security runtime can evaluate the relevant metadata without requiring the decorators themselves to know how authentication, authorization, or rate limiting is implemented.
Security Metadata
Security decorators write their declarations to the AxilJS metadata registry.
A security consumer can then inspect the metadata and enforce the required constraints:
This separates the declaration of a security requirement from its implementation.
Complete Example
A protected API operation can combine HTTP, authentication, authorization, rate limiting, and auditing:
The business method remains focused on the operation itself while its security requirements remain explicit and machine-readable.
Security Decorator Reference
| Decorator | Purpose |
|---|---|
tAuth | Require authentication |
tRole | Require one or more roles |
tPermission | Require specific permissions |
tPolicy | Apply a named authorization policy |
tRateLimit | Limit execution frequency |
tCsrf | Require CSRF protection |
tSanitize | Declare input sanitization |
tIpAllowList | Restrict access by IP or CIDR |
tVerifiedEmail | Require a verified email identity |
Security and Other Decorators
Security decorators can be combined with decorators from other AxilJS domains.
For example:
This allows the complete operational contract of an endpoint to remain visible in its declarations while implementation details remain inside independent runtime consumers.