Aviary

Protecting Aviary consoles

Use the Gate to decide who opens the durable, telescope, media and agent consoles — authorizeByRoles, authorizeByPermission, gate names, official policies and authorizeByToken.

Every Aviary console (the durable dashboard, telescope, the media dashboard, the agent governance dashboard) takes one authorize option. With AuthzModule imported, you can answer it with the Gate: roles, permissions, gate names or official policies.

imports: [
  AuthorizationModule.forRoot(),
  AuthzModule.forRoot(),
  DurableModule.forRoot({ authorize: authorizeByRoles({ roles: ['admin'] }) }),
]

Each check uses the user the authorization guard sees: the official getUser (by default request.user), else the ambient user. Put your authentication guard in the console's decorators so the user is set before authorize runs. A denied guest gets 401, a denied user 403.

By role

import { authorizeByRoles } from '@dudousxd/nestjs-authz';

TelescopeModule.forRoot({ authorize: authorizeByRoles({ roles: ['admin', 'ops'] }) })

Allows a user holding any of the roles, the same check as @Roles() (persisted roles and the user's own, superAdmin applies).

By permission

import { authorizeByPermission } from '@dudousxd/nestjs-authz';

MediaModule.forRoot({ authorize: authorizeByPermission('media.console') })

The same check as @Permission(): persisted permissions, wildcards apply (console.*).

Sending guests to the sign-in page

Without a user, both helpers answer unauthenticated, and the console shows a 401. Pass the app's sign-in path to send the guest there instead:

DurableModule.forRoot({
  authorize: authorizeByRoles({ roles: ['admin'], loginPath: '/auth/login' }),
})
TelescopeModule.forRoot({
  authorize: authorizeByPermission('telescope.view', { loginPath: '/auth/login' }),
})
  • On a console page: the browser is redirected to /auth/login?redirect=%2Fdurable, so the sign-in route can send the user back.
  • On a console API call: the response is a 401 carrying that redirectTo, and the console follows it. The way back is the page that made the call, taken from Referer (same host only).
  • Users who are signed in but lack the role or permission: they still get a 403. Redirecting them would loop.
  • redirectParam: renames the query parameter. The default is redirect.
  • loginPath: must be a same-origin path. Your sign-in route should still validate the redirect it receives.

By gate name

Pass a string and define the gate:

DurableModule.forRoot({ authorize: 'console.durable' })
@Injectable()
export class ConsoleGates implements OnModuleInit {
  constructor(private readonly gate: Gate) {}

  onModuleInit() {
    this.gate.define('console.durable', (user) => user?.isAdmin === true);
  }
}

Any string ability the Gate resolves works: an ad-hoc gate, a persisted permission, or a policy ability. A name that resolves to nothing denies the request (never a 500) and logs a warning.

By official policy

@Policy()
export class ConsolePolicy {
  view(user: User | null) {
    return user?.isStaff === true;
  }
}

AgentModule.forRoot({ authorize: { policy: ConsolePolicy, ability: 'view' } })

By token

For machines (CI, a Prometheus scraper): the request must send Authorization: Bearer <token>.

import { authorizeByToken } from '@dudousxd/nestjs-authz';

TelescopeModule.forRoot({ authorize: authorizeByToken({ token: process.env.CONSOLE_TOKEN }) })

It throws at startup when the token is empty.

Each console also accepts an inline function (ctx) => boolean, an @Injectable() class implementing ConsoleAuthorizer, or a lazy () => import('./console-access.js'). The types (ConsoleAuthorize, ConsoleAuthorizer, ConsoleAuthorizeResult, …) are exported from @dudousxd/nestjs-authz too.

On this page