@mastra/auth-workos 0.10.5-alpha.0 → 0.10.5-alpha.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @mastra/auth-workos
2
2
 
3
+ ## 0.10.5-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#7343](https://github.com/mastra-ai/mastra/pull/7343) [`de3cbc6`](https://github.com/mastra-ai/mastra/commit/de3cbc61079211431bd30487982ea3653517278e) Thanks [@LekoArts](https://github.com/LekoArts)! - Update the `package.json` file to include additional fields like `repository`, `homepage` or `files`.
8
+
9
+ - Updated dependencies [[`de3cbc6`](https://github.com/mastra-ai/mastra/commit/de3cbc61079211431bd30487982ea3653517278e)]:
10
+ - @mastra/auth@0.1.3-alpha.0
11
+
3
12
  ## 0.10.5-alpha.0
4
13
 
5
14
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/auth-workos",
3
- "version": "0.10.5-alpha.0",
3
+ "version": "0.10.5-alpha.1",
4
4
  "description": "Mastra WorkOS Auth integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -21,7 +21,7 @@
21
21
  "license": "Apache-2.0",
22
22
  "dependencies": {
23
23
  "@workos-inc/node": "^7.69.1",
24
- "@mastra/auth": "0.1.2"
24
+ "@mastra/auth": "0.1.3-alpha.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/node": "^20.19.0",
@@ -29,9 +29,22 @@
29
29
  "tsup": "^8.5.0",
30
30
  "typescript": "^5.8.3",
31
31
  "vitest": "^3.2.4",
32
- "@mastra/core": "0.15.3-alpha.4",
32
+ "@internal/types-builder": "0.0.9",
33
33
  "@internal/lint": "0.0.34",
34
- "@internal/types-builder": "0.0.9"
34
+ "@mastra/core": "0.15.3-alpha.5"
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "CHANGELOG.md"
39
+ ],
40
+ "homepage": "https://mastra.ai",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/mastra-ai/mastra.git",
44
+ "directory": "auth/workos"
45
+ },
46
+ "bugs": {
47
+ "url": "https://github.com/mastra-ai/mastra/issues"
35
48
  },
36
49
  "scripts": {
37
50
  "build": "tsup --silent --config tsup.config.ts",
@@ -1,4 +0,0 @@
1
-
2
- > @mastra/auth-workos@0.10.5-alpha.0 build /home/runner/work/mastra/mastra/auth/workos
3
- > tsup --silent --config tsup.config.ts
4
-
package/eslint.config.js DELETED
@@ -1,6 +0,0 @@
1
- import { createConfig } from '@internal/lint/eslint';
2
-
3
- const config = await createConfig();
4
-
5
- /** @type {import("eslint").Linter.Config[]} */
6
- export default [...config];
package/src/index.test.ts DELETED
@@ -1,185 +0,0 @@
1
- import type { JwtPayload } from '@mastra/auth';
2
- import { verifyJwks } from '@mastra/auth';
3
- import { WorkOS } from '@workos-inc/node';
4
- import { describe, it, expect, vi, beforeEach } from 'vitest';
5
- import { MastraAuthWorkos } from './index';
6
-
7
- // Mock the WorkOS class
8
- vi.mock('@workos-inc/node', () => ({
9
- WorkOS: vi.fn().mockImplementation(() => ({
10
- userManagement: {
11
- getJwksUrl: vi.fn().mockReturnValue('https://mock-jwks-url'),
12
- listOrganizationMemberships: vi.fn().mockResolvedValue({
13
- data: [{ role: { slug: 'admin' } }, { role: { slug: 'member' } }],
14
- }),
15
- },
16
- })),
17
- }));
18
-
19
- // Mock the verifyJwks function
20
- vi.mock('@mastra/auth', () => ({
21
- verifyJwks: vi.fn().mockResolvedValue({
22
- sub: 'user123',
23
- email: 'test@example.com',
24
- } as JwtPayload),
25
- }));
26
-
27
- describe('MastraAuthWorkos', () => {
28
- const mockApiKey = 'test-api-key';
29
- const mockClientId = 'test-client-id';
30
-
31
- beforeEach(() => {
32
- vi.clearAllMocks();
33
- // Reset environment variables
34
- delete process.env.WORKOS_API_KEY;
35
- delete process.env.WORKOS_CLIENT_ID;
36
- });
37
-
38
- describe('constructor', () => {
39
- it('should initialize with provided options', () => {
40
- new MastraAuthWorkos({
41
- apiKey: mockApiKey,
42
- clientId: mockClientId,
43
- });
44
-
45
- expect(WorkOS).toHaveBeenCalledWith(mockApiKey, {
46
- clientId: mockClientId,
47
- });
48
- });
49
-
50
- it('should initialize with environment variables', () => {
51
- process.env.WORKOS_API_KEY = mockApiKey;
52
- process.env.WORKOS_CLIENT_ID = mockClientId;
53
-
54
- new MastraAuthWorkos();
55
-
56
- expect(WorkOS).toHaveBeenCalledWith(mockApiKey, {
57
- clientId: mockClientId,
58
- });
59
- });
60
-
61
- it('should throw error when neither options nor environment variables are provided', () => {
62
- expect(() => new MastraAuthWorkos()).toThrow('WorkOS API key and client ID are required');
63
- });
64
- });
65
-
66
- describe('authenticateToken', () => {
67
- it('should authenticate a valid token', async () => {
68
- const auth = new MastraAuthWorkos({
69
- apiKey: mockApiKey,
70
- clientId: mockClientId,
71
- });
72
-
73
- const mockToken = 'valid-token';
74
- const result = await auth.authenticateToken(mockToken);
75
-
76
- expect(verifyJwks).toHaveBeenCalledWith(mockToken, 'https://mock-jwks-url');
77
- expect(result).toEqual({
78
- sub: 'user123',
79
- email: 'test@example.com',
80
- });
81
- });
82
-
83
- it('should return null for invalid token', async () => {
84
- vi.mocked(verifyJwks).mockResolvedValueOnce(null as unknown as JwtPayload);
85
-
86
- const auth = new MastraAuthWorkos({
87
- apiKey: mockApiKey,
88
- clientId: mockClientId,
89
- });
90
-
91
- const result = await auth.authenticateToken('invalid-token');
92
- expect(result).toBeNull();
93
- });
94
- });
95
-
96
- describe('authorizeUser', () => {
97
- it('should return true for admin users', async () => {
98
- const auth = new MastraAuthWorkos({
99
- apiKey: mockApiKey,
100
- clientId: mockClientId,
101
- });
102
-
103
- const result = await auth.authorizeUser({
104
- sub: 'user123',
105
- email: 'test@example.com',
106
- });
107
-
108
- expect(result).toBe(true);
109
- });
110
-
111
- it('should return false for non-admin users', async () => {
112
- vi.mocked(WorkOS).mockImplementationOnce(
113
- () =>
114
- ({
115
- userManagement: {
116
- getJwksUrl: vi.fn().mockReturnValue('https://mock-jwks-url'),
117
- listOrganizationMemberships: vi.fn().mockResolvedValue({
118
- data: [{ role: { slug: 'member' } }],
119
- }),
120
- },
121
- }) as unknown as WorkOS,
122
- );
123
-
124
- const auth = new MastraAuthWorkos({
125
- apiKey: mockApiKey,
126
- clientId: mockClientId,
127
- });
128
-
129
- const result = await auth.authorizeUser({
130
- sub: 'user123',
131
- email: 'test@example.com',
132
- });
133
-
134
- expect(result).toBe(false);
135
- });
136
-
137
- it('should return false for falsy user', async () => {
138
- vi.mocked(WorkOS).mockImplementationOnce(
139
- () =>
140
- ({
141
- userManagement: {
142
- getJwksUrl: vi.fn().mockReturnValue('https://mock-jwks-url'),
143
- listOrganizationMemberships: vi.fn().mockResolvedValue({
144
- data: [], // Empty data array means no roles
145
- }),
146
- },
147
- }) as unknown as WorkOS,
148
- );
149
-
150
- const auth = new MastraAuthWorkos({
151
- apiKey: mockApiKey,
152
- clientId: mockClientId,
153
- });
154
-
155
- const result = await auth.authorizeUser({
156
- sub: '',
157
- email: '',
158
- });
159
- expect(result).toBe(false);
160
- });
161
- });
162
-
163
- it('can be overridden with custom authorization logic', async () => {
164
- const workos = new MastraAuthWorkos({
165
- apiKey: mockApiKey,
166
- clientId: mockClientId,
167
- async authorizeUser(user: any): Promise<boolean> {
168
- // Custom authorization logic that checks for specific permissions
169
- return user?.permissions?.includes('admin') ?? false;
170
- },
171
- });
172
-
173
- // Test with admin user
174
- const adminUser = { sub: 'user123', permissions: ['admin'] };
175
- expect(await workos.authorizeUser(adminUser)).toBe(true);
176
-
177
- // Test with non-admin user
178
- const regularUser = { sub: 'user456', permissions: ['read'] };
179
- expect(await workos.authorizeUser(regularUser)).toBe(false);
180
-
181
- // Test with user without permissions
182
- const noPermissionsUser = { sub: 'user789' };
183
- expect(await workos.authorizeUser(noPermissionsUser)).toBe(false);
184
- });
185
- });
package/src/index.ts DELETED
@@ -1,57 +0,0 @@
1
- import { verifyJwks } from '@mastra/auth';
2
- import type { JwtPayload } from '@mastra/auth';
3
- import type { MastraAuthProviderOptions } from '@mastra/core/server';
4
- import { MastraAuthProvider } from '@mastra/core/server';
5
- import { WorkOS } from '@workos-inc/node';
6
-
7
- type WorkosUser = JwtPayload;
8
-
9
- interface MastraAuthWorkosOptions extends MastraAuthProviderOptions<WorkosUser> {
10
- apiKey?: string;
11
- clientId?: string;
12
- }
13
-
14
- export class MastraAuthWorkos extends MastraAuthProvider<WorkosUser> {
15
- protected workos: WorkOS;
16
-
17
- constructor(options?: MastraAuthWorkosOptions) {
18
- super({ name: options?.name ?? 'workos' });
19
-
20
- const apiKey = options?.apiKey ?? process.env.WORKOS_API_KEY;
21
- const clientId = options?.clientId ?? process.env.WORKOS_CLIENT_ID;
22
-
23
- if (!apiKey || !clientId) {
24
- throw new Error(
25
- 'WorkOS API key and client ID are required, please provide them in the options or set the environment variables WORKOS_API_KEY and WORKOS_CLIENT_ID',
26
- );
27
- }
28
-
29
- this.workos = new WorkOS(apiKey, {
30
- clientId,
31
- });
32
-
33
- this.registerOptions(options);
34
- }
35
-
36
- async authenticateToken(token: string): Promise<WorkosUser | null> {
37
- const jwksUri = this.workos.userManagement.getJwksUrl(process.env.WORKOS_CLIENT_ID!);
38
- const user = await verifyJwks(token, jwksUri);
39
- return user;
40
- }
41
-
42
- async authorizeUser(user: WorkosUser) {
43
- if (!user) {
44
- return false;
45
- }
46
-
47
- const org = await this.workos.userManagement.listOrganizationMemberships({
48
- userId: user.sub,
49
- });
50
-
51
- const roles = org.data.map(org => org.role);
52
-
53
- const isAdmin = roles.some(role => role.slug === 'admin');
54
-
55
- return isAdmin;
56
- }
57
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": ["./tsconfig.json", "../../tsconfig.build.json"],
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src"
6
- },
7
- "include": ["src/**/*"],
8
- "exclude": ["node_modules", "**/*.test.ts", "src/**/*.mock.ts"]
9
- }
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.node.json",
3
- "include": ["src/**/*", "tsup.config.ts"],
4
- "exclude": ["node_modules", "**/*.test.ts"]
5
- }
package/tsup.config.ts DELETED
@@ -1,17 +0,0 @@
1
- import { generateTypes } from '@internal/types-builder';
2
- import { defineConfig } from 'tsup';
3
-
4
- export default defineConfig({
5
- entry: ['src/index.ts'],
6
- format: ['esm', 'cjs'],
7
- clean: true,
8
- dts: false,
9
- splitting: true,
10
- treeshake: {
11
- preset: 'smallest',
12
- },
13
- sourcemap: true,
14
- onSuccess: async () => {
15
- await generateTypes(process.cwd());
16
- },
17
- });
package/vitest.config.ts DELETED
@@ -1,8 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- globals: true,
6
- include: ['src/**/*.test.ts'],
7
- },
8
- });