@mastra/auth-studio 1.4.0-alpha.1 → 1.4.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 (29) hide show
  1. package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/index.d.ts +15 -0
  2. package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/provider/index.d.ts +73 -1
  3. package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/types/index.d.ts +7 -0
  4. package/dist/index.cjs +47 -0
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +4 -4
  7. package/dist/index.js +47 -0
  8. package/dist/index.js.map +1 -1
  9. package/package.json +4 -4
  10. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/_types/@internal_core@0.1.4/dist/base/index.d.ts +0 -0
  11. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/_types/@internal_core@0.1.4/dist/index-Diz1p9N7.d.ts +0 -0
  12. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/capabilities.d.ts +0 -0
  13. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/defaults/index.d.ts +0 -0
  14. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/defaults/rbac/index.d.ts +0 -0
  15. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/defaults/rbac/static.d.ts +0 -0
  16. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/defaults/roles.d.ts +0 -0
  17. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/fga-check.d.ts +0 -0
  18. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/index.d.ts +0 -0
  19. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/interfaces/acl.d.ts +0 -0
  20. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/interfaces/fga.d.ts +0 -0
  21. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/interfaces/index.d.ts +0 -0
  22. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/interfaces/permissions.generated.d.ts +0 -0
  23. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/interfaces/rbac.d.ts +0 -0
  24. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/interfaces/user.d.ts +0 -0
  25. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/ee/license.d.ts +0 -0
  26. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/session/cookie.d.ts +0 -0
  27. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/session/index.d.ts +0 -0
  28. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/dist/session/memory.d.ts +0 -0
  29. /package/dist/_types/{@internal_auth@0.0.36 → @internal_auth@0.0.37}/package.json +0 -0
@@ -14,7 +14,22 @@ export interface User {
14
14
  name?: string;
15
15
  /** Avatar URL */
16
16
  avatarUrl?: string;
17
+ /**
18
+ * Primary role, when the auth provider or a configured RBAC provider
19
+ * supplies one. `undefined` means the auth setup has no role concept —
20
+ * not that the user lacks permissions.
21
+ */
22
+ role?: string;
17
23
  }
