@agentuity/auth 0.0.109 → 0.0.111

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 (95) hide show
  1. package/AGENTS.md +82 -28
  2. package/README.md +259 -236
  3. package/dist/agentuity/config.d.ts +2386 -0
  4. package/dist/agentuity/config.d.ts.map +1 -0
  5. package/dist/agentuity/config.js +220 -0
  6. package/dist/agentuity/config.js.map +1 -0
  7. package/dist/agentuity/plugins/api-key.d.ts +152 -0
  8. package/dist/agentuity/plugins/api-key.d.ts.map +1 -0
  9. package/dist/agentuity/plugins/api-key.js +21 -0
  10. package/dist/agentuity/plugins/api-key.js.map +1 -0
  11. package/dist/agentuity/plugins/index.d.ts +23 -0
  12. package/dist/agentuity/plugins/index.d.ts.map +1 -0
  13. package/dist/agentuity/plugins/index.js +10 -0
  14. package/dist/agentuity/plugins/index.js.map +1 -0
  15. package/dist/agentuity/plugins/jwt.d.ts +34 -0
  16. package/dist/agentuity/plugins/jwt.d.ts.map +1 -0
  17. package/dist/agentuity/plugins/jwt.js +11 -0
  18. package/dist/agentuity/plugins/jwt.js.map +1 -0
  19. package/dist/agentuity/plugins/organization.d.ts +355 -0
  20. package/dist/agentuity/plugins/organization.d.ts.map +1 -0
  21. package/dist/agentuity/plugins/organization.js +12 -0
  22. package/dist/agentuity/plugins/organization.js.map +1 -0
  23. package/dist/agentuity/react.d.ts +1375 -0
  24. package/dist/agentuity/react.d.ts.map +1 -0
  25. package/dist/agentuity/react.js +206 -0
  26. package/dist/agentuity/react.js.map +1 -0
  27. package/dist/agentuity/server.d.ts +220 -0
  28. package/dist/agentuity/server.d.ts.map +1 -0
  29. package/dist/agentuity/server.js +505 -0
  30. package/dist/agentuity/server.js.map +1 -0
  31. package/dist/agentuity/types.d.ts +172 -0
  32. package/dist/agentuity/types.d.ts.map +1 -0
  33. package/dist/agentuity/types.js +7 -0
  34. package/dist/agentuity/types.js.map +1 -0
  35. package/dist/index.d.ts +31 -8
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +33 -8
  38. package/dist/index.js.map +1 -1
  39. package/dist/schema.d.ts +2922 -0
  40. package/dist/schema.d.ts.map +1 -0
  41. package/dist/schema.js +223 -0
  42. package/dist/schema.js.map +1 -0
  43. package/dist/types.d.ts +14 -18
  44. package/dist/types.d.ts.map +1 -1
  45. package/dist/types.js +1 -1
  46. package/package.json +15 -39
  47. package/src/agentuity/config.ts +401 -0
  48. package/src/agentuity/plugins/api-key.ts +158 -0
  49. package/src/agentuity/plugins/index.ts +35 -0
  50. package/src/agentuity/plugins/jwt.ts +30 -0
  51. package/src/agentuity/plugins/organization.ts +345 -0
  52. package/src/agentuity/react.tsx +328 -0
  53. package/src/agentuity/server.ts +734 -0
  54. package/src/agentuity/types.ts +201 -0
  55. package/src/index.ts +76 -8
  56. package/src/schema.ts +270 -0
  57. package/src/types.ts +14 -22
  58. package/test/agentuity/config.test.ts +621 -0
  59. package/test/agentuity/server.test.ts +537 -0
  60. package/test/schema.test.ts +147 -0
  61. package/tsconfig.json +3 -2
  62. package/tsconfig.tsbuildinfo +1 -1
  63. package/dist/auth0/client.d.ts +0 -44
  64. package/dist/auth0/client.d.ts.map +0 -1
  65. package/dist/auth0/client.js +0 -79
  66. package/dist/auth0/client.js.map +0 -1
  67. package/dist/auth0/index.d.ts +0 -35
  68. package/dist/auth0/index.d.ts.map +0 -1
  69. package/dist/auth0/index.js +0 -38
  70. package/dist/auth0/index.js.map +0 -1
  71. package/dist/auth0/server.d.ts +0 -91
  72. package/dist/auth0/server.d.ts.map +0 -1
  73. package/dist/auth0/server.js +0 -237
  74. package/dist/auth0/server.js.map +0 -1
  75. package/dist/clerk/client.d.ts +0 -42
  76. package/dist/clerk/client.d.ts.map +0 -1
  77. package/dist/clerk/client.js +0 -65
  78. package/dist/clerk/client.js.map +0 -1
  79. package/dist/clerk/index.d.ts +0 -37
  80. package/dist/clerk/index.d.ts.map +0 -1
  81. package/dist/clerk/index.js +0 -35
  82. package/dist/clerk/index.js.map +0 -1
  83. package/dist/clerk/server.d.ts +0 -55
  84. package/dist/clerk/server.d.ts.map +0 -1
  85. package/dist/clerk/server.js +0 -111
  86. package/dist/clerk/server.js.map +0 -1
  87. package/docs/adding-providers.md +0 -261
  88. package/src/auth0/client.tsx +0 -109
  89. package/src/auth0/index.ts +0 -40
  90. package/src/auth0/server.ts +0 -378
  91. package/src/clerk/client.tsx +0 -86
  92. package/src/clerk/index.ts +0 -37
  93. package/src/clerk/server.ts +0 -168
  94. package/test/clerk-client.test.tsx +0 -21
  95. package/test/clerk-server.test.ts +0 -51
@@ -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
- });