@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
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth types for @agentuity/auth.
|
|
3
|
+
*
|
|
4
|
+
* @module agentuity/types
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Session as BetterAuthSession, User as BetterAuthUser } from 'better-auth';
|
|
8
|
+
import type { AgentuityAuth } from '../types';
|
|
9
|
+
|
|
10
|
+
// =============================================================================
|
|
11
|
+
// Canonical User/Session Types
|
|
12
|
+
// =============================================================================
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Canonical authenticated user type for Agentuity Auth.
|
|
16
|
+
*
|
|
17
|
+
* This is an alias for BetterAuth's `User` type and represents the shape of
|
|
18
|
+
* the `user` object you receive from:
|
|
19
|
+
*
|
|
20
|
+
* - `AuthInterface#getUser()` / `c.var.auth.getUser()` on the server
|
|
21
|
+
* - `c.var.user` in Hono route handlers
|
|
22
|
+
* - React hooks and context (`useAuth().user`) in `@agentuity/auth/react`
|
|
23
|
+
*
|
|
24
|
+
* Common fields include:
|
|
25
|
+
* - `id` – Stable user identifier
|
|
26
|
+
* - `email` – Primary email address
|
|
27
|
+
* - `name` – Display name
|
|
28
|
+
* - `image` – Avatar URL (if configured)
|
|
29
|
+
* - `createdAt` / `updatedAt` – Timestamps
|
|
30
|
+
*
|
|
31
|
+
* The exact fields are defined by BetterAuth and may be extended by plugins.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* Prefer using this `AuthUser` alias instead of referring to BetterAuth's
|
|
35
|
+
* `User` type directly so your code stays aligned with Agentuity's auth
|
|
36
|
+
* abstractions.
|
|
37
|
+
*/
|
|
38
|
+
export type AuthUser = BetterAuthUser;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Auth session type.
|
|
42
|
+
* Extends BetterAuth's Session with organization plugin fields.
|
|
43
|
+
*/
|
|
44
|
+
export type AuthSession = BetterAuthSession & {
|
|
45
|
+
/** Active organization ID from the organization plugin */
|
|
46
|
+
activeOrganizationId?: string;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Auth context containing user, session, and org data.
|
|
51
|
+
* This is the full auth context available on AgentContext.auth and c.var.auth.
|
|
52
|
+
* Session may be null for API key authentication.
|
|
53
|
+
*/
|
|
54
|
+
export interface AuthContext<TUser = AuthUser, TSession = AuthSession | null> {
|
|
55
|
+
user: TUser;
|
|
56
|
+
session: TSession;
|
|
57
|
+
org: AuthOrgContext | null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Organization context from the organization plugin.
|
|
62
|
+
*/
|
|
63
|
+
export interface AuthOrgContext {
|
|
64
|
+
/** Organization ID */
|
|
65
|
+
id: string;
|
|
66
|
+
/** Organization slug (URL-friendly identifier) */
|
|
67
|
+
slug?: string | null;
|
|
68
|
+
/** Organization display name */
|
|
69
|
+
name?: string | null;
|
|
70
|
+
/** Member's role in this organization (e.g., 'owner', 'admin', 'member') */
|
|
71
|
+
role?: string | null;
|
|
72
|
+
/** Member ID for this user in this organization */
|
|
73
|
+
memberId?: string | null;
|
|
74
|
+
/** Organization metadata (if enabled) */
|
|
75
|
+
metadata?: unknown;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// =============================================================================
|
|
79
|
+
// API Key Types
|
|
80
|
+
// =============================================================================
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* API key permissions format.
|
|
84
|
+
* Maps resource names to arrays of allowed actions.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const permissions: AuthApiKeyPermissions = {
|
|
89
|
+
* project: ['read', 'write'],
|
|
90
|
+
* user: ['read'],
|
|
91
|
+
* admin: ['*'], // wildcard - all actions
|
|
92
|
+
* };
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export type AuthApiKeyPermissions = Record<string, string[]>;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* API key context when request is authenticated via API key.
|
|
99
|
+
*/
|
|
100
|
+
export interface AuthApiKeyContext {
|
|
101
|
+
/** API key ID */
|
|
102
|
+
id: string;
|
|
103
|
+
/** Display name of the API key */
|
|
104
|
+
name?: string | null;
|
|
105
|
+
/** Permissions associated with this API key */
|
|
106
|
+
permissions: AuthApiKeyPermissions;
|
|
107
|
+
/** User ID the API key belongs to */
|
|
108
|
+
userId?: string | null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Authentication method used for the current request.
|
|
113
|
+
*/
|
|
114
|
+
export type AuthMethod = 'session' | 'api-key' | 'bearer';
|
|
115
|
+
|
|
116
|
+
// =============================================================================
|
|
117
|
+
// Extended Auth Interface
|
|
118
|
+
// =============================================================================
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Organization helpers available on the auth context.
|
|
122
|
+
*/
|
|
123
|
+
export interface AuthOrgHelpers {
|
|
124
|
+
/** Active organization context if available, null otherwise */
|
|
125
|
+
org: AuthOrgContext | null;
|
|
126
|
+
|
|
127
|
+
/** Returns active org or null (never throws) */
|
|
128
|
+
getOrg(): Promise<AuthOrgContext | null>;
|
|
129
|
+
|
|
130
|
+
/** Convenience accessor for the member's role on the active org */
|
|
131
|
+
getOrgRole(): Promise<string | null>;
|
|
132
|
+
|
|
133
|
+
/** True if the current member's role is one of the provided roles */
|
|
134
|
+
hasOrgRole(...roles: string[]): Promise<boolean>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* API key helpers available on the auth context.
|
|
139
|
+
*/
|
|
140
|
+
export interface AuthApiKeyHelpers {
|
|
141
|
+
/** How this request was authenticated */
|
|
142
|
+
authMethod: AuthMethod;
|
|
143
|
+
|
|
144
|
+
/** API key context when request is authenticated via API key, null otherwise */
|
|
145
|
+
apiKey: AuthApiKeyContext | null;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Check if the API key has the required permissions.
|
|
149
|
+
* All specified actions must be present for the resource.
|
|
150
|
+
* Supports '*' wildcard which matches any action.
|
|
151
|
+
*
|
|
152
|
+
* @param resource - The resource to check (e.g., 'project', 'user')
|
|
153
|
+
* @param actions - Actions required (e.g., 'read', 'write')
|
|
154
|
+
* @returns true if all actions are permitted, false otherwise
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* // Check for specific permission
|
|
159
|
+
* if (c.var.auth.hasPermission('project', 'write')) { ... }
|
|
160
|
+
*
|
|
161
|
+
* // Check for multiple permissions (all required)
|
|
162
|
+
* if (c.var.auth.hasPermission('project', 'read', 'write')) { ... }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
hasPermission(resource: string, ...actions: string[]): boolean;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Full authentication interface available on `c.var.auth` and `ctx.auth`.
|
|
170
|
+
*
|
|
171
|
+
* This is the primary interface you'll use to access authentication data
|
|
172
|
+
* in your route handlers and agents. It provides:
|
|
173
|
+
*
|
|
174
|
+
* - User data via `getUser()`
|
|
175
|
+
* - Organization helpers via `getOrg()`, `getOrgRole()`, `hasOrgRole()`
|
|
176
|
+
* - API key helpers via `apiKey`, `hasPermission()`
|
|
177
|
+
* - Token access via `getToken()`
|
|
178
|
+
*
|
|
179
|
+
* @example Route handler
|
|
180
|
+
* ```typescript
|
|
181
|
+
* app.get('/api/profile', async (c) => {
|
|
182
|
+
* const user = await c.var.auth.getUser();
|
|
183
|
+
* const org = await c.var.auth.getOrg();
|
|
184
|
+
* return c.json({ user, org });
|
|
185
|
+
* });
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @example Agent handler
|
|
189
|
+
* ```typescript
|
|
190
|
+
* handler: async (ctx, input) => {
|
|
191
|
+
* if (!ctx.auth) return { error: 'Unauthorized' };
|
|
192
|
+
* const user = await ctx.auth.getUser();
|
|
193
|
+
* return { message: `Hello, ${user.email}!` };
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @typeParam TUser - User type (defaults to AuthUser)
|
|
198
|
+
*/
|
|
199
|
+
export type AuthInterface<TUser = AuthUser> = AgentuityAuth<TUser, AuthContext> &
|
|
200
|
+
AuthOrgHelpers &
|
|
201
|
+
AuthApiKeyHelpers;
|
package/src/index.ts
CHANGED
|
@@ -1,18 +1,86 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @agentuity/auth - Authentication
|
|
2
|
+
* @agentuity/auth - Authentication for Agentuity projects
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Provides first-class authentication powered by BetterAuth.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* This is the server-only entry point. For React components, import from '@agentuity/auth/react'.
|
|
7
|
+
*
|
|
8
|
+
* @example Server-side setup
|
|
7
9
|
* ```typescript
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
+
* import { createAuth, createSessionMiddleware, mountAuthRoutes } from '@agentuity/auth';
|
|
11
|
+
*
|
|
12
|
+
* const auth = createAuth({
|
|
13
|
+
* connectionString: process.env.DATABASE_URL,
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* api.on(['GET', 'POST'], '/api/auth/*', mountAuthRoutes(auth));
|
|
17
|
+
* api.use('/api/*', createSessionMiddleware(auth));
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example Client-side setup
|
|
21
|
+
* ```tsx
|
|
22
|
+
* import { createAuthClient, AuthProvider } from '@agentuity/auth/react';
|
|
23
|
+
*
|
|
24
|
+
* const authClient = createAuthClient();
|
|
10
25
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
26
|
+
* <AgentuityProvider>
|
|
27
|
+
* <AuthProvider authClient={authClient}>
|
|
28
|
+
* <App />
|
|
29
|
+
* </AuthProvider>
|
|
30
|
+
* </AgentuityProvider>
|
|
13
31
|
* ```
|
|
14
32
|
*
|
|
15
33
|
* @module @agentuity/auth
|
|
16
34
|
*/
|
|
17
35
|
|
|
18
|
-
|
|
36
|
+
// =============================================================================
|
|
37
|
+
// Config
|
|
38
|
+
// =============================================================================
|
|
39
|
+
|
|
40
|
+
export { createAuth, getDefaultPlugins, DEFAULT_API_KEY_OPTIONS } from './agentuity/config';
|
|
41
|
+
export type {
|
|
42
|
+
AuthOptions,
|
|
43
|
+
AuthInstance,
|
|
44
|
+
AuthBase,
|
|
45
|
+
ApiKeyPluginOptions,
|
|
46
|
+
OrganizationApiMethods,
|
|
47
|
+
ApiKeyApiMethods,
|
|
48
|
+
JwtApiMethods,
|
|
49
|
+
DefaultPluginApiMethods,
|
|
50
|
+
} from './agentuity/config';
|
|
51
|
+
|
|
52
|
+
// =============================================================================
|
|
53
|
+
// Server (Hono middleware and handlers)
|
|
54
|
+
// =============================================================================
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
createSessionMiddleware,
|
|
58
|
+
createApiKeyMiddleware,
|
|
59
|
+
mountAuthRoutes,
|
|
60
|
+
} from './agentuity/server';
|
|
61
|
+
export type {
|
|
62
|
+
AuthMiddlewareOptions,
|
|
63
|
+
ApiKeyMiddlewareOptions,
|
|
64
|
+
AuthEnv,
|
|
65
|
+
OtelSpansConfig,
|
|
66
|
+
MountAuthRoutesOptions,
|
|
67
|
+
} from './agentuity/server';
|
|
68
|
+
|
|
69
|
+
// =============================================================================
|
|
70
|
+
// Types
|
|
71
|
+
// =============================================================================
|
|
72
|
+
|
|
73
|
+
export type {
|
|
74
|
+
AuthContext,
|
|
75
|
+
AuthOrgContext,
|
|
76
|
+
AuthApiKeyContext,
|
|
77
|
+
AuthApiKeyPermissions,
|
|
78
|
+
AuthMethod,
|
|
79
|
+
AuthOrgHelpers,
|
|
80
|
+
AuthApiKeyHelpers,
|
|
81
|
+
AuthInterface,
|
|
82
|
+
AuthUser,
|
|
83
|
+
AuthSession,
|
|
84
|
+
} from './agentuity/types';
|
|
85
|
+
|
|
86
|
+
export type { AgentuityAuth } from './types';
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agentuity Auth Drizzle schema.
|
|
3
|
+
*
|
|
4
|
+
* Provides type-safe Drizzle table definitions for BetterAuth with Agentuity's
|
|
5
|
+
* default plugins (organization, JWT, bearer, API key).
|
|
6
|
+
*
|
|
7
|
+
* @module agentuity/schema
|
|
8
|
+
*
|
|
9
|
+
* @example Merge with your app schema
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import * as authSchema from '@agentuity/auth/schema';
|
|
12
|
+
* import { drizzle } from 'drizzle-orm/bun-sql';
|
|
13
|
+
*
|
|
14
|
+
* const schema = { ...authSchema, ...myAppSchema };
|
|
15
|
+
* const db = drizzle(connectionString, { schema });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { pgTable, text, boolean, timestamp, integer, index } from 'drizzle-orm/pg-core';
|
|
20
|
+
import { relations } from 'drizzle-orm';
|
|
21
|
+
|
|
22
|
+
// =============================================================================
|
|
23
|
+
// BetterAuth Core Tables
|
|
24
|
+
// =============================================================================
|
|
25
|
+
|
|
26
|
+
export const user = pgTable('user', {
|
|
27
|
+
id: text('id').primaryKey(),
|
|
28
|
+
name: text('name').notNull(),
|
|
29
|
+
email: text('email').notNull().unique(),
|
|
30
|
+
emailVerified: boolean('emailVerified').notNull().default(false),
|
|
31
|
+
image: text('image'),
|
|
32
|
+
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow().notNull(),
|
|
33
|
+
updatedAt: timestamp('updatedAt', { withTimezone: true }).defaultNow().notNull(),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export const session = pgTable(
|
|
37
|
+
'session',
|
|
38
|
+
{
|
|
39
|
+
id: text('id').primaryKey(),
|
|
40
|
+
expiresAt: timestamp('expiresAt', { withTimezone: true }).notNull(),
|
|
41
|
+
token: text('token').notNull().unique(),
|
|
42
|
+
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow().notNull(),
|
|
43
|
+
updatedAt: timestamp('updatedAt', { withTimezone: true }).notNull(),
|
|
44
|
+
ipAddress: text('ipAddress'),
|
|
45
|
+
userAgent: text('userAgent'),
|
|
46
|
+
userId: text('userId')
|
|
47
|
+
.notNull()
|
|
48
|
+
.references(() => user.id, { onDelete: 'cascade' }),
|
|
49
|
+
activeOrganizationId: text('activeOrganizationId'),
|
|
50
|
+
},
|
|
51
|
+
(table) => [index('session_userId_idx').on(table.userId)]
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
export const account = pgTable(
|
|
55
|
+
'account',
|
|
56
|
+
{
|
|
57
|
+
id: text('id').primaryKey(),
|
|
58
|
+
accountId: text('accountId').notNull(),
|
|
59
|
+
providerId: text('providerId').notNull(),
|
|
60
|
+
userId: text('userId')
|
|
61
|
+
.notNull()
|
|
62
|
+
.references(() => user.id, { onDelete: 'cascade' }),
|
|
63
|
+
accessToken: text('accessToken'),
|
|
64
|
+
refreshToken: text('refreshToken'),
|
|
65
|
+
idToken: text('idToken'),
|
|
66
|
+
accessTokenExpiresAt: timestamp('accessTokenExpiresAt', { withTimezone: true }),
|
|
67
|
+
refreshTokenExpiresAt: timestamp('refreshTokenExpiresAt', { withTimezone: true }),
|
|
68
|
+
scope: text('scope'),
|
|
69
|
+
password: text('password'),
|
|
70
|
+
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow().notNull(),
|
|
71
|
+
updatedAt: timestamp('updatedAt', { withTimezone: true }).notNull(),
|
|
72
|
+
},
|
|
73
|
+
(table) => [index('account_userId_idx').on(table.userId)]
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
export const verification = pgTable(
|
|
77
|
+
'verification',
|
|
78
|
+
{
|
|
79
|
+
id: text('id').primaryKey(),
|
|
80
|
+
identifier: text('identifier').notNull(),
|
|
81
|
+
value: text('value').notNull(),
|
|
82
|
+
expiresAt: timestamp('expiresAt', { withTimezone: true }).notNull(),
|
|
83
|
+
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow().notNull(),
|
|
84
|
+
updatedAt: timestamp('updatedAt', { withTimezone: true }).defaultNow().notNull(),
|
|
85
|
+
},
|
|
86
|
+
(table) => [index('verification_identifier_idx').on(table.identifier)]
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// =============================================================================
|
|
90
|
+
// Organization Plugin Tables
|
|
91
|
+
// =============================================================================
|
|
92
|
+
|
|
93
|
+
export const organization = pgTable('organization', {
|
|
94
|
+
id: text('id').primaryKey(),
|
|
95
|
+
name: text('name').notNull(),
|
|
96
|
+
slug: text('slug').notNull().unique(),
|
|
97
|
+
logo: text('logo'),
|
|
98
|
+
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow().notNull(),
|
|
99
|
+
metadata: text('metadata'),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export const member = pgTable(
|
|
103
|
+
'member',
|
|
104
|
+
{
|
|
105
|
+
id: text('id').primaryKey(),
|
|
106
|
+
organizationId: text('organizationId')
|
|
107
|
+
.notNull()
|
|
108
|
+
.references(() => organization.id, { onDelete: 'cascade' }),
|
|
109
|
+
userId: text('userId')
|
|
110
|
+
.notNull()
|
|
111
|
+
.references(() => user.id, { onDelete: 'cascade' }),
|
|
112
|
+
role: text('role').notNull(),
|
|
113
|
+
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow().notNull(),
|
|
114
|
+
},
|
|
115
|
+
(table) => [
|
|
116
|
+
index('member_organizationId_idx').on(table.organizationId),
|
|
117
|
+
index('member_userId_idx').on(table.userId),
|
|
118
|
+
]
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
export const invitation = pgTable(
|
|
122
|
+
'invitation',
|
|
123
|
+
{
|
|
124
|
+
id: text('id').primaryKey(),
|
|
125
|
+
organizationId: text('organizationId')
|
|
126
|
+
.notNull()
|
|
127
|
+
.references(() => organization.id, { onDelete: 'cascade' }),
|
|
128
|
+
email: text('email').notNull(),
|
|
129
|
+
role: text('role'),
|
|
130
|
+
status: text('status').notNull(),
|
|
131
|
+
expiresAt: timestamp('expiresAt', { withTimezone: true }).notNull(),
|
|
132
|
+
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow().notNull(),
|
|
133
|
+
inviterId: text('inviterId')
|
|
134
|
+
.notNull()
|
|
135
|
+
.references(() => user.id, { onDelete: 'cascade' }),
|
|
136
|
+
},
|
|
137
|
+
(table) => [
|
|
138
|
+
index('invitation_organizationId_idx').on(table.organizationId),
|
|
139
|
+
index('invitation_email_idx').on(table.email),
|
|
140
|
+
]
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
// =============================================================================
|
|
144
|
+
// JWT Plugin Table
|
|
145
|
+
// =============================================================================
|
|
146
|
+
|
|
147
|
+
export const jwks = pgTable('jwks', {
|
|
148
|
+
id: text('id').primaryKey(),
|
|
149
|
+
publicKey: text('publicKey').notNull(),
|
|
150
|
+
privateKey: text('privateKey').notNull(),
|
|
151
|
+
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow().notNull(),
|
|
152
|
+
expiresAt: timestamp('expiresAt', { withTimezone: true }),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// =============================================================================
|
|
156
|
+
// API Key Plugin Table
|
|
157
|
+
// =============================================================================
|
|
158
|
+
|
|
159
|
+
export const apikey = pgTable(
|
|
160
|
+
'apikey',
|
|
161
|
+
{
|
|
162
|
+
id: text('id').primaryKey(),
|
|
163
|
+
name: text('name'),
|
|
164
|
+
start: text('start'),
|
|
165
|
+
prefix: text('prefix'),
|
|
166
|
+
key: text('key').notNull(),
|
|
167
|
+
userId: text('userId')
|
|
168
|
+
.notNull()
|
|
169
|
+
.references(() => user.id, { onDelete: 'cascade' }),
|
|
170
|
+
refillInterval: integer('refillInterval'),
|
|
171
|
+
refillAmount: integer('refillAmount'),
|
|
172
|
+
lastRefillAt: timestamp('lastRefillAt', { withTimezone: true }),
|
|
173
|
+
enabled: boolean('enabled').notNull().default(true),
|
|
174
|
+
rateLimitEnabled: boolean('rateLimitEnabled').notNull().default(true),
|
|
175
|
+
rateLimitTimeWindow: integer('rateLimitTimeWindow').notNull().default(86400000),
|
|
176
|
+
rateLimitMax: integer('rateLimitMax').notNull().default(10),
|
|
177
|
+
requestCount: integer('requestCount').notNull().default(0),
|
|
178
|
+
remaining: integer('remaining'),
|
|
179
|
+
lastRequest: timestamp('lastRequest', { withTimezone: true }),
|
|
180
|
+
expiresAt: timestamp('expiresAt', { withTimezone: true }),
|
|
181
|
+
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow().notNull(),
|
|
182
|
+
updatedAt: timestamp('updatedAt', { withTimezone: true }).defaultNow().notNull(),
|
|
183
|
+
permissions: text('permissions'),
|
|
184
|
+
metadata: text('metadata'),
|
|
185
|
+
},
|
|
186
|
+
(table) => [index('apikey_userId_idx').on(table.userId), index('apikey_key_idx').on(table.key)]
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
// =============================================================================
|
|
190
|
+
// Relations (required for BetterAuth join optimization)
|
|
191
|
+
// =============================================================================
|
|
192
|
+
|
|
193
|
+
export const userRelations = relations(user, ({ many }) => ({
|
|
194
|
+
sessions: many(session),
|
|
195
|
+
accounts: many(account),
|
|
196
|
+
members: many(member),
|
|
197
|
+
apikeys: many(apikey),
|
|
198
|
+
invitations: many(invitation),
|
|
199
|
+
}));
|
|
200
|
+
|
|
201
|
+
export const sessionRelations = relations(session, ({ one }) => ({
|
|
202
|
+
user: one(user, {
|
|
203
|
+
fields: [session.userId],
|
|
204
|
+
references: [user.id],
|
|
205
|
+
}),
|
|
206
|
+
}));
|
|
207
|
+
|
|
208
|
+
export const accountRelations = relations(account, ({ one }) => ({
|
|
209
|
+
user: one(user, {
|
|
210
|
+
fields: [account.userId],
|
|
211
|
+
references: [user.id],
|
|
212
|
+
}),
|
|
213
|
+
}));
|
|
214
|
+
|
|
215
|
+
export const organizationRelations = relations(organization, ({ many }) => ({
|
|
216
|
+
members: many(member),
|
|
217
|
+
invitations: many(invitation),
|
|
218
|
+
}));
|
|
219
|
+
|
|
220
|
+
export const memberRelations = relations(member, ({ one }) => ({
|
|
221
|
+
organization: one(organization, {
|
|
222
|
+
fields: [member.organizationId],
|
|
223
|
+
references: [organization.id],
|
|
224
|
+
}),
|
|
225
|
+
user: one(user, {
|
|
226
|
+
fields: [member.userId],
|
|
227
|
+
references: [user.id],
|
|
228
|
+
}),
|
|
229
|
+
}));
|
|
230
|
+
|
|
231
|
+
export const invitationRelations = relations(invitation, ({ one }) => ({
|
|
232
|
+
organization: one(organization, {
|
|
233
|
+
fields: [invitation.organizationId],
|
|
234
|
+
references: [organization.id],
|
|
235
|
+
}),
|
|
236
|
+
inviter: one(user, {
|
|
237
|
+
fields: [invitation.inviterId],
|
|
238
|
+
references: [user.id],
|
|
239
|
+
}),
|
|
240
|
+
}));
|
|
241
|
+
|
|
242
|
+
export const apikeyRelations = relations(apikey, ({ one }) => ({
|
|
243
|
+
user: one(user, {
|
|
244
|
+
fields: [apikey.userId],
|
|
245
|
+
references: [user.id],
|
|
246
|
+
}),
|
|
247
|
+
}));
|
|
248
|
+
|
|
249
|
+
// =============================================================================
|
|
250
|
+
// Combined schema export (for easy spreading into app schema)
|
|
251
|
+
// =============================================================================
|
|
252
|
+
|
|
253
|
+
export const authSchema = {
|
|
254
|
+
user,
|
|
255
|
+
session,
|
|
256
|
+
account,
|
|
257
|
+
verification,
|
|
258
|
+
organization,
|
|
259
|
+
member,
|
|
260
|
+
invitation,
|
|
261
|
+
jwks,
|
|
262
|
+
apikey,
|
|
263
|
+
userRelations,
|
|
264
|
+
sessionRelations,
|
|
265
|
+
accountRelations,
|
|
266
|
+
organizationRelations,
|
|
267
|
+
memberRelations,
|
|
268
|
+
invitationRelations,
|
|
269
|
+
apikeyRelations,
|
|
270
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -1,38 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Core authentication types
|
|
2
|
+
* Core authentication types for Agentuity Auth.
|
|
3
3
|
*
|
|
4
4
|
* @module types
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* Generic authenticated user interface.
|
|
9
|
-
* All auth providers return this structure with provider-specific data in `raw`.
|
|
10
|
-
*/
|
|
11
|
-
export interface AgentuityAuthUser<T = unknown> {
|
|
12
|
-
/** Unique user identifier from the auth provider */
|
|
13
|
-
id: string;
|
|
14
|
-
|
|
15
|
-
/** User's full name */
|
|
16
|
-
name?: string;
|
|
17
|
-
|
|
18
|
-
/** Primary email address */
|
|
19
|
-
email?: string;
|
|
20
|
-
|
|
21
|
-
/** Raw provider-specific user object for advanced use cases */
|
|
22
|
-
raw: T;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
7
|
/**
|
|
26
8
|
* Generic authentication interface exposed on Hono context.
|
|
27
|
-
*
|
|
9
|
+
*
|
|
10
|
+
* This type is intentionally provider-agnostic. For AgentuityAuth-based
|
|
11
|
+
* projects, prefer {@link AuthInterface} from `./agentuity/types`,
|
|
12
|
+
* which binds:
|
|
13
|
+
* - `TUser` to {@link AuthUser}
|
|
14
|
+
* - `TRaw` to {@link AuthContext}
|
|
15
|
+
*
|
|
16
|
+
* @typeParam TUser - Domain user type (e.g. AuthUser in AgentuityAuth projects).
|
|
17
|
+
* @typeParam TRaw - Underlying auth context (e.g. AuthContext, JWT payload, or session object).
|
|
18
|
+
*
|
|
19
|
+
* @see {@link AuthInterface} for the full AgentuityAuth-specific interface with org and API key helpers.
|
|
28
20
|
*/
|
|
29
21
|
export interface AgentuityAuth<TUser = unknown, TRaw = unknown> {
|
|
30
22
|
/** Get the authenticated user, throws if not authenticated */
|
|
31
|
-
getUser(): Promise<
|
|
23
|
+
getUser(): Promise<TUser>;
|
|
32
24
|
|
|
33
25
|
/** Get the raw JWT token */
|
|
34
26
|
getToken(): Promise<string | null>;
|
|
35
27
|
|
|
36
|
-
/** Raw provider-specific auth object (e.g
|
|
28
|
+
/** Raw provider-specific auth object or auth context (e.g. AuthContext, JWT payload, session) */
|
|
37
29
|
raw: TRaw;
|
|
38
30
|
}
|