@mastra/auth 1.1.0 → 1.1.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/_types/@internal_auth/dist/_types/@internal_core/dist/base/index.d.ts +31 -0
  3. package/dist/_types/@internal_auth/dist/_types/@internal_core/dist/logger/index.d.ts +217 -0
  4. package/dist/_types/@internal_auth/dist/ee/capabilities.d.ts +142 -0
  5. package/dist/_types/@internal_auth/dist/ee/defaults/index.d.ts +8 -0
  6. package/dist/_types/@internal_auth/dist/ee/defaults/rbac/index.d.ts +7 -0
  7. package/dist/_types/@internal_auth/dist/ee/defaults/rbac/static.d.ts +106 -0
  8. package/dist/_types/@internal_auth/dist/ee/defaults/roles.d.ts +92 -0
  9. package/dist/_types/@internal_auth/dist/ee/fga-check.d.ts +62 -0
  10. package/dist/_types/@internal_auth/dist/ee/index.d.ts +15 -0
  11. package/dist/_types/@internal_auth/dist/ee/interfaces/acl.d.ts +140 -0
  12. package/dist/_types/@internal_auth/dist/ee/interfaces/fga.d.ts +350 -0
  13. package/dist/_types/@internal_auth/dist/ee/interfaces/index.d.ts +15 -0
  14. package/dist/_types/@internal_auth/dist/ee/interfaces/permissions.generated.d.ts +519 -0
  15. package/dist/_types/@internal_auth/dist/ee/interfaces/rbac.d.ts +207 -0
  16. package/dist/_types/@internal_auth/dist/ee/interfaces/user.d.ts +18 -0
  17. package/dist/_types/@internal_auth/dist/ee/license.d.ts +171 -0
  18. package/dist/_types/@internal_auth/dist/index.d.ts +277 -0
  19. package/dist/_types/@internal_auth/dist/provider/index.d.ts +125 -0
  20. package/dist/_types/@internal_auth/dist/session/cookie.d.ts +82 -0
  21. package/dist/_types/@internal_auth/dist/session/index.d.ts +30 -0
  22. package/dist/_types/@internal_auth/dist/session/memory.d.ts +60 -0
  23. package/dist/_types/@internal_auth/dist/types/index.d.ts +52 -0
  24. package/dist/docs/SKILL.md +1 -1
  25. package/dist/docs/assets/SOURCE_MAP.json +1 -1
  26. package/dist/index.cjs +182 -2
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.js +181 -1
  29. package/dist/index.js.map +1 -1
  30. package/dist/jwt.d.ts +3 -3
  31. package/dist/jwt.d.ts.map +1 -1
  32. package/package.json +4 -7
  33. package/LICENSE.md +0 -30
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @mastra/core/auth/ee
3
+ *
4
+ * Enterprise authentication capabilities for Mastra.
5
+ * This code is licensed under the Mastra Enterprise License - see ee/LICENSE.
6
+ *
7
+ * @license Mastra Enterprise License - see ee/LICENSE
8
+ * @packageDocumentation
9
+ */
10
+ export * from './interfaces/index.js';
11
+ export * from './capabilities.js';
12
+ export { validateLicense, isLicenseValid, isEELicenseValid, isFeatureEnabled, isDevEnvironment, isEEEnabled, startLicenseValidation, getSafeLicenseSummary, warnIfDevEENeedsLicense, clearLicenseCache, type LicenseInfo, } from './license.js';
13
+ export { checkFGA, requireFGA, FGADeniedError, getAgentFGAResourceId, getWorkflowFGAResourceId, getStandaloneToolFGAResourceId, getAgentToolFGAResourceId, getMCPToolFGAResourceId, type ActorSignal, type CheckFGAOptions, type RequireFGAOptions, } from './fga-check.js';
14
+ export * from './defaults/index.js';
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,140 @@
1
+ /**
2
+ * ACL provider interface for EE authentication.
3
+ * Enables resource-level access control in Studio.
4
+ */
5
+ /**
6
+ * Identifier for a resource.
7
+ */
8
+ export interface ResourceIdentifier {
9
+ /** Resource type (e.g., 'agent', 'workflow', 'thread') */
10
+ type: string;
11
+ /** Resource ID */
12
+ id: string;
13
+ }
14
+ /**
15
+ * An access control grant.
16
+ */
17
+ export interface ACLGrant {
18
+ /** Subject of the grant (user or role) */
19
+ subject: {
20
+ type: 'user' | 'role';
21
+ id: string;
22
+ };
23
+ /** Resource the grant applies to */
24
+ resource: ResourceIdentifier;
25
+ /** Actions granted */
26
+ actions: string[];
27
+ /** When the grant was created */
28
+ grantedAt: Date;
29
+ /** Who created the grant */
30
+ grantedBy?: string;
31
+ }
32
+ /**
33
+ * Provider interface for access control lists (read-only).
34
+ *
35
+ * Implement this interface to enable:
36
+ * - Resource-level permission checks
37
+ * - Filtered resource lists based on access
38
+ * - ACL display in resource settings
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * class DatabaseACLProvider implements IACLProvider {
43
+ * async canAccess(user, resource, action) {
44
+ * const grants = await this.db.query(
45
+ * `SELECT * FROM acl_grants
46
+ * WHERE (subject_type = 'user' AND subject_id = $1)
47
+ * OR (subject_type = 'role' AND subject_id = ANY($2))
48
+ * AND resource_type = $3 AND resource_id = $4
49
+ * AND $5 = ANY(actions)`,
50
+ * [user.id, user.roles, resource.type, resource.id, action]
51
+ * );
52
+ * return grants.length > 0;
53
+ * }
54
+ *
55
+ * async filterAccessible(user, resources, resourceType, action) {
56
+ * const accessible = await this.listAccessible(user, resourceType, action);
57
+ * return resources.filter(r => accessible.includes(r.id));
58
+ * }
59
+ * }
60
+ * ```
61
+ */
62
+ export interface IACLProvider<TUser = unknown> {
63
+ /**
64
+ * Check if user can perform action on resource.
65
+ *
66
+ * @param user - User making the request
67
+ * @param resource - Resource to check access for
68
+ * @param action - Action to check (e.g., 'read', 'write', 'execute', 'delete')
69
+ * @returns True if access is granted
70
+ */
71
+ canAccess(user: TUser, resource: ResourceIdentifier, action: string): Promise<boolean>;
72
+ /**
73
+ * Get list of resource IDs user can access.
74
+ *
75
+ * @param user - User to check access for
76
+ * @param resourceType - Type of resources to list
77
+ * @param action - Action to filter by
78
+ * @returns Array of accessible resource IDs
79
+ */
80
+ listAccessible(user: TUser, resourceType: string, action: string): Promise<string[]>;
81
+ /**
82
+ * Filter array of resources to only those user can access.
83
+ *
84
+ * @param user - User to check access for
85
+ * @param resources - Resources to filter
86
+ * @param resourceType - Type of the resources
87
+ * @param action - Action to filter by
88
+ * @returns Filtered array of accessible resources
89
+ */
90
+ filterAccessible<T extends {
91
+ id: string;
92
+ }>(user: TUser, resources: T[], resourceType: string, action: string): Promise<T[]>;
93
+ }
94
+ /**
95
+ * Extended interface for managing ACLs (write operations).
96
+ *
97
+ * Implement this in addition to IACLProvider to enable ACL management.
98
+ */
99
+ export interface IACLManager<TUser = unknown> extends IACLProvider<TUser> {
100
+ /**
101
+ * Grant access to a resource.
102
+ *
103
+ * @param subject - User or role to grant access to
104
+ * @param resource - Resource to grant access to
105
+ * @param actions - Actions to grant
106
+ */
107
+ grant(subject: {
108
+ type: 'user' | 'role';
109
+ id: string;
110
+ }, resource: ResourceIdentifier, actions: string[]): Promise<void>;
111
+ /**
112
+ * Revoke access to a resource.
113
+ *
114
+ * @param subject - User or role to revoke access from
115
+ * @param resource - Resource to revoke access to
116
+ * @param actions - Actions to revoke (omit to revoke all)
117
+ */
118
+ revoke(subject: {
119
+ type: 'user' | 'role';
120
+ id: string;
121
+ }, resource: ResourceIdentifier, actions?: string[]): Promise<void>;
122
+ /**
123
+ * List all grants for a resource.
124
+ *
125
+ * @param resource - Resource to list grants for
126
+ * @returns Array of grants
127
+ */
128
+ listGrants(resource: ResourceIdentifier): Promise<ACLGrant[]>;
129
+ /**
130
+ * List all grants for a subject.
131
+ *
132
+ * @param subject - User or role to list grants for
133
+ * @returns Array of grants
134
+ */
135
+ listGrantsForSubject(subject: {
136
+ type: 'user' | 'role';
137
+ id: string;
138
+ }): Promise<ACLGrant[]>;
139
+ }
140
+ //# sourceMappingURL=acl.d.ts.map
@@ -0,0 +1,350 @@
1
+ /**
2
+ * FGA (Fine-Grained Authorization) provider interface for EE authentication.
3
+ * Enables relationship-based, resource-level authorization.
4
+ *
5
+ * FGA complements RBAC by answering "Can this user do this action on this specific resource?"
6
+ * rather than "Can this role do this action?"
7
+ *
8
+ * @license Mastra Enterprise License - see ee/LICENSE
9
+ */
10
+ import type { MastraFGAPermissionInput } from './permissions.generated.js';
11
+ /**
12
+ * Optional context for an authorization check.
13
+ */
14
+ export interface FGACheckContext {
15
+ /**
16
+ * The owning application resource ID for the target resource.
17
+ * Useful when the authorization resource ID differs from the route-level ID,
18
+ * such as thread checks scoped by a thread's owning tenant/resource.
19
+ */
20
+ resourceId?: string;
21
+ /**
22
+ * Optional request context for providers that need additional request-scoped
23
+ * data to derive the authorization resource identifier.
24
+ */
25
+ requestContext?: any;
26
+ /**
27
+ * Optional provider-specific metadata about the attempted action.
28
+ */
29
+ metadata?: Record<string, unknown>;
30
+ }
31
+ /**
32
+ * Parameters for an authorization check.
33
+ */
34
+ export interface FGACheckParams {
35
+ /** The resource being accessed */
36
+ resource: {
37
+ type: string;
38
+ id: string;
39
+ };
40
+ /**
41
+ * The permission(s) being checked.
42
+ * When an array is provided, the user needs ANY ONE of the listed permissions
43
+ * (the check passes if any single permission resolves to allow).
44
+ */
45
+ permission: MastraFGAPermissionInput | MastraFGAPermissionInput[];
46
+ /** Optional provider-specific context for resource resolution */
47
+ context?: FGACheckContext;
48
+ }
49
+ /**
50
+ * Route-level FGA metadata.
51
+ */
52
+ export interface FGARouteConfig {
53
+ /** Resource type slug to authorize against, e.g. `agent`, `workflow`, or `thread`. */
54
+ resourceType: string;
55
+ /** Path/body/query parameter name that contains the resource ID. */
56
+ resourceIdParam?: string;
57
+ /** Static or dynamic resource ID resolver. */
58
+ resourceId?: string | ((params: Record<string, unknown>, context: {
59
+ requestContext?: any;
60
+ }) => string | undefined);
61
+ /**
62
+ * Permission(s) to check for this route. Falls back to the route permission when omitted.
63
+ * When an array is provided, the user needs ANY ONE of the listed permissions.
64
+ */
65
+ permission?: MastraFGAPermissionInput | MastraFGAPermissionInput[];
66
+ }
67
+ /**
68
+ * Minimal route information exposed to global FGA route resolvers.
69
+ */
70
+ export interface FGARouteInfo {
71
+ path: string;
72
+ method: string;
73
+ requiresAuth?: boolean;
74
+ /**
75
+ * Permission(s) required by this route.
76
+ * When an array is provided, the user needs ANY ONE of the listed permissions.
77
+ */
78
+ requiresPermission?: MastraFGAPermissionInput | MastraFGAPermissionInput[];
79
+ fga?: FGARouteConfig;
80
+ }
81
+ /**
82
+ * Context passed to global FGA route resolvers.
83
+ */
84
+ export interface FGARouteResolverContext {
85
+ route: FGARouteInfo;
86
+ params: Record<string, unknown>;
87
+ requestContext?: any;
88
+ }
89
+ /**
90
+ * Resolves route-level FGA metadata without mutating each route registration.
91
+ */
92
+ export type FGARouteResolver = (context: FGARouteResolverContext) => FGARouteConfig | null | undefined | Promise<FGARouteConfig | null | undefined>;
93
+ /**
94
+ * An authorization resource in the FGA system.
95
+ */
96
+ export interface FGAResource {
97
+ /** Internal resource ID */
98
+ id: string;
99
+ /** External ID (your application's resource identifier) */
100
+ externalId: string;
101
+ /** Display name */
102
+ name: string;
103
+ /** Optional description */
104
+ description?: string | null;
105
+ /** Resource type slug (e.g., 'team', 'project', 'workspace') */
106
+ resourceTypeSlug: string;
107
+ /** Organization ID the resource belongs to */
108
+ organizationId: string;
109
+ /** Parent resource ID for hierarchical resources */
110
+ parentResourceId?: string | null;
111
+ }
112
+ /**
113
+ * Parameters for creating an authorization resource.
114
+ */
115
+ export interface FGACreateResourceParams {
116
+ /** External ID (your application's resource identifier) */
117
+ externalId: string;
118
+ /** Display name */
119
+ name: string;
120
+ /** Optional description */
121
+ description?: string | null;
122
+ /** Resource type slug */
123
+ resourceTypeSlug: string;
124
+ /** Organization ID */
125
+ organizationId: string;
126
+ /** Parent resource ID (by internal ID) */
127
+ parentResourceId?: string;
128
+ /** Parent resource external ID (alternative to parentResourceId) */
129
+ parentResourceExternalId?: string;
130
+ /** Parent resource type slug (required with parentResourceExternalId) */
131
+ parentResourceTypeSlug?: string;
132
+ }
133
+ /**
134
+ * Parameters for updating an authorization resource.
135
+ */
136
+ export interface FGAUpdateResourceParams {
137
+ /** Internal resource ID */
138
+ resourceId: string;
139
+ /** New name */
140
+ name?: string;
141
+ /** New description */
142
+ description?: string | null;
143
+ }
144
+ /**
145
+ * Parameters for deleting an authorization resource.
146
+ */
147
+ export interface FGADeleteResourceParams {
148
+ /** Delete by internal resource ID */
149
+ resourceId?: string;
150
+ /** Delete by external ID (use with resourceTypeSlug and organizationId) */
151
+ externalId?: string;
152
+ /** Resource type slug (required when using externalId) */
153
+ resourceTypeSlug?: string;
154
+ /** Organization ID (required when using externalId) */
155
+ organizationId?: string;
156
+ }
157
+ /**
158
+ * A role assignment binding a membership to a role on a resource.
159
+ */
160
+ export interface FGARoleAssignment {
161
+ /** Assignment ID */
162
+ id: string;
163
+ /** The role */
164
+ role: {
165
+ slug: string;
166
+ };
167
+ /** The resource the role is assigned on */
168
+ resource: {
169
+ id: string;
170
+ externalId: string;
171
+ resourceTypeSlug: string;
172
+ };
173
+ }
174
+ /**
175
+ * Parameters for assigning or removing a role on a resource.
176
+ */
177
+ export interface FGARoleParams {
178
+ /** Organization membership ID */
179
+ organizationMembershipId: string;
180
+ /** Role slug to assign/remove */
181
+ roleSlug: string;
182
+ /** Resource ID to scope the role to */
183
+ resourceId?: string;
184
+ /** Resource external ID (alternative to resourceId) */
185
+ resourceExternalId?: string;
186
+ /** Resource type slug (required when using resourceExternalId) */
187
+ resourceTypeSlug?: string;
188
+ }
189
+ /**
190
+ * Options for listing role assignments.
191
+ */
192
+ export interface FGAListRoleAssignmentsOptions {
193
+ /** Organization membership ID */
194
+ organizationMembershipId: string;
195
+ /** Pagination limit */
196
+ limit?: number;
197
+ /** Pagination cursor */
198
+ after?: string;
199
+ }
200
+ /**
201
+ * Options for listing authorization resources.
202
+ */
203
+ export interface FGAListResourcesOptions {
204
+ /** Filter by organization */
205
+ organizationId?: string;
206
+ /** Filter by resource type */
207
+ resourceTypeSlug?: string;
208
+ /** Filter by parent resource */
209
+ parentResourceId?: string;
210
+ /** Search by name */
211
+ search?: string;
212
+ /** Pagination limit */
213
+ limit?: number;
214
+ /** Pagination cursor */
215
+ after?: string;
216
+ }
217
+ /**
218
+ * Provider interface for fine-grained authorization (read-only).
219
+ *
220
+ * This interface follows a user-centric model:
221
+ * - `check()` answers "Can this user perform this permission on this resource?"
222
+ * - `require()` throws if the user lacks permission
223
+ * - `filterAccessible()` filters a list of resources to only those the user can access
224
+ *
225
+ * The provider is responsible for translating between Mastra's resource/permission
226
+ * model and the underlying FGA provider's API (e.g., WorkOS Authorization).
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * import { MastraFGAPermissions } from '@mastra/core/auth/ee';
231
+ *
232
+ * const fga = new MastraFGAWorkos({
233
+ * resourceMapping: {
234
+ * agent: { fgaResourceType: 'team', deriveId: (ctx) => ctx.user.teamId },
235
+ * workflow: { fgaResourceType: 'team', deriveId: (ctx) => ctx.user.teamId },
236
+ * thread: { fgaResourceType: 'user', deriveId: (ctx) => ctx.resourceId ?? ctx.user.userId },
237
+ * },
238
+ * permissionMapping: {
239
+ * [MastraFGAPermissions.AGENTS_READ]: 'read',
240
+ * [MastraFGAPermissions.AGENTS_EXECUTE]: 'manage-workflows',
241
+ * [MastraFGAPermissions.WORKFLOWS_EXECUTE]: 'manage-workflows',
242
+ * [MastraFGAPermissions.MEMORY_READ]: 'read',
243
+ * [MastraFGAPermissions.MEMORY_WRITE]: 'update',
244
+ * },
245
+ * });
246
+ *
247
+ * // Check if a user can execute an agent
248
+ * const allowed = await fga.check(user, {
249
+ * resource: { type: 'agent', id: 'chef-agent' },
250
+ * permission: MastraFGAPermissions.AGENTS_EXECUTE,
251
+ * });
252
+ * ```
253
+ */
254
+ export interface IFGAProvider<TUser = unknown> {
255
+ /**
256
+ * When true, protected routes without route-level FGA metadata or resolver
257
+ * output are denied instead of being allowed through.
258
+ *
259
+ * @default false
260
+ */
261
+ requireForProtectedRoutes?: boolean;
262
+ /**
263
+ * Audits protected routes that do not have built-in FGA metadata.
264
+ * Use `true` or `'warn'` to log a startup warning, `'error'` to fail startup,
265
+ * or `false` to disable the audit.
266
+ *
267
+ * @default false
268
+ */
269
+ auditProtectedRoutes?: boolean | 'warn' | 'error';
270
+ /**
271
+ * Global route FGA resolver. This can derive resource type, resource ID, and
272
+ * permission from the route, parsed params, and request context.
273
+ */
274
+ resolveRouteFGA?: FGARouteResolver;
275
+ /**
276
+ * Optional startup validation for provider-specific permission mappings.
277
+ * Providers can throw when a permission Mastra may emit is not mapped.
278
+ */
279
+ validatePermissions?: (permissions: MastraFGAPermissionInput[]) => void | Promise<void>;
280
+ /**
281
+ * Check if a user has a specific permission on a resource.
282
+ *
283
+ * @param user - The user to check
284
+ * @param params - The resource and permission to check
285
+ * @returns true if the user has permission, false otherwise
286
+ */
287
+ check(user: TUser, params: FGACheckParams): Promise<boolean>;
288
+ /**
289
+ * Require that a user has a specific permission on a resource.
290
+ * Throws FGADeniedError if the user does not have permission.
291
+ *
292
+ * @param user - The user to check
293
+ * @param params - The resource and permission to check
294
+ * @throws FGADeniedError if the user does not have permission
295
+ */
296
+ require(user: TUser, params: FGACheckParams): Promise<void>;
297
+ /**
298
+ * Filter a list of resources to only those the user can access.
299
+ *
300
+ * @param user - The user to check
301
+ * @param resources - The resources to filter
302
+ * @param resourceType - The type of resources being filtered
303
+ * @param permission - The permission to check for
304
+ * @returns The filtered list of resources the user can access
305
+ */
306
+ filterAccessible<T extends {
307
+ id: string;
308
+ }>(user: TUser, resources: T[], resourceType: string, permission: MastraFGAPermissionInput): Promise<T[]>;
309
+ }
310
+ /**
311
+ * Extended FGA interface with write operations for managing resources and role assignments.
312
+ *
313
+ * Implement this interface when the FGA provider also manages the authorization
314
+ * model (creating resources, assigning roles, etc.).
315
+ */
316
+ export interface IFGAManager<TUser = unknown> extends IFGAProvider<TUser> {
317
+ /**
318
+ * Create an authorization resource.
319
+ */
320
+ createResource(params: FGACreateResourceParams): Promise<FGAResource>;
321
+ /**
322
+ * Get an authorization resource by ID.
323
+ */
324
+ getResource(resourceId: string): Promise<FGAResource>;
325
+ /**
326
+ * List authorization resources.
327
+ */
328
+ listResources(options?: FGAListResourcesOptions): Promise<FGAResource[]>;
329
+ /**
330
+ * Update an authorization resource.
331
+ */
332
+ updateResource(params: FGAUpdateResourceParams): Promise<FGAResource>;
333
+ /**
334
+ * Delete an authorization resource.
335
+ */
336
+ deleteResource(params: FGADeleteResourceParams): Promise<void>;
337
+ /**
338
+ * Assign a role to an organization membership on a specific resource.
339
+ */
340
+ assignRole(params: FGARoleParams): Promise<FGARoleAssignment>;
341
+ /**
342
+ * Remove a role assignment.
343
+ */
344
+ removeRole(params: FGARoleParams): Promise<void>;
345
+ /**
346
+ * List role assignments for an organization membership.
347
+ */
348
+ listRoleAssignments(options: FGAListRoleAssignmentsOptions): Promise<FGARoleAssignment[]>;
349
+ }
350
+ //# sourceMappingURL=fga.d.ts.map
@@ -0,0 +1,15 @@
1
+ /**
2
+ * EE Authentication Interfaces
3
+ *
4
+ * Enterprise interfaces for RBAC, ACL, and advanced authorization.
5
+ *
6
+ * @license Mastra Enterprise License - see ee/LICENSE
7
+ * @packageDocumentation
8
+ */
9
+ export type { EEUser } from './user.js';
10
+ export type { RoleDefinition, RoleMapping, IRBACProvider, IRBACManager } from './rbac.js';
11
+ export type { Resource, Action, Permission, PermissionPattern, MastraFGAPermission, MastraFGAPermissionInput, TypedRoleMapping, } from './permissions.generated.js';
12
+ export { RESOURCES, ACTIONS, PERMISSIONS, PERMISSION_PATTERNS, MastraFGAPermissions, isValidPermissionPattern, validatePermissions, } from './permissions.generated.js';
13
+ export type { ResourceIdentifier, ACLGrant, IACLProvider, IACLManager } from './acl.js';
14
+ export type { FGACheckContext, FGACheckParams, FGARouteConfig, FGARouteInfo, FGARouteResolver, FGARouteResolverContext, FGAResource, FGACreateResourceParams, FGAUpdateResourceParams, FGADeleteResourceParams, FGARoleAssignment, FGARoleParams, FGAListRoleAssignmentsOptions, FGAListResourcesOptions, IFGAProvider, IFGAManager, } from './fga.js';
15
+ //# sourceMappingURL=index.d.ts.map