@mastra/auth-supabase 0.0.0-fix-generate-title-20250616171351 → 0.0.0-toolOptionTypes-20250917085558
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 +57 -1
- package/LICENSE.md +12 -4
- package/README.md +0 -5
- package/dist/chunk-FID6GZ7C.cjs +979 -0
- package/dist/chunk-FID6GZ7C.cjs.map +1 -0
- package/dist/{chunk-N62AETLJ.js → chunk-G7ML3FDV.js} +2 -0
- package/dist/chunk-G7ML3FDV.js.map +1 -0
- package/dist/{chunk-JLXWUSDO.js → chunk-NFPWCYXA.js} +34 -32
- package/dist/chunk-NFPWCYXA.js.map +1 -0
- package/dist/chunk-UYPX6MT6.cjs +32 -0
- package/dist/chunk-UYPX6MT6.cjs.map +1 -0
- package/dist/{getMachineId-bsd-IPBZSYCM.js → getMachineId-bsd-33IUL36D.js} +4 -2
- package/dist/getMachineId-bsd-33IUL36D.js.map +1 -0
- package/dist/getMachineId-bsd-7LX32PWC.cjs +25 -0
- package/dist/getMachineId-bsd-7LX32PWC.cjs.map +1 -0
- package/dist/getMachineId-darwin-2GRB6VRE.cjs +26 -0
- package/dist/getMachineId-darwin-2GRB6VRE.cjs.map +1 -0
- package/dist/{getMachineId-darwin-UJH25LDA.js → getMachineId-darwin-Y4EF6EE5.js} +4 -2
- package/dist/getMachineId-darwin-Y4EF6EE5.js.map +1 -0
- package/dist/getMachineId-linux-6PTMU7AF.cjs +21 -0
- package/dist/getMachineId-linux-6PTMU7AF.cjs.map +1 -0
- package/dist/{getMachineId-linux-YF3RLZNP.js → getMachineId-linux-ZMMZL7AP.js} +3 -1
- package/dist/getMachineId-linux-ZMMZL7AP.js.map +1 -0
- package/dist/{getMachineId-unsupported-UOUTBEEW.js → getMachineId-unsupported-AM53N2Q3.js} +3 -1
- package/dist/getMachineId-unsupported-AM53N2Q3.js.map +1 -0
- package/dist/getMachineId-unsupported-VSEGAZUW.cjs +13 -0
- package/dist/getMachineId-unsupported-VSEGAZUW.cjs.map +1 -0
- package/dist/getMachineId-win-GHQJSLHS.cjs +47 -0
- package/dist/getMachineId-win-GHQJSLHS.cjs.map +1 -0
- package/dist/{getMachineId-win-PKATJI4A.js → getMachineId-win-RSU6GP4O.js} +4 -2
- package/dist/getMachineId-win-RSU6GP4O.js.map +1 -0
- package/dist/index.cjs +108 -1201
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +15 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +101 -23
- package/dist/index.js.map +1 -0
- package/package.json +24 -10
- package/.turbo/turbo-build.log +0 -30
- package/dist/_tsup-dts-rollup.d.cts +0 -18
- package/dist/_tsup-dts-rollup.d.ts +0 -18
- package/dist/index.d.cts +0 -1
- package/eslint.config.js +0 -6
- package/src/index.test.ts +0 -152
- package/src/index.ts +0 -55
- package/tsconfig.json +0 -5
- package/vitest.config.ts +0 -8
package/src/index.test.ts
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { createClient } from '@supabase/supabase-js';
|
|
2
|
-
import type { User } from '@supabase/supabase-js';
|
|
3
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
4
|
-
import { MastraAuthSupabase } from './index';
|
|
5
|
-
|
|
6
|
-
// Mock Supabase client
|
|
7
|
-
vi.mock('@supabase/supabase-js', () => ({
|
|
8
|
-
createClient: vi.fn(),
|
|
9
|
-
}));
|
|
10
|
-
|
|
11
|
-
describe('MastraAuthSupabase', () => {
|
|
12
|
-
const mockSupabaseUrl = 'https://test.supabase.co';
|
|
13
|
-
const mockSupabaseAnonKey = 'test-anon-key';
|
|
14
|
-
const mockUser: User = {
|
|
15
|
-
id: 'test-user-id',
|
|
16
|
-
email: 'test@example.com',
|
|
17
|
-
created_at: '',
|
|
18
|
-
aud: '',
|
|
19
|
-
role: '',
|
|
20
|
-
app_metadata: {},
|
|
21
|
-
user_metadata: {},
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
let authProvider: MastraAuthSupabase;
|
|
25
|
-
let mockSupabaseClient: any;
|
|
26
|
-
|
|
27
|
-
beforeEach(() => {
|
|
28
|
-
// Reset environment variables
|
|
29
|
-
vi.resetModules();
|
|
30
|
-
process.env.SUPABASE_URL = mockSupabaseUrl;
|
|
31
|
-
process.env.SUPABASE_ANON_KEY = mockSupabaseAnonKey;
|
|
32
|
-
|
|
33
|
-
// Setup mock Supabase client
|
|
34
|
-
mockSupabaseClient = {
|
|
35
|
-
auth: {
|
|
36
|
-
getUser: vi.fn(),
|
|
37
|
-
},
|
|
38
|
-
from: vi.fn().mockReturnThis(),
|
|
39
|
-
select: vi.fn().mockReturnThis(),
|
|
40
|
-
eq: vi.fn().mockReturnThis(),
|
|
41
|
-
single: vi.fn(),
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
(createClient as any).mockReturnValue(mockSupabaseClient);
|
|
45
|
-
authProvider = new MastraAuthSupabase();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
describe('constructor', () => {
|
|
49
|
-
it('should create instance with environment variables', () => {
|
|
50
|
-
expect(createClient).toHaveBeenCalledWith(mockSupabaseUrl, mockSupabaseAnonKey);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should create instance with provided options', () => {
|
|
54
|
-
const customUrl = 'https://custom.supabase.co';
|
|
55
|
-
const customKey = 'custom-key';
|
|
56
|
-
new MastraAuthSupabase({ url: customUrl, anonKey: customKey });
|
|
57
|
-
expect(createClient).toHaveBeenCalledWith(customUrl, customKey);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('should throw error when required credentials are missing', () => {
|
|
61
|
-
delete process.env.SUPABASE_URL;
|
|
62
|
-
delete process.env.SUPABASE_ANON_KEY;
|
|
63
|
-
|
|
64
|
-
expect(() => new MastraAuthSupabase()).toThrow('Supabase URL and anon key are required');
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
describe('authenticateToken', () => {
|
|
69
|
-
it('should return user when token is valid', async () => {
|
|
70
|
-
const mockToken = 'valid-token';
|
|
71
|
-
mockSupabaseClient.auth.getUser.mockResolvedValue({
|
|
72
|
-
data: { user: mockUser },
|
|
73
|
-
error: null,
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const result = await authProvider.authenticateToken(mockToken);
|
|
77
|
-
expect(result).toEqual(mockUser);
|
|
78
|
-
expect(mockSupabaseClient.auth.getUser).toHaveBeenCalledWith(mockToken);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should return null when token is invalid', async () => {
|
|
82
|
-
const mockToken = 'invalid-token';
|
|
83
|
-
mockSupabaseClient.auth.getUser.mockResolvedValue({
|
|
84
|
-
data: { user: null },
|
|
85
|
-
error: new Error('Invalid token'),
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
const result = await authProvider.authenticateToken(mockToken);
|
|
89
|
-
expect(result).toBeNull();
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
describe('authorizeUser', () => {
|
|
94
|
-
it('should return true for admin users', async () => {
|
|
95
|
-
mockSupabaseClient.single.mockResolvedValue({
|
|
96
|
-
data: { isAdmin: true },
|
|
97
|
-
error: null,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const result = await authProvider.authorizeUser(mockUser);
|
|
101
|
-
expect(result).toBe(true);
|
|
102
|
-
expect(mockSupabaseClient.from).toHaveBeenCalledWith('users');
|
|
103
|
-
expect(mockSupabaseClient.select).toHaveBeenCalledWith('isAdmin');
|
|
104
|
-
expect(mockSupabaseClient.eq).toHaveBeenCalledWith('id', mockUser.id);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should return false for non-admin users', async () => {
|
|
108
|
-
mockSupabaseClient.single.mockResolvedValue({
|
|
109
|
-
data: { isAdmin: false },
|
|
110
|
-
error: null,
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
const result = await authProvider.authorizeUser(mockUser);
|
|
114
|
-
expect(result).toBe(false);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('should return false when user data cannot be retrieved', async () => {
|
|
118
|
-
mockSupabaseClient.single.mockResolvedValue({
|
|
119
|
-
data: null,
|
|
120
|
-
error: new Error('Database error'),
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
const result = await authProvider.authorizeUser(mockUser);
|
|
124
|
-
expect(result).toBe(false);
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('can be overridden with custom authorization logic', async () => {
|
|
129
|
-
// class CustomSupabase extends MastraAuthSupabase {
|
|
130
|
-
|
|
131
|
-
// }
|
|
132
|
-
|
|
133
|
-
const supabase = new MastraAuthSupabase({
|
|
134
|
-
async authorizeUser(user: any): Promise<boolean> {
|
|
135
|
-
// Custom authorization logic that checks for specific permissions
|
|
136
|
-
return user?.permissions?.includes('admin') ?? false;
|
|
137
|
-
},
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
// Test with admin user
|
|
141
|
-
const adminUser = { sub: 'user123', permissions: ['admin'] } as unknown as User;
|
|
142
|
-
expect(await supabase.authorizeUser(adminUser)).toBe(true);
|
|
143
|
-
|
|
144
|
-
// Test with non-admin user
|
|
145
|
-
const regularUser = { sub: 'user456', permissions: ['read'] } as unknown as User;
|
|
146
|
-
expect(await supabase.authorizeUser(regularUser)).toBe(false);
|
|
147
|
-
|
|
148
|
-
// Test with user without permissions
|
|
149
|
-
const noPermissionsUser = { sub: 'user789' } as unknown as User;
|
|
150
|
-
expect(await supabase.authorizeUser(noPermissionsUser)).toBe(false);
|
|
151
|
-
});
|
|
152
|
-
});
|
package/src/index.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { MastraAuthProviderOptions } from '@mastra/core/server';
|
|
2
|
-
import { MastraAuthProvider } from '@mastra/core/server';
|
|
3
|
-
|
|
4
|
-
import { createClient } from '@supabase/supabase-js';
|
|
5
|
-
import type { SupabaseClient, User } from '@supabase/supabase-js';
|
|
6
|
-
|
|
7
|
-
interface MastraAuthSupabaseOptions extends MastraAuthProviderOptions<User> {
|
|
8
|
-
url?: string;
|
|
9
|
-
anonKey?: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class MastraAuthSupabase extends MastraAuthProvider<User> {
|
|
13
|
-
protected supabase: SupabaseClient;
|
|
14
|
-
|
|
15
|
-
constructor(options?: MastraAuthSupabaseOptions) {
|
|
16
|
-
super({ name: options?.name ?? 'supabase' });
|
|
17
|
-
|
|
18
|
-
const supabaseUrl = options?.url ?? process.env.SUPABASE_URL;
|
|
19
|
-
const supabaseAnonKey = options?.anonKey ?? process.env.SUPABASE_ANON_KEY;
|
|
20
|
-
|
|
21
|
-
if (!supabaseUrl || !supabaseAnonKey) {
|
|
22
|
-
throw new Error(
|
|
23
|
-
'Supabase URL and anon key are required, please provide them in the options or set the environment variables SUPABASE_URL and SUPABASE_ANON_KEY',
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
this.supabase = createClient(supabaseUrl, supabaseAnonKey);
|
|
28
|
-
|
|
29
|
-
this.registerOptions(options);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async authenticateToken(token: string): Promise<User | null> {
|
|
33
|
-
const { data, error } = await this.supabase.auth.getUser(token);
|
|
34
|
-
|
|
35
|
-
if (error) {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return data.user;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async authorizeUser(user: User) {
|
|
43
|
-
// Get user data from Supabase
|
|
44
|
-
const { data, error } = await this.supabase.from('users').select('isAdmin').eq('id', user?.id).single();
|
|
45
|
-
|
|
46
|
-
if (error) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const isAdmin = data?.isAdmin;
|
|
51
|
-
|
|
52
|
-
// Check permissions based on role
|
|
53
|
-
return isAdmin;
|
|
54
|
-
}
|
|
55
|
-
}
|
package/tsconfig.json
DELETED