For the complete documentation index, see llms.txt. This page is also available as Markdown.

Route Middleware

Secure routes and route groups with cbsecurity middleware, declared right next to the route.

CBSecurity ships ready-made ColdBox route middleware so you can secure a route, or a whole group of routes, where it is declared. It uses the same validators, invalid actions, interception points and logging as the firewall, so you do not need security rules.

// config/Router.bx
route( "/account" ).middleware( "Authenticated@cbsecurity" ).to( "account.index" )

route( "/admin" )
    .middleware( "Authorized@cbsecurity" )
    .meta( { permissions : "ADMIN" } )
    .to( "admin.index" )

Route middleware needs ColdBox 8.2+. Group-level meta and running middleware in integration tests (execute(), get(), etc.) need ColdBox 8.3+.

The Middleware

WireBox ID
What it verifies

Authenticated@cbsecurity

The user is logged in. Route meta is ignored.

Authorized@cbsecurity

The user is logged in and satisfies the permissions and/or roles in the route meta.

JwtAuth@cbsecurity

Like Authorized, but authenticating with a JWT through the JwtAuthValidator.

BasicAuth@cbsecurity

Like Authorized, but authenticating with HTTP Basic credentials through the BasicAuthValidator.

Authenticated and Authorized use the validator configured in firewall.validator.

More Protection

These do not authenticate users. They guard routes against abuse and answer denied requests directly, with a JSON error and the right status code, without the firewall's invalid actions. Each is configured in the route meta(), with defaults in the module settings.

WireBox ID
What it does

Rate limits requests with named or inline limiters.

Requires an API key in a header or request key.

Only listed IPs and CIDR ranges get in.

Blocks listed IPs and CIDR ranges.

Redirects to HTTPS, or denies non GET requests.

Verifies a CSRF token on unsafe requests.

Catches spam bots with a hidden field.

Only lets valid, unexpired signed URLs through.

Stack them as needed. Middleware runs in the order you declare it, so put cheap checks first:

Declaring Permissions and Roles

Authorized, JwtAuth and BasicAuth read their requirements from the route meta():

Meta key
Description

permissions

One permission, a list or an array.

roles

One role, a list or an array. Any one role is enough.

mode

How permissions are verified: any (default, one is enough), all (every one is required) or none (the user must have none of them).

Securing a Group

Attach the middleware and the meta to a group() and every route inside inherits them. A route's own meta() wins over the group's.

Use middlewareGroup() to name a bundle once and reuse it:

APIs With JWT

When Access Is Denied

A denied request goes through the firewall's invalid access flow, so it behaves exactly like a firewall rule or a security annotation:

  • A guest is an authentication failure. A logged in user without the required permissions or roles is an authorization failure.

  • The firewall settings decide the response: defaultAuthenticationAction, invalidAuthenticationEvent, defaultAuthorizationAction and invalidAuthorizationEvent (redirect, override or block), including the per-module overrides.

  • The cbSecurity_onInvalidAuthentication and cbSecurity_onInvalidAuthorization interceptions are announced with annotationType set to middleware.

  • The validator results are stored in prc.cbSecurity_validatorResults and the secured URL is flashed to _securedURL.

When access is allowed, the authenticated user is stored in the prc using the authentication.prcUserVariable setting, just like the firewall does.

Firewall Rules, Annotations or Middleware?

They share the same validators and invalid actions, so choose by where you want the rule to live. When combined, the firewall rules and annotations run first, then the route middleware.

Approach
Rules live in
Best for

Config, JSON, XML, a database or a model

Central, data-driven policies and rules contributed by modules.

On the handler or action

Security that must travel with the code.

Route middleware

Next to the route

Securing routes and groups in config/Router, with no rules file.

Custom Middleware

Extend cbsecurity.models.middleware.Guard to build your own named middleware. Its constructor accepts permissions, roles, mode, validator (auth, cbauth, jwt, basic or a WireBox ID) and useMeta:

Testing

With ColdBox 8.3+, execute(), get(), post() and the other integration test helpers run route middleware, so you can test secured routes end to end:

Last updated

Was this helpful?