24
+ /**
25
+ * Authenticated end user, normalized to a stable shape.
26
+ *
27
+ * Produced by `MastraAuthProvider.toMastraUser()` at authentication time and
28
+ * surfaced to `DynamicArgument` callbacks as the optional `user` argument.
29
+ * The raw provider-specific user object remains available on the request
30
+ * context under the `mastra__user` key.
31
+ */
32
+ export type MastraUser = User;
18
33
  /**
19
34
  * Provider interface for user awareness in Studio.
20
35
  *
@@ -1,6 +1,18 @@
1
1
  import { MastraBase } from '../_types/@internal_core@0.1.4/dist/base/index.js';
2
- import type { CredentialsResult, IAuthHttpHandler, IAuthInit, ICredentialsProvider, IOrganizationsProvider, ISSOProvider, ISessionProvider, IUserProvider, Session, SSOCallbackResult, SSOLoginConfig, User } from '..';
2
+ import type { CredentialsResult, IAuthHttpHandler, IAuthInit, ICredentialsProvider, IOrganizationsProvider, ISSOProvider, ISessionProvider, IUserProvider, MastraUser, Session, SSOCallbackResult, SSOLoginConfig, User } from '..';
3
3
  import type { AuthorizeUserFn, MastraAuthConfig, MastraAuthRequest } from '../types/index.js';
4
+ /**
5
+ * Normalize an arbitrary provider-specific user object to the stable
6
+ * {@link MastraUser} shape surfaced to `DynamicArgument` callbacks.
7
+ *
8
+ * Picks `id` from `id`/`sub`/`userId` (numeric ids are stringified),
9
+ * `avatarUrl` from `avatarUrl`/`picture`, and passes `name`/`email`/`role`
10
+ * through when they are non-empty strings.
11
+ *
12
+ * @throws when no id can be derived — override `toMastraUser()` on the auth
13
+ * provider for user shapes this default cannot handle.
14
+ */
15
+ export declare function normalizeMastraUser(user: unknown): MastraUser;
4
16
  export interface MastraAuthProviderOptions<TUser = unknown> {
5
17
  name?: string;
6
18
  authorizeUser?: AuthorizeUserFn<TUser>;
@@ -50,6 +62,11 @@ export interface IMastraAuthProvider<TUser = unknown> {
50
62
  * Map an authenticated user to a memory resource id
51
63
  */
52
64
  mapUserToResourceId?(user: TUser): string | undefined | null;
65
+ /**
66
+ * Normalize the authenticated provider-specific user to the stable
67
+ * `MastraUser` shape surfaced to `DynamicArgument` callbacks.
68
+ */
69
+ toMastraUser?(user: TUser): MastraUser;
53
70
  /**
54
71
  * Return response headers (typically `Set-Cookie` for a rotated session)
55
72
  * that the provider accumulated while handling this request and clear them
@@ -58,7 +75,32 @@ export interface IMastraAuthProvider<TUser = unknown> {
58
75
  * reach the browser.
59
76
  */
60
77
  consumePendingResponseHeaders?(request: MastraAuthRequest): Record<string, string> | undefined;
78
+ /**
79
+ * OAuth 2.0 Protected Resource Metadata (RFC 9728) hints, used by the built-in MCP server's
80
+ * `/.well-known/oauth-protected-resource` endpoint when `server.mcp.oauth` does not set them.
81
+ */
82
+ getOAuthMetadata?(): MastraAuthOAuthMetadata | undefined;
83
+ /**
84
+ * Whether the token that authenticated `user` was issued for `audience` (its `aud` claim includes it).
85
+ * `user` is the value returned by `authenticateToken`. Called by the built-in MCP server when
86
+ * `server.mcp.oauth.resource` is set.
87
+ */
88
+ verifyAudience?(user: TUser, audience: string): boolean | Promise<boolean>;
89
+ }
90
+ /** Authorization server hints for MCP clients (RFC 9728). */
91
+ export interface MastraAuthOAuthMetadata {
92
+ /** Issuer URLs of the authorization servers that issue tokens accepted by this provider. */
93
+ authorizationServers?: string[];
94
+ /** OAuth scopes the provider understands. */
95
+ scopesSupported?: string[];
61
96
  }
97
+ /** Whether a JWT `aud` claim (string or array) includes `audience`. */
98
+ export declare function jwtAudienceIncludes(aud: unknown, audience: string): boolean;
99
+ /**
100
+ * JWT claims as the authenticated user. OIDC tokens name the user in `sub`, while RBAC and FGA look the user up
101
+ * by `id`, so `id` is filled from `sub` when the token has no `id` claim of its own.
102
+ */
103
+ export declare function withSubjectAsId<T extends object>(claims: T): T;
62
104
  export declare abstract class MastraAuthProvider<TUser = unknown> extends MastraBase implements IMastraAuthProvider<TUser> {
63
105
  protected?: MastraAuthConfig['protected'];
64
106
  public?: MastraAuthConfig['public'];
@@ -78,6 +120,16 @@ export declare abstract class MastraAuthProvider<TUser = unknown> extends Mastra
78
120
  * @returns The authorization result
79
121
  */
80
122
  abstract authorizeUser(user: TUser, request: MastraAuthRequest): Promise<boolean> | boolean;
123
+ /**
124
+ * Normalize the authenticated provider-specific user to the stable
125
+ * {@link MastraUser} shape surfaced to `DynamicArgument` callbacks as the
126
+ * optional `user` argument.
127
+ *
128
+ * The default implementation handles common user shapes (see
129
+ * {@link normalizeMastraUser}); override it when your provider's user type
130
+ * needs custom mapping.
131
+ */
132
+ toMastraUser(user: TUser): MastraUser;
81
133
  protected registerOptions(opts?: MastraAuthProviderOptions<TUser>): void;
82
134
  }
83
135
  export declare function isSSOProvider(p: unknown): p is ISSOProvider;
@@ -95,6 +147,13 @@ export declare class CompositeAuth extends MastraAuthProvider implements ISSOPro
95
147
  private findProvider;
96
148
  private rememberAuthenticatedProvider;
97
149
  private takeAuthenticatedProvider;
150
+ private peekAuthenticatedProvider;
151
+ /**
152
+ * Delegate normalization to the provider that authenticated this user.
153
+ * Falls back to the default normalization when the winning provider is
154
+ * unknown or predates `toMastraUser`.
155
+ */
156
+ toMastraUser(user: unknown): MastraUser;
98
157
  private mapAuthenticatedUserToResourceId;
99
158
  /**
100
159
  * True if any provider is MastraCloudAuth (exempt from license requirement).
@@ -106,6 +165,19 @@ export declare class CompositeAuth extends MastraAuthProvider implements ISSOPro
106
165
  get isSimpleAuth(): boolean;
107
166
  authenticateToken(token: string, request: MastraAuthRequest): Promise<unknown | null>;
108
167
  consumePendingResponseHeaders(request: MastraAuthRequest): Record<string, string> | undefined;
168
+ /** The wrapped provider that authenticated `user`, or undefined when it can't be determined. */
169
+ getAuthenticatedProvider(user: unknown): IMastraAuthProvider | undefined;
170
+ /** The wrapped providers, in the order they are tried. */
171
+ getProviders(): readonly IMastraAuthProvider[];
172
+ /** Union of the wrapped providers' OAuth metadata. */
173
+ getOAuthMetadata(): MastraAuthOAuthMetadata | undefined;
174
+ /**
175
+ * Delegates to the provider that authenticated `user`. Returns true (audience not enforced) when that
176
+ * provider has no `verifyAudience`. When the authenticating provider can't be determined (for example the
177
+ * user object was replaced after authentication), returns false if any wrapped provider could check the
178
+ * audience, so the check fails closed.
179
+ */
180
+ verifyAudience(user: unknown, audience: string): Promise<boolean>;
109
181
  authorizeUser(user: unknown, request: MastraAuthRequest): Promise<boolean>;
110
182
  /**
111
183
  * Forward cookie header to SSO provider for PKCE validation.
@@ -1,3 +1,4 @@
1
+ import type { User } from '../index.js';
1
2
  export interface HonoRequestLike {
2
3
  raw?: Request;
3
4
  headers?: Headers | Record<string, string | string[] | undefined>;
@@ -34,6 +35,12 @@ export type MastraAuthConfig<TUser = unknown, TContext = unknown> = {
34
35
  * Maps the authenticated user to a resource ID for memory/thread scoping.
35
36
  */
36
37
  mapUserToResourceId?(user: TUser): string | undefined | null;
38
+ /**
39
+ * Normalizes the authenticated user to the `MastraUser` shape passed to
40
+ * dynamic configuration callbacks as `user`. Defaults to the shared
41
+ * normalization (`id`/`sub`/`userId`, `name`, `email`, `avatarUrl`/`picture`, `role`).
42
+ */
43
+ toMastraUser?(user: TUser): User;
37
44
  /**
38
45
  * Authorization function for the server.
39
46
  */
package/dist/index.cjs CHANGED
@@ -293,6 +293,41 @@ var MastraBase = class {
293
293
  }
294
294
  };
