@mastra/auth-workos 1.6.0 → 1.6.1

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 (37) hide show
  1. package/CHANGELOG.md +18 -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/auth-provider.d.ts +3 -3
  25. package/dist/auth-provider.d.ts.map +1 -1
  26. package/dist/fga-provider.d.ts +2 -2
  27. package/dist/fga-provider.d.ts.map +1 -1
  28. package/dist/index.cjs +316 -12
  29. package/dist/index.cjs.map +1 -1
  30. package/dist/index.js +307 -3
  31. package/dist/index.js.map +1 -1
  32. package/dist/rbac-provider.d.ts +1 -1
  33. package/dist/rbac-provider.d.ts.map +1 -1
  34. package/dist/types.d.ts +3 -4
  35. package/dist/types.d.ts.map +1 -1
  36. package/package.json +6 -6
  37. package/LICENSE.md +0 -30
package/dist/index.cjs CHANGED
@@ -1,13 +1,190 @@
1
1
  'use strict';
2
2
 
3
3
  var auth = require('@mastra/auth');
4
- var server = require('@mastra/core/server');
5
4
  var authkitSession = require('@workos/authkit-session');
6
5
  var node = require('@workos-inc/node');
7
6
  var lruCache = require('lru-cache');
8
- var ee = require('@mastra/core/auth/ee');
9
7
 
