跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

IFGAProvider

The IFGAProvider interface defines a fine-grained authorization (FGA) provider. Mastra calls it to decide whether a user or a system actor may perform a permission on a specific resource. Implement it to connect Mastra to an FGA backend, such as WorkOS Authorization.

For concepts, configuration, and the lifecycle points where Mastra enforces FGA, see Fine-grained authorization.

Usage example
Usage example的直接链接

The following example implements a minimal provider. require throws to deny, and check returns a boolean.

src/mastra/fga.ts
import { FGADeniedError } from '@mastra/core/auth/ee'
import type { FGACheckParams, IFGAProvider, MastraFGAPermissionInput } from '@mastra/core/auth/ee'

class MyFGAProvider implements IFGAProvider {
async check(user: any, params: FGACheckParams): Promise<boolean> {
// Your authorization logic.
return true
}

async require(user: any, params: FGACheckParams): Promise<void> {
if (!(await this.check(user, params))) {
throw new FGADeniedError(user, params.resource, params.permission)
}
}

async filterAccessible<T extends { id: string }>(
user: any,
resources: T[],
resourceType: string,
permission: MastraFGAPermissionInput,
): Promise<T[]> {
return resources
}
}

Methods
Methods的直接链接

check(user, params)
checkuser-params的直接链接

Returns whether user has the permission on the resource. Use it for non-throwing checks, such as filtering or conditional UI.

Returns: Promise<boolean>

require(user, params)
requireuser-params的直接链接

Throws FGADeniedError when user lacks the permission. Mastra calls this at its enforcement points.

Returns: Promise<void>

filterAccessible(user, resources, resourceType, permission)
filteraccessibleuser-resources-resourcetype-permission的直接链接

Returns the subset of resources that user can access with permission.

Returns: Promise<T[]>

requireActor(actor, params)
requireactoractor-params的直接链接

Authorizes a non-user system actor, such as an autonomous or scheduled agent. Optional.

System actors skip the user-centric require() path, so implement requireActor to enforce per-agent least privilege for them. Throw FGADeniedError to deny. When a provider doesn't implement requireActor, Mastra preserves the trusted-actor bypass (allow after the tenant-scope check), so adding the method remains backward compatible.

Treat actor.permissions as an untrusted claim. Resolve the agent's authoritative grants from a trusted source keyed by actor.agentId, rather than trusting the inline values. See System actors.

import { FGADeniedError } from '@mastra/core/auth/ee'
import type { ActorSignal, FGACheckParams, IFGAProvider } from '@mastra/core/auth/ee'

class MyFGAProvider implements IFGAProvider {
// ...check, require, filterAccessible...

async requireActor(actor: ActorSignal, params: FGACheckParams): Promise<void> {
const agentId = actor === true ? undefined : actor.agentId
// Resolve the agent's authoritative grants from a trusted source keyed by agentId.
const granted = await this.grantsForAgent(agentId)
const required = Array.isArray(params.permission) ? params.permission : [params.permission]
if (!required.some(permission => granted.includes(permission))) {
throw new FGADeniedError(null, params.resource, params.permission)
}
}
}

Returns: Promise<void>

Configuration properties
Configuration properties的直接链接

Optional properties control route coverage and startup validation.

requireForProtectedRoutes?:

boolean
= false
When true, protected routes without route-level FGA metadata or resolver output are denied instead of allowed through.

auditProtectedRoutes?:

boolean | 'warn' | 'error'
= false
Audits protected routes that lack built-in FGA metadata. Use true or 'warn' to log a startup warning, 'error' to fail startup, or false to disable.

resolveRouteFGA?:

FGARouteResolver
Derives resource type, resource ID, and permission from the route, parsed params, and request context.

validatePermissions?:

(permissions: MastraFGAPermissionInput[]) => void | Promise<void>
Startup validation for provider-specific permission mappings. Throw when a permission Mastra may emit is not mapped.

Parameters
Parameters的直接链接

The params argument passed to check, require, and requireActor.

resource:

{ type: string; id: string }
The resource being accessed.

permission:

MastraFGAPermissionInput | MastraFGAPermissionInput[]
The permission(s) being checked. When an array is provided, the actor needs any one of the listed permissions.

context?:

FGACheckContext
Provider-specific context for resource resolution, including the owning resourceId, the request context, and action metadata.

ActorSignal
actorsignal的直接链接

Identifies a call made by a trusted non-user actor rather than an authenticated end user. It's either true (the anonymous system shorthand) or an object that names the acting agent and carries the grants a provider can enforce.

actorKind:

'system'
Marks the object form of the signal.

agentId?:

string
Identity of the acting system agent. Unlike the check resource (the target), this names the actor itself, so a provider can enforce per-agent least privilege.

permissions?:

MastraFGAPermissionInput[]
Permission grants claimed for this actor. This is an untrusted, self-asserted hint; a provider enforcing real least privilege resolves the agent's authoritative grants from a trusted source keyed by agentId rather than trusting these values.

scope?:

Record<string, string>
Additional provider-specific scope for the actor, for example tenant or environment.

sourceWorkflow?:

string
Name of the workflow that started the actor run, when applicable.

propagate?:

boolean
= false
Forwards this actor into the agent and tool calls the framework makes for the run's declarative .then(agent) and .then(tool) steps. Custom step execute functions aren't covered and must pass actor explicitly. Ignored by authorization itself.

FGADeniedError
fgadeniederror的直接链接

Thrown when an authorization check is denied. require and requireActor throw it to deny, and Mastra surfaces it as an HTTP 403.

import { FGADeniedError } from '@mastra/core/auth/ee'

throw new FGADeniedError(user, { type: 'agent', id: 'reporter' }, 'agents:execute')
// Optional fourth argument: a reason string included in the error message.