authfn 0.1.1 → 0.2.0

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 (118) hide show
  1. package/README.md +124 -249
  2. package/dist/core/account-linking.d.ts +30 -0
  3. package/dist/core/account-linking.d.ts.map +1 -0
  4. package/dist/core/account-linking.js +80 -0
  5. package/dist/core/account-linking.js.map +1 -0
  6. package/dist/core/account.d.ts +38 -0
  7. package/dist/core/account.d.ts.map +1 -0
  8. package/dist/core/account.js +164 -0
  9. package/dist/core/account.js.map +1 -0
  10. package/dist/core/admin-users.d.ts +50 -0
  11. package/dist/core/admin-users.d.ts.map +1 -0
  12. package/dist/core/admin-users.js +227 -0
  13. package/dist/core/admin-users.js.map +1 -0
  14. package/dist/core/api-keys.d.ts +40 -0
  15. package/dist/core/api-keys.d.ts.map +1 -0
  16. package/dist/core/api-keys.js +165 -0
  17. package/dist/core/api-keys.js.map +1 -0
  18. package/dist/core/cache.d.ts +18 -0
  19. package/dist/core/cache.d.ts.map +1 -0
  20. package/dist/core/cache.js +54 -0
  21. package/dist/core/cache.js.map +1 -0
  22. package/dist/core/cookies.d.ts +25 -0
  23. package/dist/core/cookies.d.ts.map +1 -0
  24. package/dist/core/cookies.js +89 -0
  25. package/dist/core/cookies.js.map +1 -0
  26. package/dist/core/delivery.d.ts +4 -0
  27. package/dist/core/delivery.d.ts.map +1 -0
  28. package/dist/core/delivery.js +37 -0
  29. package/dist/core/delivery.js.map +1 -0
  30. package/dist/core/environment.d.ts +4 -0
  31. package/dist/core/environment.d.ts.map +1 -0
  32. package/dist/core/environment.js +50 -0
  33. package/dist/core/environment.js.map +1 -0
  34. package/dist/core/errors.d.ts +120 -0
  35. package/dist/core/errors.d.ts.map +1 -0
  36. package/dist/core/errors.js +428 -0
  37. package/dist/core/errors.js.map +1 -0
  38. package/dist/core/gateway-routing.d.ts +88 -0
  39. package/dist/core/gateway-routing.d.ts.map +1 -0
  40. package/dist/core/gateway-routing.js +902 -0
  41. package/dist/core/gateway-routing.js.map +1 -0
  42. package/dist/core/oauth-accounts.d.ts +29 -0
  43. package/dist/core/oauth-accounts.d.ts.map +1 -0
  44. package/dist/core/oauth-accounts.js +104 -0
  45. package/dist/core/oauth-accounts.js.map +1 -0
  46. package/dist/core/observability.d.ts +4 -0
  47. package/dist/core/observability.d.ts.map +1 -0
  48. package/dist/core/observability.js +38 -0
  49. package/dist/core/observability.js.map +1 -0
  50. package/dist/core/passwords.d.ts +52 -0
  51. package/dist/core/passwords.d.ts.map +1 -0
  52. package/dist/core/passwords.js +449 -0
  53. package/dist/core/passwords.js.map +1 -0
  54. package/dist/core/plugin-runtime.d.ts +8 -0
  55. package/dist/core/plugin-runtime.d.ts.map +1 -0
  56. package/dist/core/plugin-runtime.js +15 -0
  57. package/dist/core/plugin-runtime.js.map +1 -0
  58. package/dist/core/rate-limit.d.ts +4 -0
  59. package/dist/core/rate-limit.d.ts.map +1 -0
  60. package/dist/core/rate-limit.js +177 -0
  61. package/dist/core/rate-limit.js.map +1 -0
  62. package/dist/core/regions.d.ts +47 -0
  63. package/dist/core/regions.d.ts.map +1 -0
  64. package/dist/core/regions.js +620 -0
  65. package/dist/core/regions.js.map +1 -0
  66. package/dist/core/session-responses.d.ts +10 -0
  67. package/dist/core/session-responses.d.ts.map +1 -0
  68. package/dist/core/session-responses.js +22 -0
  69. package/dist/core/session-responses.js.map +1 -0
  70. package/dist/core/sessions.d.ts +50 -0
  71. package/dist/core/sessions.d.ts.map +1 -0
  72. package/dist/core/sessions.js +421 -0
  73. package/dist/core/sessions.js.map +1 -0
  74. package/dist/core/two-factor.d.ts +34 -0
  75. package/dist/core/two-factor.d.ts.map +1 -0
  76. package/dist/core/two-factor.js +494 -0
  77. package/dist/core/two-factor.js.map +1 -0
  78. package/dist/core/users.d.ts +12 -0
  79. package/dist/core/users.d.ts.map +1 -0
  80. package/dist/core/users.js +71 -0
  81. package/dist/core/users.js.map +1 -0
  82. package/dist/core/verifications.d.ts +52 -0
  83. package/dist/core/verifications.d.ts.map +1 -0
  84. package/dist/core/verifications.js +521 -0
  85. package/dist/core/verifications.js.map +1 -0
  86. package/dist/http/envelopes.d.ts +10 -0
  87. package/dist/http/envelopes.d.ts.map +1 -0
  88. package/dist/http/envelopes.js +99 -0
  89. package/dist/http/envelopes.js.map +1 -0
  90. package/dist/http/router.d.ts +16 -0
  91. package/dist/http/router.d.ts.map +1 -0
  92. package/dist/http/router.js +347 -0
  93. package/dist/http/router.js.map +1 -0
  94. package/dist/index.d.ts +10 -6
  95. package/dist/index.d.ts.map +1 -1
  96. package/dist/index.js +92 -227
  97. package/dist/index.js.map +1 -1
  98. package/dist/openapi.d.ts +4 -0
  99. package/dist/openapi.d.ts.map +1 -0
  100. package/dist/openapi.js +43 -0
  101. package/dist/openapi.js.map +1 -0
  102. package/dist/plugin-runner.d.ts +12 -0
  103. package/dist/plugin-runner.d.ts.map +1 -0
  104. package/dist/plugin-runner.js +206 -0
  105. package/dist/plugin-runner.js.map +1 -0
  106. package/dist/plugin-types.d.ts +205 -0
  107. package/dist/plugin-types.d.ts.map +1 -0
  108. package/dist/plugin-types.js +2 -0
  109. package/dist/plugin-types.js.map +1 -0
  110. package/dist/schema.d.ts +5 -10
  111. package/dist/schema.d.ts.map +1 -1
  112. package/dist/schema.js +113 -70
  113. package/dist/schema.js.map +1 -1
  114. package/dist/types.d.ts +433 -69
  115. package/dist/types.d.ts.map +1 -1
  116. package/dist/types.js +1 -4
  117. package/dist/types.js.map +1 -1
  118. package/package.json +44 -18
