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 exampleDirect link to Usage example
The following example implements a minimal provider. require throws to deny, and check returns a boolean.
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
}
}
MethodsDirect link to Methods
check(user, params)Direct link to 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)Direct link to requireuser-params
Throws FGADeniedError when user lacks the permission. Mastra calls this at its enforcement points.
Returns: Promise<void>
filterAccessible(user, resources, resourceType, permission)Direct link to filteraccessibleuser-resources-resourcetype-permission
Returns the subset of resources that user can access with permission.
Returns: Promise<T[]>
requireActor(actor, params)Direct link to 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 it is 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 propertiesDirect link to Configuration properties
Optional properties control route coverage and startup validation.
requireForProtectedRoutes?:
auditProtectedRoutes?:
resolveRouteFGA?:
validatePermissions?:
ParametersDirect link to Parameters
The params argument passed to check, require, and requireActor.
resource:
permission:
context?:
ActorSignalDirect link to actorsignal
Identifies a call made by a trusted non-user actor rather than an authenticated end user. It is either true (the anonymous system shorthand) or an object that names the acting agent and carries the grants a provider can enforce.
actorKind:
agentId?:
permissions?:
scope?:
sourceWorkflow?:
FGADeniedErrorDirect link to 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.