10
- // src/auth-provider.ts
8
+ // ../../packages/_internal-core/dist/chunk-3M4SEWMI.js
9
+ var RegisteredLogger = {
10
+ LLM: "LLM"};
11
+ var LogLevel = {
12
+ DEBUG: "debug",
13
+ INFO: "info",
14
+ WARN: "warn",
15
+ ERROR: "error"};
16
+ var MastraLogger = class {
17
+ name;
18
+ level;
19
+ transports;
20
+ constructor(options = {}) {
21
+ this.name = options.name || "Mastra";
22
+ this.level = options.level || LogLevel.ERROR;
23
+ this.transports = new Map(Object.entries(options.transports || {}));
24
+ }
25
+ getTransports() {
26
+ return this.transports;
27
+ }
28
+ trackException(_error, _metadata) {
29
+ }
30
+ async listLogs(transportId, params) {
31
+ if (!transportId || !this.transports.has(transportId)) {
32
+ return { logs: [], total: 0, page: params?.page ?? 1, perPage: params?.perPage ?? 100, hasMore: false };
33
+ }
34
+ return this.transports.get(transportId).listLogs?.(params) ?? {
35
+ logs: [],
36
+ total: 0,
37
+ page: params?.page ?? 1,
38
+ perPage: params?.perPage ?? 100,
39
+ hasMore: false
40
+ };
41
+ }
42
+ async listLogsByRunId({
43
+ transportId,
44
+ runId,
45
+ fromDate,
46
+ toDate,
47
+ logLevel,
48
+ filters,
49
+ page,
50
+ perPage
51
+ }) {
52
+ if (!transportId || !this.transports.has(transportId) || !runId) {
53
+ return { logs: [], total: 0, page: page ?? 1, perPage: perPage ?? 100, hasMore: false };
54
+ }
55
+ return this.transports.get(transportId).listLogsByRunId?.({ runId, fromDate, toDate, logLevel, filters, page, perPage }) ?? {
56
+ logs: [],
57
+ total: 0,
58
+ page: page ?? 1,
59
+ perPage: perPage ?? 100,
60
+ hasMore: false
61
+ };
62
+ }
63
+ };
64
+ var ConsoleLogger = class _ConsoleLogger extends MastraLogger {
65
+ component;
66
+ filter;
67
+ constructor(options = {}) {
68
+ super(options);
69
+ this.component = options.component;
70
+ this.filter = options.filter;
71
+ }
72
+ child(componentOrBindings) {
73
+ const component = typeof componentOrBindings === "string" ? componentOrBindings : componentOrBindings?.component ?? this.component;
74
+ return new _ConsoleLogger({
75
+ name: this.name,
76
+ level: this.level,
77
+ component,
78
+ filter: this.filter
79
+ });
80
+ }
81
+ shouldLog(level, message, args) {
82
+ if (!this.filter) return true;
83
+ try {
84
+ return this.filter({ component: this.component, level, message, args });
85
+ } catch (e) {
86
+ console.error(`[Logger] Filter error for component=${this.component} level=${level}:`, e);
87
+ return true;
88
+ }
89
+ }
90
+ prefix() {
91
+ return this.component ? `[${this.component}] ` : "";
92
+ }
93
+ debug(message, ...args) {
94
+ if (this.level === LogLevel.DEBUG && this.shouldLog(LogLevel.DEBUG, message, args)) {
95
+ console.info(`${this.prefix()}${message}`, ...args);
96
+ }
97
+ }
98
+ info(message, ...args) {
99
+ if ((this.level === LogLevel.INFO || this.level === LogLevel.DEBUG) && this.shouldLog(LogLevel.INFO, message, args)) {
100
+ console.info(`${this.prefix()}${message}`, ...args);
101
+ }
102
+ }
103
+ warn(message, ...args) {
104
+ if ((this.level === LogLevel.WARN || this.level === LogLevel.INFO || this.level === LogLevel.DEBUG) && this.shouldLog(LogLevel.WARN, message, args)) {
105
+ console.warn(`${this.prefix()}${message}`, ...args);
106
+ }
107
+ }
108
+ error(message, ...args) {
109
+ if ((this.level === LogLevel.ERROR || this.level === LogLevel.WARN || this.level === LogLevel.INFO || this.level === LogLevel.DEBUG) && this.shouldLog(LogLevel.ERROR, message, args)) {
110
+ console.error(`${this.prefix()}${message}`, ...args);
111
+ }
112
+ }
113
+ async listLogs(_transportId, _params) {
114
+ return { logs: [], total: 0, page: _params?.page ?? 1, perPage: _params?.perPage ?? 100, hasMore: false };
115
+ }
116
+ async listLogsByRunId(_args) {
117
+ return { logs: [], total: 0, page: _args.page ?? 1, perPage: _args.perPage ?? 100, hasMore: false };
118
+ }
119
+ };
120
+
121
+ // ../../packages/_internal-core/dist/base/index.js
122
+ var MastraBase = class {
123
+ component = RegisteredLogger.LLM;
124
+ logger;
125
+ name;
126
+ #rawConfig;
127
+ constructor({
128
+ component,
129
+ name,
130
+ rawConfig
131
+ }) {
132
+ this.component = component || RegisteredLogger.LLM;
133
+ this.name = name;
134
+ this.#rawConfig = rawConfig;
135
+ this.logger = new ConsoleLogger({ name: `${this.component} - ${this.name}` });
136
+ }
137
+ /**
138
+ * Returns the raw storage configuration this primitive was created from,
139
+ * or undefined if it was created from code.
140
+ */
141
+ toRawConfig() {
142
+ return this.#rawConfig;
143
+ }
144
+ /**
145
+ * Sets the raw storage configuration for this primitive.
146
+ * @internal
147
+ */
148
+ __setRawConfig(rawConfig) {
149
+ this.#rawConfig = rawConfig;
150
+ }
151
+ /**
152
+ * Set the logger for the agent
153
+ * @param logger
154
+ */
155
+ __setLogger(logger) {
156
+ this.logger = "child" in logger && typeof logger.child === "function" ? logger.child({ component: this.component }) : logger;
157
+ }
158
+ };
159
+
160
+ // ../../packages/_internals/auth/dist/chunk-XG6GH2VJ.js
161
+ var MastraAuthProvider = class extends MastraBase {
162
+ protected;
163
+ public;
164
+ constructor(options) {
165
+ super({ component: "AUTH", name: options?.name });
166
+ if (options?.authorizeUser) {
167
+ this.authorizeUser = options.authorizeUser.bind(this);
168
+ }
169
+ this.protected = options?.protected;
170
+ this.public = options?.public;
171
+ this.mapUserToResourceId = options?.mapUserToResourceId;
172
+ }
173
+ registerOptions(opts) {
174
+ if (opts?.authorizeUser) {
175
+ this.authorizeUser = opts.authorizeUser.bind(this);
176
+ }
177
+ if (opts?.mapUserToResourceId) {
178
+ this.mapUserToResourceId = opts.mapUserToResourceId;
179
+ }
180
+ if (opts?.protected) {
181
+ this.protected = opts.protected;
182
+ }
183
+ if (opts?.public) {
184
+ this.public = opts.public;
185
+ }
186
+ }
187
+ };
11
188
  var WebSessionStorage = class extends authkitSession.CookieSessionStorage {
12
189
  constructor(config) {
13
190
  super(config);
@@ -63,7 +240,7 @@ function getWebRequest(request) {
63
240
  var DEV_COOKIE_PASSWORD = crypto.randomUUID() + crypto.randomUUID();
64
241
  var MEMBERSHIP_CACHE_TTL_MS = 60 * 1e3;
65
242
  var MEMBERSHIP_CACHE_MAX_SIZE = 1e3;
66
- var MastraAuthWorkos = class extends server.MastraAuthProvider {
243
+ var MastraAuthWorkos = class extends MastraAuthProvider {
67
244
  workos;
68
245
  clientId;
69
246
  redirectUri;
@@ -609,6 +786,101 @@ var MastraAuthWorkos = class extends server.MastraAuthProvider {
609
786
  return this.redirectUri;
610
787
  }
611
788
  };
789
+
790
+ // ../../packages/_internals/auth/dist/chunk-7SVOHBXQ.js
791
+ var FGADeniedError = class extends Error {
792
+ user;
793
+ resource;
794
+ permission;
795
+ status;
796
+ constructor(user, resource, permission, reason) {
797
+ const userId = user?.id || user?.workosId || "unknown";
798
+ const permissionLabel = Array.isArray(permission) ? `any of [${permission.join(", ")}]` : permission;
799
+ super(
800
+ reason ? `FGA authorization denied: ${reason}` : `FGA authorization denied: user ${userId} cannot ${permissionLabel} on ${resource.type}:${resource.id}`
801
+ );
802
+ this.name = "FGADeniedError";
803
+ this.user = user;
804
+ this.resource = resource;
805
+ this.permission = permission;
806
+ this.status = 403;
807
+ }
808
+ };
809
+ var RESOURCE_EXPANSIONS = {
810
+ stored: [
811
+ "stored-agents",
812
+ "stored-mcp-clients",
813
+ "stored-prompt-blocks",
814
+ "stored-scorers",
815
+ "stored-skills",
816
+ "stored-workspaces"
817
+ ]
818
+ };
819
+ function matchesPermission(userPermission, requiredPermission) {
820
+ if (userPermission === "*") {
821
+ return true;
822
+ }
823
+ const grantedParts = userPermission.split(":");
824
+ const requiredParts = requiredPermission.split(":");
825
+ const expandedFamilies = RESOURCE_EXPANSIONS[grantedParts[0] ?? ""];
826
+ if (expandedFamilies && expandedFamilies.includes(requiredParts[0] ?? "")) {
827
+ const aliased = [requiredParts[0], ...grantedParts.slice(1)].join(":");
828
+ return matchesPermission(aliased, requiredPermission);
829
+ }
830
+ if (grantedParts.length < 2 || requiredParts.length < 2) {
831
+ return userPermission === requiredPermission;
832
+ }
833
+ const [grantedResource, grantedAction, grantedId] = grantedParts;
834
+ const [requiredResource, requiredAction, requiredId] = requiredParts;
835
+ if (grantedResource === "*") {
836
+ if (grantedAction === "*") {
837
+ if (grantedId === void 0) {
838
+ return true;
839
+ }
840
+ return grantedId === requiredId;
841
+ }
842
+ if (grantedAction !== requiredAction) {
843
+ return false;
844
+ }
845
+ if (grantedId === void 0) {
846
+ return true;
847
+ }
848
+ return grantedId === requiredId;
849
+ }
850
+ if (grantedResource !== requiredResource) {
851
+ return false;
852
+ }
853
+ if (grantedAction === "*") {
854
+ if (grantedId === void 0) {
855
+ return true;
856
+ }
857
+ return grantedId === requiredId;
858
+ }
859
+ if (grantedAction !== requiredAction) {
860
+ return false;
861
+ }
862
+ if (grantedId === void 0) {
863
+ return true;
864
+ }
865
+ return grantedId === requiredId;
866
+ }
867
+ function resolvePermissionsFromMapping(roles, mapping) {
868
+ const permissions = /* @__PURE__ */ new Set();
869
+ const defaultPerms = mapping["_default"] ?? [];
870
+ for (const role of roles) {
871
+ const rolePerms = mapping[role];
872
+ if (rolePerms) {
873
+ for (const perm of rolePerms) {
874
+ permissions.add(perm);
875
+ }
876
+ } else {
877
+ for (const perm of defaultPerms) {
878
+ permissions.add(perm);
879
+ }
880
+ }
881
+ }
882
+ return Array.from(permissions);
883
+ }
612
884
  var DEFAULT_CACHE_TTL_MS = 60 * 1e3;
613
885
  var DEFAULT_CACHE_MAX_SIZE = 1e3;
614
886
  var MastraRBACWorkos = class {
@@ -716,7 +988,7 @@ var MastraRBACWorkos = class {
716
988
  if (roles.length === 0) {
717
989
  return this.options.roleMapping["_default"] ?? [];
718
990
  }
719
- return ee.resolvePermissionsFromMapping(roles, this.options.roleMapping);
991
+ return resolvePermissionsFromMapping(roles, this.options.roleMapping);
720
992
  }
721
993
  /**
722
994
  * Check if a user has a specific permission.
@@ -730,7 +1002,7 @@ var MastraRBACWorkos = class {
730
1002
  */
731
1003
  async hasPermission(user, permission) {
732
1004
  const permissions = await this.getPermissions(user);
733
- return permissions.some((p) => ee.matchesPermission(p, permission));
1005
+ return permissions.some((p) => matchesPermission(p, permission));
734
1006
  }
735
1007
  /**
736
1008
  * Check if a user has ALL of the specified permissions.
@@ -741,7 +1013,7 @@ var MastraRBACWorkos = class {
741
1013
  */
742
1014
  async hasAllPermissions(user, permissions) {
743
1015
  const userPermissions = await this.getPermissions(user);
744
- return permissions.every((required) => userPermissions.some((p) => ee.matchesPermission(p, required)));
1016
+ return permissions.every((required) => userPermissions.some((p) => matchesPermission(p, required)));
745
1017
  }
746
1018
  /**
747
1019
  * Check if a user has ANY of the specified permissions.
@@ -752,7 +1024,7 @@ var MastraRBACWorkos = class {
752
1024
  */
753
1025
  async hasAnyPermission(user, permissions) {
754
1026
  const userPermissions = await this.getPermissions(user);
755
- return permissions.some((required) => userPermissions.some((p) => ee.matchesPermission(p, required)));
1027
+ return permissions.some((required) => userPermissions.some((p) => matchesPermission(p, required)));
756
1028
  }
757
1029
  /**
758
1030
  * Get all available roles defined in the role mapping.
@@ -769,7 +1041,7 @@ var MastraRBACWorkos = class {
769
1041
  * Looks up the role in the roleMapping and returns its permissions.
770
1042
  */
771
1043
  async getPermissionsForRole(roleId) {
772
- return ee.resolvePermissionsFromMapping([roleId], this.options.roleMapping);
1044
+ return resolvePermissionsFromMapping([roleId], this.options.roleMapping);
773
1045
  }
774
1046
  /**
775
1047
  * Clear the roles cache.
@@ -907,7 +1179,7 @@ var MastraFGAWorkos = class {
907
1179
  async require(user, params) {
908
1180
  const permissions = Array.isArray(params.permission) ? params.permission : [params.permission];
909
1181
  if (permissions.length === 0) {
910
- throw new ee.FGADeniedError(user, params.resource, params.permission);
1182
+ throw new FGADeniedError(user, params.resource, params.permission);
911
1183
  }
912
1184
  let lastError;
913
1185
  for (const permission of permissions) {
@@ -921,13 +1193,13 @@ var MastraFGAWorkos = class {
921
1193
  const result = await this.workos.authorization.check(checkOptions);
922
1194
  if (result.authorized) return;
923
1195
  } catch (error) {
924
- if (error instanceof ee.FGADeniedError) throw error;
1196
+ if (error instanceof FGADeniedError) throw error;
925
1197
  if (isWorkOSResourceNotFoundError(error)) continue;
926
1198
  lastError = error;
927
1199
  }
928
1200
  }
929
1201
  if (lastError) throw lastError;
930
- throw new ee.FGADeniedError(user, params.resource, params.permission);
1202
+ throw new FGADeniedError(user, params.resource, params.permission);
931
1203
  }
932
1204
  /**
933
1205
  * Filter resources to only those the user has permission to access.
@@ -1511,6 +1783,38 @@ var WorkOSAdminPortal = class {
1511
1783
  return result.link;
1512
1784
  }
1513
1785
  };
1786
+ /**
1787
+ * EE Authentication Interfaces
1788
+ *
1789
+ * Enterprise interfaces for RBAC, ACL, and advanced authorization.
1790
+ *
1791
+ * @license Mastra Enterprise License - see ee/LICENSE
1792
+ * @packageDocumentation
1793
+ */
1794
+ /**
1795
+ * FGA enforcement utility for checking fine-grained authorization.
1796
+ *
1797
+ * @license Mastra Enterprise License - see ee/LICENSE
1798
+ */
1799
+ /**
1800
+ * RBAC provider implementations.
1801
+ *
1802
+ * @license Mastra Enterprise License - see ee/LICENSE
1803
+ */
1804
+ /**
1805
+ * Default implementations for EE authentication.
1806
+ *
1807
+ * @license Mastra Enterprise License - see ee/LICENSE
1808
+ */
1809
+ /**
1810
+ * @mastra/core/auth/ee
1811
+ *
1812
+ * Enterprise authentication capabilities for Mastra.
1813
+ * This code is licensed under the Mastra Enterprise License - see ee/LICENSE.
1814
+ *
1815
+ * @license Mastra Enterprise License - see ee/LICENSE
1816
+ * @packageDocumentation
1817
+ */
1514
1818
  /**
1515
1819
  * WorkOS FGA provider for Mastra.
1516
1820
  *