package/README.md CHANGED
@@ -1,292 +1,167 @@
1
1
  # authfn
2
2
 
3
- API key authentication library using Superfunctions abstractions.
3
+ `authfn` is the authfn session and identity kernel for the Superfunctions ecosystem. It composes declared plugins, manages browser sessions, generates schema/OpenAPI output, and provides `@superfunctions/auth` provider integration without bundling every auth method into the root package.
4
4
 
5
- ## Overview
5
+ ## What It Ships
6
6
 
7
- `authfn` is a reference implementation of the `@superfunctions/auth` abstraction that provides API key-based authentication. It includes:
7
+ - Cookie-backed browser sessions with CSRF protection
8
+ - Plugin composition primitives
9
+ - Shared OpenAPI generation through `@superfunctions/http-openapi`
10
+ - Structured lifecycle events through `config.observability.emit(...)`
8
11
 
9
- - **Auth Provider**: Conforms to `@superfunctions/auth` interface
10
- - **Database Storage**: Uses `@superfunctions/db` adapters for storing API keys
11
- - **Management API**: Optional HTTP API for managing keys (uses `@superfunctions/http`)
12
- - **Key Generation**: Secure API key generation and validation
12
+ ## Package Inventory
13
13
 
14
- ## Installation
15
-
16
- ```bash
17
- npm install authfn @superfunctions/db @superfunctions/http
18
- ```
14
+ - `authfn`
15
+ - core runtime, router composition, schema generation, provider integration
16
+ - `@authfn/password`, `@authfn/email-otp`, `@authfn/social-oauth`, `@authfn/api-keys`, `@authfn/two-factor`, `@authfn/multi-region`, `@authfn/native-handoff`
17
+ - optional server plugins
18
+ - `@authfn/client`
19
+ - typed browser client using cookie credentials by default
20
+ - `@authfn/svelte`
21
+ - thin Svelte stores over the browser client
19
22
 