295
295
  // ../../packages/_internals/auth/dist/provider/index.js
296
+ function pickString(...values) {
297
+ for (const value of values) if (typeof value === "string" && value.trim()) return value;
298
+ }
299
+ /**
300
+ * Normalize an arbitrary provider-specific user object to the stable
301
+ * {@link MastraUser} shape surfaced to `DynamicArgument` callbacks.
302
+ *
303
+ * Picks `id` from `id`/`sub`/`userId` (numeric ids are stringified),
304
+ * `avatarUrl` from `avatarUrl`/`picture`, and passes `name`/`email`/`role`
305
+ * through when they are non-empty strings.
306
+ *
307
+ * @throws when no id can be derived — override `toMastraUser()` on the auth
308
+ * provider for user shapes this default cannot handle.
309
+ */
310
+ function normalizeMastraUser(user) {
311
+ if (typeof user === "string" && user.trim()) return { id: user };
312
+ if (user === null || typeof user !== "object") throw new Error(`Unable to normalize authenticated user of type "${typeof user}" to a MastraUser. Override toMastraUser() on your auth provider to supply a normalized user.`);
313
+ const record = user;
314
+ const rawId = [
315
+ record.id,
316
+ record.sub,
317
+ record.userId
318
+ ].find((value) => typeof value === "string" && value.trim() || typeof value === "number");
319
+ if (rawId === void 0) throw new Error("Unable to derive a user id (looked at `id`, `sub`, and `userId`) while normalizing the authenticated user. Override toMastraUser() on your auth provider to supply a normalized user.");
320
+ const normalized = { id: typeof rawId === "number" ? String(rawId) : rawId };
321
+ const name = pickString(record.name);
322
+ const email = pickString(record.email);
323
+ const avatarUrl = pickString(record.avatarUrl, record.picture);
324
+ const role = pickString(record.role);
325
+ if (name) normalized.name = name;
326
+ if (email) normalized.email = email;
327
+ if (avatarUrl) normalized.avatarUrl = avatarUrl;
328
+ if (role) normalized.role = role;
329
+ return normalized;
330
+ }
296
331
  var MastraAuthProvider = class extends MastraBase {
297
332
  protected;
298
333
  public;
@@ -306,6 +341,18 @@ var MastraAuthProvider = class extends MastraBase {
306
341
  this.public = options?.public;
307
342
  if (options?.mapUserToResourceId) this.mapUserToResourceId = options.mapUserToResourceId;
308
343
  }
344
+ /**
345
+ * Normalize the authenticated provider-specific user to the stable
346
+ * {@link MastraUser} shape surfaced to `DynamicArgument` callbacks as the
347
+ * optional `user` argument.
348
+ *
349
+ * The default implementation handles common user shapes (see
350
+ * {@link normalizeMastraUser}); override it when your provider's user type
351
+ * needs custom mapping.
352
+ */
353
+ toMastraUser(user) {
354
+ return normalizeMastraUser(user);
355
+ }
309
356
  registerOptions(opts) {
310
357
  if (opts?.authorizeUser) this.authorizeUser = opts.authorizeUser.bind(this);
311
358
  if (opts?.mapUserToResourceId) this.mapUserToResourceId = opts.mapUserToResourceId;