Examples

Example 1

import { AuthPlugin, authMiddleware, requireAuth, requireRole } from '@setu-ts/auth-plugin';

app.register(AuthPlugin({
  jwt: { secret: process.env.JWT_SECRET! },
  rbac: {
    roles: {
      admin: { permissions: ['*'], inherits: ['user'] },
      user: { permissions: ['users:read'] },
    },
  },
}));
// Priority 300 is the band ARCHITECTURE.md §10 reserves for authentication;
// a bare add() would take the kernel default of 500 and run after it.
app.middleware.add(authMiddleware(), { priority: 300 });
app.router.get('/protected', { middleware: [requireAuth()], handler });

Classes

c
MalformedPasswordHashError

Thrown by PasswordHasher.verify when the stored value is not a well-formed pbkdf2$<iterations>$<salt>$<hash> string.

  • name: string

    Discriminant for consumers that cannot use instanceof across realms.

c
MemoryAccessTokenRevocationStore(runtime: IRuntimeServices)

Single-process access-token revocation store with bounded lazy expiry work.

c
MemoryRateLimitStore(runtime: IRuntimeServices)

In-memory implementation of RateLimitStore.

c
MemoryRefreshTokenStore(runtime: IRuntimeServices)

In-memory implementation of RefreshTokenStore.

c
PasswordHasher(runtime: IRuntimeServices)

Password hasher using PBKDF2-SHA256 via Web Crypto.

c
RedisRateLimitStore(options: { url?: string | undefined; client?: IRateLimitRedisClient | undefined; runtime: IRuntimeServices; keyPrefix?: string | undefined; })

Redis-backed rate limit store implementation.

c
RefreshTokenService(options: RefreshTokenOptions)

Refresh token service implementing token rotation and revocation.

Functions

f
authMiddleware(): MiddlewareFunction

Authentication middleware that runs passive strategies and populates ctx.request.user. Always calls next() - it authenticates only, does not authorize.

f
defaultRateLimitKey(ctx: IRequestContext): string

Default rate-limit key, in order of preference:

f
publicRoute(): MiddlewareFunction

Guard that allows public access (always continues). Useful for explicitly marking routes as public when auth middleware is global.

f
requireAllPermissions(permissions: readonly string[]): MiddlewareFunction

Guard that requires all of the specified permissions. Returns 401 if no principal, 403 if any missing.

f
requireAnyRole(roles: readonly string[]): MiddlewareFunction

Guard that requires any of the specified roles. Returns 401 if no principal, 403 if none match.

f
requireAuth(): MiddlewareFunction

Guard that requires authentication. Returns 401 if no principal.

f
requirePermission(permission: string): MiddlewareFunction

Guard that requires a specific permission. Returns 401 if no principal, 403 if insufficient permission.

f
requireRole(role: string): MiddlewareFunction

Guard that requires a specific role. Returns 401 if no principal, 403 if insufficient role.

Interfaces

I
ApiKeyOptions

API key configuration options.

I
AuthPluginOptions

Auth plugin configuration options.

  • apiKey: ApiKeyOptions

    API key configuration. Optional.

  • jwt: JwtOptions

    JWT configuration. Required.

  • local: LocalOptions

    Local credentials configuration. Optional.

  • rbac: RbacConfig

    RBAC configuration. When absent, AuthPlugin registers JWT authentication only and does not provide the authorization capability.

  • session: SessionAuthOptions

    Session authentication configuration. When present, the plugin appends an internal session strategy after the API-key strategy and requires the session capability (SessionPlugin) to be registered.

  • strategies: readonly IAuthStrategy[]

    Caller-supplied strategies, appended after every built-in in declaration order. A strategy whose name collides with any other strategy in the assembled chain makes register() throw.

I
IAccessTokenRevocationStore

Store for access-token identifiers revoked before their JWT expiry.

I
IAuthorizationService

Authorization service for RBAC with role hierarchy.

I
IAuthService

Authentication service that coordinates strategies and provides credential verification for login flows.

I
IAuthStrategy

Authentication strategy interface. Implementations extract credentials from a request and return a principal, or null if the strategy does not apply.

I
IJwtService

JWT sign/verify service.

I
IPrincipal

The authenticated identity attached to a request by authentication middleware.

I
JwtOptions

JWT configuration options.

I
JwtSignOptions

Options accepted when signing a JWT.

I
LocalOptions

Local (credentials) configuration options.

I
RateLimitOptions

Options for rate limiting middleware.

I
RateLimitResult

Result of incrementing a rate limit counter.

I
RateLimitStore

Store interface for rate limiting.

I
RbacConfig

RBAC configuration for role hierarchy and permissions.

I
RefreshTokenOptions

Options for constructing a RefreshTokenService.

I
RefreshTokenRecord

A refresh token record stored on the server.

I
RefreshTokenStore

Store interface for refresh tokens.

I
RoleDefinition

Role definition for RBAC configuration.

I
SessionAuthOptions

Session authentication configuration options.

I
TokenPair

A pair of access + refresh tokens issued together.

Type Aliases

Variables

v
DEFAULT_RATE_LIMIT_EXCLUDED_PATHS: readonly PathPattern[]

The operational paths RateLimitOptions.exclude exempts by default: the framework's health, metrics and OpenAPI routes plus the interactive docs. The same six tenantMiddleware exempts, deliberately — one list to remember rather than two.

v
DEFAULT_RATE_LIMIT_KEY_PREFIX: "setu:ratelimit:"

The namespace RedisRateLimitStore prepends to every key when no keyPrefix is supplied.