@agentuity/auth 0.0.108 → 0.0.110
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/AGENTS.md +82 -28
- package/README.md +259 -236
- package/dist/agentuity/config.d.ts +2386 -0
- package/dist/agentuity/config.d.ts.map +1 -0
- package/dist/agentuity/config.js +220 -0
- package/dist/agentuity/config.js.map +1 -0
- package/dist/agentuity/plugins/api-key.d.ts +152 -0
- package/dist/agentuity/plugins/api-key.d.ts.map +1 -0
- package/dist/agentuity/plugins/api-key.js +21 -0
- package/dist/agentuity/plugins/api-key.js.map +1 -0
- package/dist/agentuity/plugins/index.d.ts +23 -0
- package/dist/agentuity/plugins/index.d.ts.map +1 -0
- package/dist/agentuity/plugins/index.js +10 -0
- package/dist/agentuity/plugins/index.js.map +1 -0
- package/dist/agentuity/plugins/jwt.d.ts +34 -0
- package/dist/agentuity/plugins/jwt.d.ts.map +1 -0
- package/dist/agentuity/plugins/jwt.js +11 -0
- package/dist/agentuity/plugins/jwt.js.map +1 -0
- package/dist/agentuity/plugins/organization.d.ts +355 -0
- package/dist/agentuity/plugins/organization.d.ts.map +1 -0
- package/dist/agentuity/plugins/organization.js +12 -0
- package/dist/agentuity/plugins/organization.js.map +1 -0
- package/dist/agentuity/react.d.ts +1375 -0
- package/dist/agentuity/react.d.ts.map +1 -0
- package/dist/agentuity/react.js +206 -0
- package/dist/agentuity/react.js.map +1 -0
- package/dist/agentuity/server.d.ts +220 -0
- package/dist/agentuity/server.d.ts.map +1 -0
- package/dist/agentuity/server.js +505 -0
- package/dist/agentuity/server.js.map +1 -0
- package/dist/agentuity/types.d.ts +172 -0
- package/dist/agentuity/types.d.ts.map +1 -0
- package/dist/agentuity/types.js +7 -0
- package/dist/agentuity/types.js.map +1 -0
- package/dist/index.d.ts +31 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +33 -8
- package/dist/index.js.map +1 -1
- package/dist/schema.d.ts +2922 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +223 -0
- package/dist/schema.js.map +1 -0
- package/dist/types.d.ts +14 -18
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/package.json +15 -39
- package/src/agentuity/config.ts +401 -0
- package/src/agentuity/plugins/api-key.ts +158 -0
- package/src/agentuity/plugins/index.ts +35 -0
- package/src/agentuity/plugins/jwt.ts +30 -0
- package/src/agentuity/plugins/organization.ts +345 -0
- package/src/agentuity/react.tsx +328 -0
- package/src/agentuity/server.ts +734 -0
- package/src/agentuity/types.ts +201 -0
- package/src/index.ts +76 -8
- package/src/schema.ts +270 -0
- package/src/types.ts +14 -22
- package/test/agentuity/config.test.ts +621 -0
- package/test/agentuity/server.test.ts +537 -0
- package/test/schema.test.ts +147 -0
- package/tsconfig.json +3 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/auth0/client.d.ts +0 -44
- package/dist/auth0/client.d.ts.map +0 -1
- package/dist/auth0/client.js +0 -79
- package/dist/auth0/client.js.map +0 -1
- package/dist/auth0/index.d.ts +0 -35
- package/dist/auth0/index.d.ts.map +0 -1
- package/dist/auth0/index.js +0 -38
- package/dist/auth0/index.js.map +0 -1
- package/dist/auth0/server.d.ts +0 -91
- package/dist/auth0/server.d.ts.map +0 -1
- package/dist/auth0/server.js +0 -237
- package/dist/auth0/server.js.map +0 -1
- package/dist/clerk/client.d.ts +0 -42
- package/dist/clerk/client.d.ts.map +0 -1
- package/dist/clerk/client.js +0 -65
- package/dist/clerk/client.js.map +0 -1
- package/dist/clerk/index.d.ts +0 -37
- package/dist/clerk/index.d.ts.map +0 -1
- package/dist/clerk/index.js +0 -35
- package/dist/clerk/index.js.map +0 -1
- package/dist/clerk/server.d.ts +0 -55
- package/dist/clerk/server.d.ts.map +0 -1
- package/dist/clerk/server.js +0 -111
- package/dist/clerk/server.js.map +0 -1
- package/docs/adding-providers.md +0 -261
- package/src/auth0/client.tsx +0 -109
- package/src/auth0/index.ts +0 -40
- package/src/auth0/server.ts +0 -378
- package/src/clerk/client.tsx +0 -86
- package/src/clerk/index.ts +0 -37
- package/src/clerk/server.ts +0 -168
- package/test/clerk-client.test.tsx +0 -21
- package/test/clerk-server.test.ts +0 -51
package/src/clerk/server.ts
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Clerk server-side authentication middleware for Hono.
|
|
3
|
-
*
|
|
4
|
-
* @module clerk/server
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { createMiddleware as createHonoMiddleware } from 'hono/factory';
|
|
8
|
-
import { createClerkClient, verifyToken } from '@clerk/backend';
|
|
9
|
-
import type { User } from '@clerk/backend';
|
|
10
|
-
import type { AgentuityAuth, AgentuityAuthUser } from '../types';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Clerk JWT payload structure.
|
|
14
|
-
*/
|
|
15
|
-
export interface ClerkJWTPayload {
|
|
16
|
-
/** Subject (user ID) */
|
|
17
|
-
sub: string;
|
|
18
|
-
/** Additional claims */
|
|
19
|
-
[key: string]: unknown;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Environment type for Clerk middleware - provides typed context variables.
|
|
24
|
-
*/
|
|
25
|
-
export type ClerkEnv = {
|
|
26
|
-
Variables: {
|
|
27
|
-
auth: AgentuityAuth<User, ClerkJWTPayload>;
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Options for Clerk middleware.
|
|
33
|
-
*/
|
|
34
|
-
export interface ClerkMiddlewareOptions {
|
|
35
|
-
/** Clerk secret key (defaults to process.env.CLERK_SECRET_KEY) */
|
|
36
|
-
secretKey?: string;
|
|
37
|
-
|
|
38
|
-
/** Custom token extractor function */
|
|
39
|
-
getToken?: (authHeader: string) => string;
|
|
40
|
-
|
|
41
|
-
/** Clerk publishable key for token verification */
|
|
42
|
-
publishableKey?: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Create Hono middleware for Clerk authentication.
|
|
47
|
-
*
|
|
48
|
-
* This middleware:
|
|
49
|
-
* - Extracts and validates JWT tokens from Authorization header
|
|
50
|
-
* - Returns 401 if token is missing or invalid
|
|
51
|
-
* - Exposes authenticated user via c.var.auth
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```typescript
|
|
55
|
-
* import { createMiddleware } from '@agentuity/auth/clerk';
|
|
56
|
-
*
|
|
57
|
-
* router.get('/api/profile', createMiddleware(), async (c) => {
|
|
58
|
-
* const user = await c.var.auth.getUser();
|
|
59
|
-
* return c.json({ email: user.email });
|
|
60
|
-
* });
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
export function createMiddleware(options: ClerkMiddlewareOptions = {}) {
|
|
64
|
-
const secretKey = options.secretKey || process.env.CLERK_SECRET_KEY;
|
|
65
|
-
const publishableKey =
|
|
66
|
-
options.publishableKey ||
|
|
67
|
-
process.env.AGENTUITY_PUBLIC_CLERK_PUBLISHABLE_KEY ||
|
|
68
|
-
process.env.CLERK_PUBLISHABLE_KEY;
|
|
69
|
-
|
|
70
|
-
if (!secretKey) {
|
|
71
|
-
console.error(
|
|
72
|
-
'[Clerk Auth] CLERK_SECRET_KEY is not set. Add it to your .env file or pass secretKey option to createMiddleware()'
|
|
73
|
-
);
|
|
74
|
-
throw new Error(
|
|
75
|
-
'Clerk secret key is required (set CLERK_SECRET_KEY or pass secretKey option)'
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (!publishableKey) {
|
|
80
|
-
console.warn(
|
|
81
|
-
'[Clerk Auth] AGENTUITY_PUBLIC_CLERK_PUBLISHABLE_KEY is not set. Token validation may fail. Add it to your .env file.'
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Create Clerk client instance
|
|
86
|
-
const clerkClient = createClerkClient({ secretKey });
|
|
87
|
-
|
|
88
|
-
return createHonoMiddleware<ClerkEnv>(async (c, next) => {
|
|
89
|
-
const authHeader = c.req.header('Authorization');
|
|
90
|
-
|
|
91
|
-
if (!authHeader) {
|
|
92
|
-
return c.json({ error: 'Unauthorized' }, 401);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
try {
|
|
96
|
-
// Extract token from Bearer header
|
|
97
|
-
let token: string;
|
|
98
|
-
if (options.getToken) {
|
|
99
|
-
token = options.getToken(authHeader);
|
|
100
|
-
} else {
|
|
101
|
-
// Validate Authorization scheme is Bearer
|
|
102
|
-
if (!authHeader.match(/^Bearer\s+/i)) {
|
|
103
|
-
return c.json({ error: 'Unauthorized' }, 401);
|
|
104
|
-
}
|
|
105
|
-
token = authHeader.replace(/^Bearer\s+/i, '');
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Ensure token is not empty
|
|
109
|
-
if (!token || token.trim().length === 0) {
|
|
110
|
-
return c.json({ error: 'Unauthorized' }, 401);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Verify token with Clerk (delegates validation to provider)
|
|
114
|
-
const payload = (await verifyToken(token, {
|
|
115
|
-
secretKey,
|
|
116
|
-
})) as ClerkJWTPayload;
|
|
117
|
-
|
|
118
|
-
// Validate payload has required subject claim
|
|
119
|
-
if (!payload.sub || typeof payload.sub !== 'string') {
|
|
120
|
-
throw new Error('Invalid token: missing or invalid subject claim');
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Memoize user fetch to avoid multiple API calls
|
|
124
|
-
let cachedUser: AgentuityAuthUser<User> | null = null;
|
|
125
|
-
|
|
126
|
-
// Create auth object with Clerk user and payload types
|
|
127
|
-
const auth: AgentuityAuth<User, ClerkJWTPayload> = {
|
|
128
|
-
async getUser() {
|
|
129
|
-
if (cachedUser) {
|
|
130
|
-
return cachedUser;
|
|
131
|
-
}
|
|
132
|
-
const user = await clerkClient.users.getUser(payload.sub);
|
|
133
|
-
cachedUser = mapClerkUserToAgentuityUser(user);
|
|
134
|
-
return cachedUser;
|
|
135
|
-
},
|
|
136
|
-
|
|
137
|
-
async getToken() {
|
|
138
|
-
return token;
|
|
139
|
-
},
|
|
140
|
-
|
|
141
|
-
raw: payload,
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
c.set('auth', auth);
|
|
145
|
-
await next();
|
|
146
|
-
} catch (error) {
|
|
147
|
-
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
148
|
-
const errorCode =
|
|
149
|
-
error && typeof error === 'object' && 'code' in error && typeof error.code === 'string'
|
|
150
|
-
? error.code
|
|
151
|
-
: 'CLERK_AUTH_ERROR';
|
|
152
|
-
console.error(`[Clerk Auth] Authentication failed: ${errorCode} - ${errorMessage}`);
|
|
153
|
-
return c.json({ error: 'Unauthorized' }, 401);
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Map Clerk User to AgentuityAuthUser.
|
|
160
|
-
*/
|
|
161
|
-
function mapClerkUserToAgentuityUser(clerkUser: User): AgentuityAuthUser<User> {
|
|
162
|
-
return {
|
|
163
|
-
id: clerkUser.id,
|
|
164
|
-
name: `${clerkUser.firstName || ''} ${clerkUser.lastName || ''}`.trim() || undefined,
|
|
165
|
-
email: clerkUser.emailAddresses[0]?.emailAddress,
|
|
166
|
-
raw: clerkUser,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { describe, test, expect } from 'bun:test';
|
|
3
|
-
import { AgentuityClerk } from '../src/clerk/client';
|
|
4
|
-
|
|
5
|
-
describe('AgentuityClerk', () => {
|
|
6
|
-
test('exports AgentuityClerk component', () => {
|
|
7
|
-
expect(AgentuityClerk).toBeDefined();
|
|
8
|
-
expect(typeof AgentuityClerk).toBe('function');
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test('component props interface is correct', () => {
|
|
12
|
-
// Type test - will fail at compile time if interface changes
|
|
13
|
-
const validProps = {
|
|
14
|
-
children: null,
|
|
15
|
-
useAuth: (() => ({ getToken: async () => null, isLoaded: true })) as any,
|
|
16
|
-
refreshInterval: 60000,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
expect(validProps).toBeDefined();
|
|
20
|
-
});
|
|
21
|
-
});
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { describe, test, expect, beforeEach } from 'bun:test';
|
|
2
|
-
import { Hono } from 'hono';
|
|
3
|
-
import { createMiddleware } from '../src/clerk/server';
|
|
4
|
-
|
|
5
|
-
describe('Clerk server middleware', () => {
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
process.env.CLERK_SECRET_KEY = 'sk_test_secret';
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
test('returns 401 when Authorization header is missing', async () => {
|
|
11
|
-
const app = new Hono();
|
|
12
|
-
app.use('/protected', createMiddleware());
|
|
13
|
-
app.get('/protected', (c) => c.json({ success: true }));
|
|
14
|
-
|
|
15
|
-
const res = await app.request('/protected', {
|
|
16
|
-
method: 'GET',
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
expect(res.status).toBe(401);
|
|
20
|
-
const body = await res.json();
|
|
21
|
-
expect(body).toEqual({ error: 'Unauthorized' });
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('throws error when CLERK_SECRET_KEY is missing', () => {
|
|
25
|
-
delete process.env.CLERK_SECRET_KEY;
|
|
26
|
-
|
|
27
|
-
expect(() => createMiddleware()).toThrow('Clerk secret key is required');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('creates middleware function', () => {
|
|
31
|
-
const middleware = createMiddleware();
|
|
32
|
-
expect(typeof middleware).toBe('function');
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test('middleware rejects requests with invalid tokens', async () => {
|
|
36
|
-
const app = new Hono();
|
|
37
|
-
app.use('/protected', createMiddleware());
|
|
38
|
-
app.get('/protected', (c) => c.json({ success: true }));
|
|
39
|
-
|
|
40
|
-
const res = await app.request('/protected', {
|
|
41
|
-
method: 'GET',
|
|
42
|
-
headers: {
|
|
43
|
-
Authorization: 'Bearer invalid_token',
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
expect(res.status).toBe(401);
|
|
48
|
-
const body = await res.json();
|
|
49
|
-
expect(body).toEqual({ error: 'Unauthorized' });
|
|
50
|
-
});
|
|
51
|
-
});
|