Skip to main content

IAuthorizationProvider

Package: @nauth-toolkit/core Type: Interface

Decides whether a privileged (admin) operation may proceed. Consulted by AdminAuthService, MFAService, ApiKeyService and AuthAuditService before each admin method runs. See Authorization for how to write one.

import type { IAuthorizationProvider, AuthorizationContext, AuthorizationDecision, AuthAction } from '@nauth-toolkit/nestjs';

Methods​

MethodReturnsDescription
authorize(context)Promise<AuthorizationDecision> | AuthorizationDecisionDecide whether context.action may proceed. Throwing is treated as a denial.

AuthorizationContext​

Passed to authorize().

PropertyTypeDescription
actionAuthActionThe operation being attempted
actorIUser | undefinedThe authenticated caller. Undefined outside a request; such calls are denied unless wrapped in runAsSystem()
requestNAuthRequest | undefinedThe active request, when one exists
targetSubstring | undefinedsub of the user being acted upon. Absent for non-targeted actions such as admin.user.list
viaApiKeyboolean | undefinedTrue when the caller authenticated with an API key rather than a session

AuthorizationDecision​

Returned from authorize().

PropertyTypeDescription
allowbooleanWhether the action may proceed
reasonstring | undefinedSurfaced as the FORBIDDEN message and stored on the AUTHORIZATION_DENIED audit record

AuthAction​

A closed union of 26 string literals naming each privileged operation independently of any route: admin.user.*, admin.session.*, admin.mfa.*, admin.apiKey.*, admin.audit.*. The full list, with the service method that enforces each one, is in Authorization > Actions.

runAsSystem()​

import { runAsSystem } from '@nauth-toolkit/core';

await runAsSystem(async () => {
// provider is not consulted in here
});

Runs the callback with authorization bypassed. For seeds, migrations, scheduled jobs and tests — code with no authenticated caller. Never call it on a request path.

Configuration​

PlatformWhere
NestJSAuthModule.forRoot({ authorization: YourProviderClass }) — a class, registered as a NestJS provider
Express / FastifyNAuth.create({ authorization: new YourProvider() }) — an instance

See Configuration > Authorization.