Skip to Content
HeronJS v4 is now available.
Versionv4SecurityAuthorization

Authorization

GateKeeper resolves credentials into a security context. @Guard evaluates requirements against that identity. In 4.7.0 the container and authentication registrations belong to each application.

Register authentication

Apply @GateKeeper({context: AuthContext, resolver: authResolver}) to the application module. The context implements SecureContext; its OnGuard maps verified authentication data into a SecureProperty. The authResolver object implements AuthResolver, selects credentials (for example, the authorization header), and validates them before returning an identity.

Use the exported GATEKEEPER and GATEKEEPER_RESOLVER token identities when registering these dependencies explicitly. Reject missing or invalid credentials before assigning privileged roles.

Protect a route

import 'reflect-metadata'; import {Guard} from '@heronjs/common'; import {Rest, Get} from '@heronjs/express'; const adminOnly = Guard({roles: ['admin']}); @Rest('/reports') export class ReportsController { @Get({uri: '/'}) @adminOnly list() { return []; } }

Guard returns a typed class/method decorator in 4.7.0; no decorator cast is needed.

Register the controller and authentication configuration in your module. Both class and method guards apply. Evaluation awaits promised requirements and asynchronous or Observable predicates. Missing identity, mismatched requirements, empty predicates and failed evaluation deny access.

HTTP exposes the resolved identity as request.authContext, accessible with @Principal(). WebSocket, MCP and ACP have their own authentication options; see their guides for connection and endpoint boundaries. MCP token verification and ACP application authentication retain their protocol-specific contracts.

Repeated and inherited policies

Class and method guards both apply. Repeated @Guard decorators use AND semantics, including multiple requirements on the same claim. Subclass guards add to inherited policies; they cannot replace a parent’s restrictions. Custom WebSocket/MCP authorizers receive each policy separately and must return exactly true for every policy.

@Guard({ role: 'admin' }) class AdminController { @Guard({ active: true }) @Guard({ scopes: ['orders:write'] }) update() { /* All three policies must pass. */ } }

Guard policies must be nonempty plain objects containing own enumerable string data properties. Missing/null policies, empty policies, inherited/accessor/symbol requirements, empty role arrays, and unsupported requirement values are rejected at declaration or registration. For authentication-only access, require a verified claim such as @Guard({sub: value => typeof value === 'string' && value.length > 0}); do not use @Guard({}). Policy objects and direct requirement arrays are copied and frozen when declared, so changing a shared configuration object cannot weaken registered guards. Empty-string method names are guarded normally.

Shared evaluation awaits promised requirements and predicate results, supports Observable predicates, and denies missing identity, inherited identity claims, array identities, mismatches, empty predicates, unsupported resolved requirements, and failed evaluation. Predicates must produce the boolean true; truthy strings or numbers do not grant access.

Authentication is application-wide for HTTP, GraphQL, and tRPC. Imported modules must use the same gatekeeper; conflicting gatekeeper constructors now fail startup instead of choosing whichever module was visited first. Modules sharing a gatekeeper must also share its resolver instance; conflicting resolver declarations are rejected rather than overwriting authentication policy. Use separate applications for distinct authentication domains. WebSocket and MCP retain their endpoint authentication options.

GraphQL authenticates once per field and checks every field/class policy against that identity. Header, cookie, and session credentials all pass through the configured resolver.resolve() before OnGuard; session credentials follow the same string-value contract as the other credential sources. Cancellation interrupts pending resolver authentication.

Last updated on