20
23
  ## Quick Start
21
24
 
22
- ### 1. Generate Schema
23
-
24
- First, generate the database schema for your ORM:
25
-
26
- ```bash
27
- npx @superfunctions/cli generate-schema \
28
- --config ./node_modules/authfn \
29
- --adapter drizzle \
30
- --output ./src/db
31
- ```
32
-
33
- ### 2. Setup Database
34
-
35
- ```typescript
36
- import { drizzle } from 'drizzle-orm/postgres-js';
37
- import postgres from 'postgres';
38
- import * as authSchema from './db/authfn-schema';
39
-
40
- const client = postgres(process.env.DATABASE_URL);
41
- const db = drizzle(client, { schema: authSchema });
42
- ```
43
-
44
- ### 3. Create authfn Instance
45
-
46
- ```typescript
47
- import { createAuthFn } from 'authfn';
48
- import { drizzleAdapter } from '@superfunctions/db/adapters';
49
-
50
- const adapter = drizzleAdapter({ db, dialect: 'postgres' });
51
-
52
- const auth = createAuthFn({
53
- database: adapter,
25
+ ```ts
26
+ import { memoryAdapter } from '@superfunctions/db/testing';
27
+ import {
28
+ authFnPlugins,
29
+ authfn
30
+ } from 'authfn';
31
+ import { authFnPasswordPlugin } from '@authfn/password';
32
+ import { authFnEmailOtpPlugin } from '@authfn/email-otp';
33
+ import { authFnSocialOAuthPlugin } from '@authfn/social-oauth';
34
+ import { authFnTwoFactorPlugin } from '@authfn/two-factor';
35
+ import { authFnMultiRegionPlugin } from '@authfn/multi-region';
36
+
37
+ const authApp = authfn({
54
38
  namespace: 'authfn',
55
- enableApi: true,
56
- apiConfig: {
57
- adminKey: process.env.ADMIN_KEY, // Optional: protect management API
39
+ openApi: {
40
+ title: 'AuthFn API',
41
+ version: '1.0.0'
58
42
  },
43
+ plugins: authFnPlugins(
44
+ authFnPasswordPlugin(),
45
+ authFnEmailOtpPlugin(),
46
+ authFnSocialOAuthPlugin(),
47
+ authFnTwoFactorPlugin(),
48
+ authFnMultiRegionPlugin()
49
+ )
59
50
  });
60
- ```
61
-
62
- ### 4. Use with Libraries
63
-
64
- Use the auth provider with any library accepting `@superfunctions/auth`:
65
51
 
66
- ```typescript
67
- import { createConductBackend } from 'conduct-backend';
68
-
69
- const conduct = createConductBackend({
70
- database: adapter,
71
- auth: auth.provider,
52
+ const auth = authApp.createServer({
53
+ database: memoryAdapter({ debug: false }),
54
+ observability: {
55
+ emit(event) {
56
+ console.log(event.type, event.requestId);
57
+ }
58
+ },
59
+ pluginRuntime: {
60
+ emailOtp: {
61
+ delivery: {
62
+ async send(input) {
63
+ return { sent: true, metadata: { channel: input.channel } };
64
+ }
65
+ }
66
+ },
67
+ socialOAuth: {
68
+ providers: {
69
+ google: {
70
+ clientId: process.env.GOOGLE_CLIENT_ID!,
71
+ clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
72
+ allowlistedReturnTo: ['https://app.example.com/post-auth']
73
+ }
74
+ }
75
+ }
76
+ }
72
77
  });
73
78
  ```
74
79
 
75
- ### 5. Mount API
80
+ Consumers mount `auth.router` with a `@superfunctions/http` adapter and use `auth.provider` anywhere an `@superfunctions/auth` provider is expected.
76
81
 
77
- Mount both the auth management API and your library's API:
82
+ ## Route Surface
78
83
 
79
- ```typescript
80
- import express from 'express';
81
- import { toExpress } from '@superfunctions/http-express';
84
+ Base routes:
82
85
 
83
- const app = express();
86
+ - `GET /auth/session`
87
+ - `GET /auth/sessions`
88
+ - `POST /auth/sign-out`
89
+ - `POST /auth/sessions/:sessionId/revoke`
84
90
 
85
- // Mount auth management API
86
- app.use('/api/auth', toExpress(auth.router));
91
+ Password routes:
87
92
 
88
- // Mount your library's API
89
- app.use('/api/conduct', toExpress(conduct.router));
93
+ - `POST /auth/sign-up/password`
94
+ - `POST /auth/sign-in/password`
95
+ - `POST /auth/password/reset/start`
96
+ - `POST /auth/password/reset/complete`
90
97
 
91
- app.listen(3000);
92
- ```
98
+ OTP routes:
93
99
 
94
- ## API Reference
100
+ - `POST /auth/otp/send`
101
+ - `POST /auth/otp/verify`
95
102
 
96
- ### createAuthFn(config)
103
+ Social routes:
97
104
 
98
- Creates an authfn instance.
105
+ - `POST /auth/social/start`
106
+ - `GET /auth/social/callback/:provider`
107
+ - `POST /auth/social/disconnect/:provider`
99
108
 
100
- **Config Options:**
109
+ API key routes:
101
110
 
102
- ```typescript
103
- {
104
- database: Adapter; // Database adapter
105
- namespace?: string; // Table prefix (default: 'authfn')
106
- enableApi?: boolean; // Enable management API (default: false)
107
- apiConfig?: {
108
- basePath?: string; // API base path (default: '/auth')
109
- adminKey?: string; // Admin key for managing keys
110
- };
111
- }
112
- ```
111
+ - `POST /auth/api-keys`
112
+ - `GET /auth/api-keys`
113
+ - `DELETE /auth/api-keys/:keyId`
113
114
 
114
- **Returns:**
115
-
116
- ```typescript
117
- {
118
- provider: AuthProvider; // Auth provider for use with libraries
119
- router?: Router; // Management API router (if enabled)
120
- createKey: Function; // Create API key programmatically
121
- revokeKey: Function; // Revoke API key
122
- getKey: Function; // Get API key by ID
123
- listKeys: Function; // List API keys
124
- }
125
- ```
115
+ 2FA routes:
126
116
 
127
- ### Management API Endpoints
117
+ - `POST /auth/2fa/enroll`
118
+ - `POST /auth/2fa/confirm`
119
+ - `POST /auth/2fa/challenge`
120
+ - `POST /auth/2fa/disable`
128
121
 
129
- When `enableApi` is true, the following endpoints are available:
122
+ Multi-region routes:
130
123
 
131
- #### POST /keys
124
+ - `POST /auth/regions/lookup`
125
+ - `GET /auth/environment`
132
126
 
133
- Create a new API key.
127
+ ## OpenAPI
134
128
 
135
- **Request:**
136
- ```json
137
- {
138
- "name": "My API Key",
139
- "resourceIds": ["project_abc", "project_def"],
140
- "scopes": ["read", "write"],
141
- "metadata": { "team": "engineering" },
142
- "expiresAt": "2025-12-31T23:59:59Z"
143
- }
144
- ```
129
+ OpenAPI output is generated through the shared packages layer, not by authfn-specific code:
145
130
 
146
- **Response:**
147
- ```json
148
- {
149
- "id": "key_abc123",
150
- "key": "ak_64characterhexstring..."
151
- }
131
+ ```ts
132
+ const document = auth.openApi?.();
152
133
  ```
153
134
 
154
- **Note:** The actual key value is only returned once during creation. Store it securely.
155
-
156
- #### GET /keys
157
-
158
- List all API keys (without key values).
159
-
160
- **Query Parameters:**
161
- - `resourceId` - Filter by resource ID
162
-
163
- **Response:**
164
- ```json
165
- {
166
- "keys": [
167
- {
168
- "id": "key_abc123",
169
- "name": "My API Key",
170
- "resourceIds": ["project_abc"],
171
- "scopes": ["read", "write"],
172
- "metadata": { "team": "engineering" },
173
- "expiresAt": "2025-12-31T23:59:59Z",
174
- "lastUsedAt": "2025-01-15T10:30:00Z",
175
- "createdAt": "2025-01-01T00:00:00Z"
176
- }
177
- ]
178
- }
179
- ```
135
+ The generated document is deterministic and includes auth-prefixed paths like:
180
136
 
181
- #### GET /keys/:id
137
+ - `/auth/session`
138
+ - `/auth/sign-up/password`
139
+ - `/auth/sign-in/password`
140
+ - `/auth/otp/send`
141
+ - `/auth/social/start`
182
142
 
183
- Get a specific API key by ID (without key value).
143
+ ## Observability
184
144
 
185
- #### DELETE /keys/:id
145
+ Provide `config.observability.emit(event)` to receive structured lifecycle events. The current event surface includes:
186
146
 
187
- Revoke an API key.
147
+ - `authfn.user.created`
148
+ - `authfn.session.issued`
149
+ - `authfn.session.revoked`
150
+ - `authfn.otp.sent`
151
+ - `authfn.otp.verified`
152
+ - `authfn.oauth.started`
153
+ - `authfn.oauth.completed`
154
+ - `authfn.api_key.created`
155
+ - `authfn.api_key.revoked`
156
+ - `authfn.2fa.enabled`
157
+ - `authfn.2fa.challenged`
158
+ - `authfn.region.lookup`
159
+ - `authfn.plugin.failed`
188
160
 
189
- **Response:**
190
- ```json
191
- {
192
- "success": true
193
- }
194
- ```
195
-
196
- ### Programmatic Usage
197
-
198
- You can also create and manage keys programmatically:
199
-
200
- ```typescript
201
- // Create a key
202
- const { id, key } = await auth.createKey({
203
- name: 'My API Key',
204
- resourceIds: ['project_abc'],
205
- scopes: ['read', 'write'],
206
- expiresAt: new Date('2025-12-31'),
207
- });
208
-
209
- // List keys
210
- const keys = await auth.listKeys();
211
-
212
- // Get a key
213
- const apiKey = await auth.getKey('key_abc123');
214
-
215
- // Revoke a key
216
- await auth.revokeKey('key_abc123');
217
- ```
218
-
219
- ## Database Schema
220
-
221
- authfn creates a single table:
222
-
223
- ### `{namespace}_api_keys`
224
-
225
- - `id` - Unique identifier
226
- - `key` - API key value (unique, indexed)
227
- - `name` - Descriptive name
228
- - `resource_ids` - JSON array of resource IDs
229
- - `scopes` - JSON array of scopes/permissions
230
- - `metadata` - JSON object for additional data
231
- - `expires_at` - Expiration timestamp
232
- - `last_used_at` - Last usage timestamp
233
- - `revoked_at` - Revocation timestamp
234
- - `created_at` - Creation timestamp
235
-
236
- ## Security
237
-
238
- - API keys are 64-character hex strings with a prefix
239
- - Keys are validated against database on each request
240
- - Expired and revoked keys are rejected
241
- - Management API requires admin key authentication
242
- - Key values are never returned in list/get operations
243
-
244
- ## Integration Examples
245
-
246
- ### With Conduct Backend
247
-
248
- ```typescript
249
- const auth = createAuthFn({
250
- database: adapter,
251
- enableApi: true,
252
- });
253
-
254
- const conduct = createConductBackend({
255
- database: adapter,
256
- auth: {
257
- ...auth.provider,
258
- // Map resourceIds to projectIds
259
- authenticate: async (request) => {
260
- const session = await auth.provider.authenticate(request);
261
- if (!session) return null;
262
- return {
263
- ...session,
264
- projectIds: session.resourceIds,
265
- };
266
- },
267
- },
268
- });
269
- ```
270
-
271
- ### Custom Implementation
272
-
273
- Implement your own auth provider using the same abstraction:
274
-
275
- ```typescript
276
- import type { AuthProvider, AuthSession } from '@superfunctions/auth';
277
-
278
- const customAuth: AuthProvider = {
279
- async authenticate(request: Request): Promise<AuthSession | null> {
280
- // Your custom auth logic
281
- return {
282
- id: 'user_123',
283
- type: 'custom',
284
- resourceIds: ['resource_1'],
285
- };
286
- },
287
- };
288
- ```
161
+ Sensitive values such as passwords, OTP codes, API key secrets, and OAuth tokens are redacted or omitted from emitted events.
289
162
 
290
- ## License
163
+ ## Notes
291
164
 
292
- MIT
165
+ - `authfn` does not bundle framework adapters. Use the shared `@superfunctions/http-*` adapters directly.
166
+ - `authfn` does not bundle email or SMS delivery. Plug a provider in through the OTP delivery interface today, or layer `sendfn` in at the application boundary.
167
+ - Migration and cutover planning from Better Auth is intentionally out of scope for this spec bundle.
@@ -0,0 +1,30 @@
1
+ import type { AuthFnRuntimeConfig, AuthFnSocialProviderId, AuthFnUserRecord } from '../types.js';
2
+ export declare function allowsOAuthLinkByVerifiedEmail(config: Pick<AuthFnRuntimeConfig, 'accountLinking'>, providerId: AuthFnSocialProviderId): {
3
+ allowed: boolean;
4
+ requireExistingEmailVerified: boolean;
5
+ requireProviderEmailVerified: boolean;
6
+ };
7
+ export declare function allowsOtpSignUpExistingUser(config: Pick<AuthFnRuntimeConfig, 'accountLinking'>): boolean;
8
+ export declare function allowsPasswordForAuthenticatedUser(config: Pick<AuthFnRuntimeConfig, 'accountLinking'>): {
9
+ allowed: boolean;
10
+ requireExistingEmailVerified: boolean;
11
+ };
12
+ export declare function emitAccountLinkedEvent(config: Pick<AuthFnRuntimeConfig, 'observability'>, input: {
13
+ request?: Request;
14
+ user: AuthFnUserRecord;
15
+ method: string;
16
+ provider?: AuthFnSocialProviderId;
17
+ regionId?: string;
18
+ metadata?: Record<string, unknown>;
19
+ }): Promise<void>;
20
+ export declare function emitAccountLinkingConflictEvent(config: Pick<AuthFnRuntimeConfig, 'observability'>, input: {
21
+ request?: Request;
22
+ user?: AuthFnUserRecord;
23
+ primaryEmail?: string;
24
+ method: string;
25
+ provider?: AuthFnSocialProviderId;
26
+ regionId?: string;
27
+ reason: string;
28
+ metadata?: Record<string, unknown>;
29
+ }): Promise<void>;
30
+ //# sourceMappingURL=account-linking.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"account-linking.d.ts","sourceRoot":"","sources":["../../src/core/account-linking.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,sBAAsB,EACtB,gBAAgB,EACjB,MAAM,aAAa,CAAC;AAGrB,wBAAgB,8BAA8B,CAC5C,MAAM,EAAE,IAAI,CAAC,mBAAmB,EAAE,gBAAgB,CAAC,EACnD,UAAU,EAAE,sBAAsB,GACjC;IACD,OAAO,EAAE,OAAO,CAAC;IACjB,4BAA4B,EAAE,OAAO,CAAC;IACtC,4BAA4B,EAAE,OAAO,CAAC;CACvC,CAwBA;AAED,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,IAAI,CAAC,mBAAmB,EAAE,gBAAgB,CAAC,GAClD,OAAO,CAET;AAED,wBAAgB,kCAAkC,CAChD,MAAM,EAAE,IAAI,CAAC,mBAAmB,EAAE,gBAAgB,CAAC,GAClD;IACD,OAAO,EAAE,OAAO,CAAC;IACjB,4BAA4B,EAAE,OAAO,CAAC;CACvC,CAoBA;AAED,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,IAAI,CAAC,mBAAmB,EAAE,eAAe,CAAC,EAClD,KAAK,EAAE;IACL,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,gBAAgB,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,sBAAsB,CAAC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC,GACA,OAAO,CAAC,IAAI,CAAC,CAef;AAED,wBAAsB,+BAA+B,CACnD,MAAM,EAAE,IAAI,CAAC,mBAAmB,EAAE,eAAe,CAAC,EAClD,KAAK,EAAE;IACL,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,IAAI,CAAC,EAAE,gBAAgB,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,sBAAsB,CAAC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC,GACA,OAAO,CAAC,IAAI,CAAC,CAgBf"}
@@ -0,0 +1,80 @@
1
+ import { emitAuthEvent, eventRequestId } from './observability.js';
2
+ export function allowsOAuthLinkByVerifiedEmail(config, providerId) {
3
+ const policy = config.accountLinking?.oauthByVerifiedEmail;
4
+ if (!policy) {
5
+ return {
6
+ allowed: false,
7
+ requireExistingEmailVerified: true,
8
+ requireProviderEmailVerified: true
9
+ };
10
+ }
11
+ if (policy === true) {
12
+ return {
13
+ allowed: true,
14
+ requireExistingEmailVerified: true,
15
+ requireProviderEmailVerified: true
16
+ };
17
+ }
18
+ const providers = policy.providers;
19
+ return {
20
+ allowed: !providers?.length || providers.includes(providerId),
21
+ requireExistingEmailVerified: policy.requireExistingEmailVerified ?? true,
22
+ requireProviderEmailVerified: policy.requireProviderEmailVerified ?? true
23
+ };
24
+ }
25
+ export function allowsOtpSignUpExistingUser(config) {
26
+ return config.accountLinking?.otpSignUpExistingUser ?? false;
27
+ }
28
+ export function allowsPasswordForAuthenticatedUser(config) {
29
+ const policy = config.accountLinking?.passwordForAuthenticatedUser;
30
+ if (!policy) {
31
+ return {
32
+ allowed: false,
33
+ requireExistingEmailVerified: true
34
+ };
35
+ }
36
+ if (policy === true) {
37
+ return {
38
+ allowed: true,
39
+ requireExistingEmailVerified: true
40
+ };
41
+ }
42
+ return {
43
+ allowed: true,
44
+ requireExistingEmailVerified: policy.requireExistingEmailVerified ?? true
45
+ };
46
+ }
47
+ export async function emitAccountLinkedEvent(config, input) {
48
+ await emitAuthEvent(config, {
49
+ type: 'authfn.account_linked',
50
+ requestId: eventRequestId(input.request),
51
+ actorId: input.user.id,
52
+ userId: input.user.id,
53
+ regionId: input.regionId,
54
+ provider: input.provider,
55
+ outcome: 'linked',
56
+ metadata: {
57
+ ...(input.metadata ?? {}),
58
+ method: input.method,
59
+ primaryEmail: input.user.primaryEmail
60
+ }
61
+ });
62
+ }
63
+ export async function emitAccountLinkingConflictEvent(config, input) {
64
+ await emitAuthEvent(config, {
65
+ type: 'authfn.account_linking.conflict',
66
+ requestId: eventRequestId(input.request),
67
+ actorId: input.user?.id,
68
+ userId: input.user?.id,
69
+ regionId: input.regionId,
70
+ provider: input.provider,
71
+ outcome: 'conflict',
72
+ metadata: {
73
+ method: input.method,
74
+ primaryEmail: input.primaryEmail ?? input.user?.primaryEmail,
75
+ reason: input.reason,
76
+ ...(input.metadata ?? {})
77
+ }
78
+ });
79
+ }
80
+ //# sourceMappingURL=account-linking.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"account-linking.js","sourceRoot":"","sources":["../../src/core/account-linking.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEnE,MAAM,UAAU,8BAA8B,CAC5C,MAAmD,EACnD,UAAkC;IAMlC,MAAM,MAAM,GAAG,MAAM,CAAC,cAAc,EAAE,oBAAoB,CAAC;IAC3D,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO;YACL,OAAO,EAAE,KAAK;YACd,4BAA4B,EAAE,I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CAAC;QACxC,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE;QACtB,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE;QACrB,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,OAAO,EAAE,QAAQ;QACjB,QAAQ,EAAE;YACR,GAAG,CAAC,KAAK,CAAC,QAAQ,IAAI,EAAE,CAAC;YACzB,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,YAAY,EAAE,KAAK,CAAC,IAAI,CAAC,YAAY;SACtC;KACF,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,+BAA+B,CACnD,MAAkD,EAClD,KASC;IAED,MAAM,aAAa,CAAC,MAAM,EAAE;QAC1B,IAAI,EAAE,iCAAiC;QACvC,SAAS,EAAE,cAAc,CAAC,KAAK,CAAC,OAAO,CAAC;QACxC,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE,EAAE;QACvB,MAAM,EAAE,KAAK,CAAC,IAAI,EAAE,EAAE;QACtB,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,OAAO,EAAE,UAAU;QACnB,QAAQ,EAAE;YACR,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,YAAY,EAAE,KAAK,CAAC,YAAY,IAAI,KAAK,CAAC,IAAI,EAAE,YAAY;YAC5D,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,GAAG,CAAC,KAAK,CAAC,QAAQ,IAAI,EAAE,CAAC;SAC1B;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { AuthFnAccountDeletionResult, AuthFnRuntimeConfig, AuthFnHooks, AuthFnSession, AuthFnSocialProviderId, AuthFnUserRecord } from '../types.js';
2
+ export interface AuthFnAccountOAuthAccount {
3
+ id: string;
4
+ provider: AuthFnSocialProviderId;
5
+ email?: string;
6
+ profile?: Record<string, unknown>;
7
+ createdAt: Date | string;
8
+ updatedAt: Date | string;
9
+ }
10
+ export interface AuthFnAccountDetails {
11
+ user: {
12
+ id: string;
13
+ primaryEmail?: string;
14
+ emailVerifiedAt?: Date | string | null;
15
+ metadata?: Record<string, unknown>;
16
+ createdAt: Date | string;
17
+ updatedAt: Date | string;
18
+ };
19
+ hasPassword: boolean;
20
+ twoFactorEnabled: boolean;
21
+ oauthAccounts: AuthFnAccountOAuthAccount[];
22
+ methods: {
23
+ password: boolean;
24
+ emailOtp: boolean;
25
+ oauth: AuthFnSocialProviderId[];
26
+ twoFactor: boolean;
27
+ };
28
+ }
29
+ export declare function getAccountDetailsForUser(config: Pick<AuthFnRuntimeConfig, 'database' | 'namespace' | 'plugins'>, user: AuthFnUserRecord): Promise<AuthFnAccountDetails>;
30
+ export declare function deleteAccountForUser(config: AuthFnRuntimeConfig, hooks: Partial<AuthFnHooks>, input: {
31
+ user: AuthFnUserRecord;
32
+ session?: AuthFnSession;
33
+ request?: Request;
34
+ actorId?: string;
35
+ /** True for administrator/delegated deletion, independent of actor identity. */
36
+ delegated?: boolean;
37
+ }): Promise<AuthFnAccountDeletionResult>;
38
+ //# sourceMappingURL=account.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"account.d.ts","sourceRoot":"","sources":["../../src/core/account.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,2BAA2B,EAC3B,mBAAmB,EACnB,WAAW,EACX,aAAa,EAEb,sBAAsB,EACtB,gBAAgB,EACjB,MAAM,aAAa,CAAC;AAUrB,MAAM,WAAW,yBAAyB;IACxC,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,sBAAsB,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,SAAS,EAAE,IAAI,GAAG,MAAM,CAAC;IACzB,SAAS,EAAE,IAAI,GAAG,MA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