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.
- package/README.md +124 -249
- package/dist/core/account-linking.d.ts +30 -0
- package/dist/core/account-linking.d.ts.map +1 -0
- package/dist/core/account-linking.js +80 -0
- package/dist/core/account-linking.js.map +1 -0
- package/dist/core/account.d.ts +38 -0
- package/dist/core/account.d.ts.map +1 -0
- package/dist/core/account.js +164 -0
- package/dist/core/account.js.map +1 -0
- package/dist/core/admin-users.d.ts +50 -0
- package/dist/core/admin-users.d.ts.map +1 -0
- package/dist/core/admin-users.js +227 -0
- package/dist/core/admin-users.js.map +1 -0
- package/dist/core/api-keys.d.ts +40 -0
- package/dist/core/api-keys.d.ts.map +1 -0
- package/dist/core/api-keys.js +165 -0
- package/dist/core/api-keys.js.map +1 -0
- package/dist/core/cache.d.ts +18 -0
- package/dist/core/cache.d.ts.map +1 -0
- package/dist/core/cache.js +54 -0
- package/dist/core/cache.js.map +1 -0
- package/dist/core/cookies.d.ts +25 -0
- package/dist/core/cookies.d.ts.map +1 -0
- package/dist/core/cookies.js +89 -0
- package/dist/core/cookies.js.map +1 -0
- package/dist/core/delivery.d.ts +4 -0
- package/dist/core/delivery.d.ts.map +1 -0
- package/dist/core/delivery.js +37 -0
- package/dist/core/delivery.js.map +1 -0
- package/dist/core/environment.d.ts +4 -0
- package/dist/core/environment.d.ts.map +1 -0
- package/dist/core/environment.js +50 -0
- package/dist/core/environment.js.map +1 -0
- package/dist/core/errors.d.ts +120 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +428 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/gateway-routing.d.ts +88 -0
- package/dist/core/gateway-routing.d.ts.map +1 -0
- package/dist/core/gateway-routing.js +902 -0
- package/dist/core/gateway-routing.js.map +1 -0
- package/dist/core/oauth-accounts.d.ts +29 -0
- package/dist/core/oauth-accounts.d.ts.map +1 -0
- package/dist/core/oauth-accounts.js +104 -0
- package/dist/core/oauth-accounts.js.map +1 -0
- package/dist/core/observability.d.ts +4 -0
- package/dist/core/observability.d.ts.map +1 -0
- package/dist/core/observability.js +38 -0
- package/dist/core/observability.js.map +1 -0
- package/dist/core/passwords.d.ts +52 -0
- package/dist/core/passwords.d.ts.map +1 -0
- package/dist/core/passwords.js +449 -0
- package/dist/core/passwords.js.map +1 -0
- package/dist/core/plugin-runtime.d.ts +8 -0
- package/dist/core/plugin-runtime.d.ts.map +1 -0
- package/dist/core/plugin-runtime.js +15 -0
- package/dist/core/plugin-runtime.js.map +1 -0
- package/dist/core/rate-limit.d.ts +4 -0
- package/dist/core/rate-limit.d.ts.map +1 -0
- package/dist/core/rate-limit.js +177 -0
- package/dist/core/rate-limit.js.map +1 -0
- package/dist/core/regions.d.ts +47 -0
- package/dist/core/regions.d.ts.map +1 -0
- package/dist/core/regions.js +620 -0
- package/dist/core/regions.js.map +1 -0
- package/dist/core/session-responses.d.ts +10 -0
- package/dist/core/session-responses.d.ts.map +1 -0
- package/dist/core/session-responses.js +22 -0
- package/dist/core/session-responses.js.map +1 -0
- package/dist/core/sessions.d.ts +50 -0
- package/dist/core/sessions.d.ts.map +1 -0
- package/dist/core/sessions.js +421 -0
- package/dist/core/sessions.js.map +1 -0
- package/dist/core/two-factor.d.ts +34 -0
- package/dist/core/two-factor.d.ts.map +1 -0
- package/dist/core/two-factor.js +494 -0
- package/dist/core/two-factor.js.map +1 -0
- package/dist/core/users.d.ts +12 -0
- package/dist/core/users.d.ts.map +1 -0
- package/dist/core/users.js +71 -0
- package/dist/core/users.js.map +1 -0
- package/dist/core/verifications.d.ts +52 -0
- package/dist/core/verifications.d.ts.map +1 -0
- package/dist/core/verifications.js +521 -0
- package/dist/core/verifications.js.map +1 -0
- package/dist/http/envelopes.d.ts +10 -0
- package/dist/http/envelopes.d.ts.map +1 -0
- package/dist/http/envelopes.js +99 -0
- package/dist/http/envelopes.js.map +1 -0
- package/dist/http/router.d.ts +16 -0
- package/dist/http/router.d.ts.map +1 -0
- package/dist/http/router.js +347 -0
- package/dist/http/router.js.map +1 -0
- package/dist/index.d.ts +10 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +92 -227
- package/dist/index.js.map +1 -1
- package/dist/openapi.d.ts +4 -0
- package/dist/openapi.d.ts.map +1 -0
- package/dist/openapi.js +43 -0
- package/dist/openapi.js.map +1 -0
- package/dist/plugin-runner.d.ts +12 -0
- package/dist/plugin-runner.d.ts.map +1 -0
- package/dist/plugin-runner.js +206 -0
- package/dist/plugin-runner.js.map +1 -0
- package/dist/plugin-types.d.ts +205 -0
- package/dist/plugin-types.d.ts.map +1 -0
- package/dist/plugin-types.js +2 -0
- package/dist/plugin-types.js.map +1 -0
- package/dist/schema.d.ts +5 -10
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +113 -70
- package/dist/schema.js.map +1 -1
- package/dist/types.d.ts +433 -69
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -4
- package/dist/types.js.map +1 -1
- package/package.json +44 -18
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { AuthFnError as AuthFnErrorClass, AuthFnPluginAbortedError } from './core/errors.js';
|
|
2
|
+
import { emitAuthEvent, eventRequestId } from './core/observability.js';
|
|
3
|
+
const BEFORE_HOOK_NAMES = [
|
|
4
|
+
'beforeUserCreate',
|
|
5
|
+
'beforeSessionIssue',
|
|
6
|
+
'beforeChallengeSend',
|
|
7
|
+
'beforeOAuthStart',
|
|
8
|
+
'beforeAccountDelete'
|
|
9
|
+
];
|
|
10
|
+
const AFTER_HOOK_NAMES = [
|
|
11
|
+
'afterUserCreate',
|
|
12
|
+
'afterSessionIssue',
|
|
13
|
+
'afterChallengeSend',
|
|
14
|
+
'afterOAuthCallback',
|
|
15
|
+
'afterAccountDelete',
|
|
16
|
+
'afterAccountDeleteFailure'
|
|
17
|
+
];
|
|
18
|
+
export function createPluginRuntimeContext(config, hooks = config.hooks ?? {}) {
|
|
19
|
+
return {
|
|
20
|
+
config,
|
|
21
|
+
namespace: config.namespace ?? 'authfn',
|
|
22
|
+
basePath: config.basePath ?? '/auth',
|
|
23
|
+
hooks,
|
|
24
|
+
environment: config.environment
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function validatePlugins(config) {
|
|
28
|
+
for (const plugin of config.plugins) {
|
|
29
|
+
plugin.validateConfig?.(config);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function composePluginRoutes(config, hooks = composePluginHooks(config)) {
|
|
33
|
+
const runtimeContext = createPluginRuntimeContext(config, hooks);
|
|
34
|
+
return config.plugins.flatMap((plugin) => plugin.routes?.(runtimeContext) ?? []);
|
|
35
|
+
}
|
|
36
|
+
export function composePluginHooks(config) {
|
|
37
|
+
return {
|
|
38
|
+
beforeUserCreate: async (ctx, input) => runBeforeHooks(config.plugins, config, config.hooks, 'beforeUserCreate', ctx, input),
|
|
39
|
+
beforeSessionIssue: async (ctx, input) => runBeforeHooks(config.plugins, config, config.hooks, 'beforeSessionIssue', ctx, input),
|
|
40
|
+
beforeChallengeSend: async (ctx, input) => runBeforeHooks(config.plugins, config, config.hooks, 'beforeChallengeSend', ctx, input),
|
|
41
|
+
beforeOAuthStart: async (ctx, input) => runBeforeHooks(config.plugins, config, config.hooks, 'beforeOAuthStart', ctx, input),
|
|
42
|
+
beforeAccountDelete: async (ctx, input) => runBeforeHooks(config.plugins, config, config.hooks, 'beforeAccountDelete', ctx, input),
|
|
43
|
+
afterUserCreate: async (ctx, user) => runAfterHooks(config.plugins, config, config.hooks, 'afterUserCreate', ctx, user),
|
|
44
|
+
afterSessionIssue: async (ctx, session) => runAfterHooks(config.plugins, config, config.hooks, 'afterSessionIssue', ctx, session),
|
|
45
|
+
afterChallengeSend: async (ctx, result) => runAfterHooks(config.plugins, config, config.hooks, 'afterChallengeSend', ctx, result),
|
|
46
|
+
afterOAuthCallback: async (ctx, result) => runAfterHooks(config.plugins, config, config.hooks, 'afterOAuthCallback', ctx, result),
|
|
47
|
+
afterAccountDelete: async (ctx, result) => runAfterHooks(config.plugins, config, config.hooks, 'afterAccountDelete', ctx, result),
|
|
48
|
+
afterAccountDeleteFailure: async (ctx, failure) => runAfterHooks(config.plugins, config, config.hooks, 'afterAccountDeleteFailure', ctx, failure)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export function createPluginRunner(config) {
|
|
52
|
+
validatePlugins(config);
|
|
53
|
+
const hooks = composePluginHooks(config);
|
|
54
|
+
return {
|
|
55
|
+
routes: composePluginRoutes(config, hooks),
|
|
56
|
+
hooks
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async function runBeforeHooks(plugins, config, configHooks, hookName, ctx, input) {
|
|
60
|
+
let current = input;
|
|
61
|
+
for (const plugin of plugins) {
|
|
62
|
+
const hook = plugin.hooks?.[hookName];
|
|
63
|
+
if (!hook) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
let result;
|
|
67
|
+
try {
|
|
68
|
+
result = await hook?.({ ...ctx, config: ctx.config, pluginName: plugin.name }, current ?? input);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
await emitAuthEvent(config, {
|
|
72
|
+
type: 'authfn.plugin.failed',
|
|
73
|
+
requestId: eventRequestId(ctx.request),
|
|
74
|
+
actorId: ctx.actorId,
|
|
75
|
+
pluginName: plugin.name,
|
|
76
|
+
hookName,
|
|
77
|
+
outcome: 'aborted',
|
|
78
|
+
metadata: {
|
|
79
|
+
errorCode: readErrorCode(error),
|
|
80
|
+
retriable: readRetryable(error)
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
if (isAuthFnError(error)) {
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
throw new AuthFnPluginAbortedError(`${plugin.name}.${hookName} aborted authfn execution`, {
|
|
87
|
+
pluginName: plugin.name,
|
|
88
|
+
hookName,
|
|
89
|
+
actorId: ctx.actorId
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (result !== undefined) {
|
|
93
|
+
current = result;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const configHook = configHooks?.[hookName];
|
|
97
|
+
if (!configHook) {
|
|
98
|
+
return current;
|
|
99
|
+
}
|
|
100
|
+
let result;
|
|
101
|
+
try {
|
|
102
|
+
result = await configHook?.({ ...ctx, config: ctx.config }, current ?? input);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
await emitAuthEvent(config, {
|
|
106
|
+
type: 'authfn.plugin.failed',
|
|
107
|
+
requestId: eventRequestId(ctx.request),
|
|
108
|
+
actorId: ctx.actorId,
|
|
109
|
+
pluginName: 'config',
|
|
110
|
+
hookName,
|
|
111
|
+
outcome: 'aborted',
|
|
112
|
+
metadata: {
|
|
113
|
+
errorCode: readErrorCode(error),
|
|
114
|
+
retriable: readRetryable(error)
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
if (isAuthFnError(error)) {
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
throw new AuthFnPluginAbortedError(`config.${hookName} aborted authfn execution`, {
|
|
121
|
+
pluginName: 'config',
|
|
122
|
+
hookName,
|
|
123
|
+
actorId: ctx.actorId
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return result ?? current;
|
|
127
|
+
}
|
|
128
|
+
async function runAfterHooks(plugins, config, configHooks, hookName, ctx, payload) {
|
|
129
|
+
for (const plugin of plugins) {
|
|
130
|
+
const hook = plugin.hooks?.[hookName];
|
|
131
|
+
if (!hook) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
await hook({ ...ctx, config: ctx.config, pluginName: plugin.name }, payload);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
const failurePolicy = getAfterHookFailurePolicy(plugin, hookName);
|
|
139
|
+
await emitAuthEvent(config, {
|
|
140
|
+
type: 'authfn.plugin.failed',
|
|
141
|
+
requestId: eventRequestId(ctx.request),
|
|
142
|
+
actorId: ctx.actorId,
|
|
143
|
+
pluginName: plugin.name,
|
|
144
|
+
hookName,
|
|
145
|
+
outcome: failurePolicy === 'fail' ? 'aborted' : 'observed',
|
|
146
|
+
metadata: {
|
|
147
|
+
errorCode: readErrorCode(error),
|
|
148
|
+
retriable: readRetryable(error)
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
if (failurePolicy === 'fail') {
|
|
152
|
+
if (isAuthFnError(error)) {
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
throw new AuthFnPluginAbortedError(`${plugin.name}.${hookName} aborted authfn execution`, {
|
|
156
|
+
pluginName: plugin.name,
|
|
157
|
+
hookName,
|
|
158
|
+
actorId: ctx.actorId,
|
|
159
|
+
cause: error instanceof Error ? error.message : String(error)
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const configHook = configHooks?.[hookName];
|
|
165
|
+
if (!configHook) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
await configHook({ ...ctx, config: ctx.config }, payload);
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
await emitAuthEvent(config, {
|
|
173
|
+
type: 'authfn.plugin.failed',
|
|
174
|
+
requestId: eventRequestId(ctx.request),
|
|
175
|
+
actorId: ctx.actorId,
|
|
176
|
+
pluginName: 'config',
|
|
177
|
+
hookName,
|
|
178
|
+
outcome: 'observed',
|
|
179
|
+
metadata: {
|
|
180
|
+
errorCode: readErrorCode(error),
|
|
181
|
+
retriable: readRetryable(error)
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function getAfterHookFailurePolicy(plugin, hookName) {
|
|
187
|
+
return plugin.hookFailurePolicy?.[hookName] ?? 'observe';
|
|
188
|
+
}
|
|
189
|
+
function isAuthFnError(error) {
|
|
190
|
+
if (error instanceof AuthFnErrorClass) {
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
return Boolean(error
|
|
194
|
+
&& typeof error === 'object'
|
|
195
|
+
&& Object.prototype.hasOwnProperty.call(error, 'code')
|
|
196
|
+
&& typeof error.code === 'string'
|
|
197
|
+
&& Object.prototype.hasOwnProperty.call(error, 'status')
|
|
198
|
+
&& typeof error.status === 'number');
|
|
199
|
+
}
|
|
200
|
+
function readErrorCode(error) {
|
|
201
|
+
return isAuthFnError(error) ? error.code : 'AUTHFN_INTERNAL_ERROR';
|
|
202
|
+
}
|
|
203
|
+
function readRetryable(error) {
|
|
204
|
+
return isAuthFnError(error) ? Boolean(error.retryable) : false;
|
|
205
|
+
}
|
|
206
|
+
//# sourceMappingURL=plugin-runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin-runner.js","sourceRoot":"","sources":["../src/plugin-runner.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,IAAI,gBAAgB,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAExE,MAAM,iBAAiB,GAAG;IACxB,kBAAkB;IAClB,oBAAoB;IACpB,qBAAqB;IACrB,kBAAkB;IAClB,qBAAqB;CACb,CAAC;AAEX,MAAM,gBAAgB,GAAG;IACvB,iBAAiB;IACjB,mBAAmB;IACnB,oBAAoB;IACpB,oBAAoB;IACpB,oBAAoB;IACpB,2BAA2B;CACnB,CAAC;AAUX,MAAM,UAAU,0BAA0B,CACxC,MAA2B,EAC3B,QAA8B,MAAM,CAAC,KAAK,IAAI,EAAE;IAEhD,OAAO;QACL,MAAM;QACN,SAAS,EAAE,MAAM,CAAC,SAAS,IAAI,QAAQ;QACvC,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,OAAO;QACpC,KAAK;QACL,WAAW,EAAE,MAAM,CAAC,WAAW;KAChC,CAAC;AACJ,CAAC;AAED,MAAM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,IAAI,CAAC;IACd,CAAC;IAED,OAAO,OAAO,CACZ,KAAK;WACA,OAAO,KAAK,KAAK,QAAQ;WACzB,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC;WACnD,OAAQ,KAA4B,CAAC,IAAI,KAAK,QAAQ;WACtD,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC;WACrD,OAAQ,KAA8B,CAAC,MAAM,KAAK,QAAQ,CAChE,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,uBAAuB,CAAC;AACrE,CAAC;AAED,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AACjE,CAAC"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { ConditionalKVStoreAdapter, TableSchema } from '@superfunctions/db';
|
|
2
|
+
import type { ObservabilityInput } from '@superfunctions/observability';
|
|
3
|
+
import type { AuthFnPlugin } from './types.js';
|
|
4
|
+
import type { AuthFnCookieConfig, AuthFnDeliveryMessageResolver, AuthFnPasswordCompromiseChecker, AuthFnEnvironment, AuthFnDeliveryProvider, AuthFnEvent } from './types.js';
|
|
5
|
+
export interface AuthFnBundledPluginConfig {
|
|
6
|
+
schema?: TableSchema[];
|
|
7
|
+
}
|
|
8
|
+
export interface PasswordPluginConfig extends AuthFnBundledPluginConfig {
|
|
9
|
+
compromisedPasswordChecker?: AuthFnPasswordCompromiseChecker;
|
|
10
|
+
requireEmailVerifiedForSignIn?: boolean;
|
|
11
|
+
/** @deprecated Configure password OTP delivery through pluginRuntime.password.otp. */
|
|
12
|
+
otp?: Partial<EmailOtpPluginRuntimeConfig>;
|
|
13
|
+
}
|
|
14
|
+
export interface PasswordPluginRuntimeConfig {
|
|
15
|
+
/** OTP delivery and challenge settings used by password sign-up, sign-in, and reset flows. */
|
|
16
|
+
otp?: EmailOtpPluginRuntimeConfig;
|
|
17
|
+
}
|
|
18
|
+
export interface EmailOtpPluginConfig extends AuthFnBundledPluginConfig {
|
|
19
|
+
}
|
|
20
|
+
export interface EmailOtpPluginRuntimeConfig {
|
|
21
|
+
/** Provider responsible for sending OTP challenges and receiving OTP lifecycle events. */
|
|
22
|
+
delivery: AuthFnDeliveryProvider;
|
|
23
|
+
/** Optional resolver used to customize the OTP message before it is passed to the delivery provider. */
|
|
24
|
+
message?: AuthFnDeliveryMessageResolver;
|
|
25
|
+
/** Optional code generator for tests or custom OTP formats; defaults to the built-in numeric generator. */
|
|
26
|
+
codeGenerator?: () => string;
|
|
27
|
+
/** Clock source used for challenge creation and expiry checks; primarily useful for tests. */
|
|
28
|
+
now?: () => Date;
|
|
29
|
+
/** Number of seconds before an OTP challenge expires. */
|
|
30
|
+
challengeTtlSeconds?: number;
|
|
31
|
+
/** Maximum verification attempts allowed before a challenge is rejected. */
|
|
32
|
+
maxAttempts?: number;
|
|
33
|
+
}
|
|
34
|
+
export interface ApiKeyPluginConfig extends AuthFnBundledPluginConfig {
|
|
35
|
+
secretPrefix?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface ApiKeyPluginRuntimeConfig {
|
|
38
|
+
/** Clock source used for API key timestamps and expiry checks; primarily useful for tests. */
|
|
39
|
+
now?: () => Date;
|
|
40
|
+
}
|
|
41
|
+
export interface TwoFactorPluginConfig extends AuthFnBundledPluginConfig {
|
|
42
|
+
}
|
|
43
|
+
export interface TwoFactorPluginRuntimeConfig {
|
|
44
|
+
/** Issuer name displayed by authenticator apps for generated TOTP enrollments. */
|
|
45
|
+
issuer?: string;
|
|
46
|
+
/** Clock source used for TOTP challenge creation and verification; primarily useful for tests. */
|
|
47
|
+
now?: () => Date;
|
|
48
|
+
/** Number of seconds before a two-factor challenge expires. */
|
|
49
|
+
challengeTtlSeconds?: number;
|
|
50
|
+
/** Number of recovery codes generated for each confirmed enrollment. */
|
|
51
|
+
recoveryCodeCount?: number;
|
|
52
|
+
/** Number of digits in generated TOTP codes. */
|
|
53
|
+
digits?: number;
|
|
54
|
+
/** TOTP time-step duration in seconds. */
|
|
55
|
+
periodSeconds?: number;
|
|
56
|
+
/** Number of adjacent TOTP windows accepted during verification. */
|
|
57
|
+
window?: number;
|
|
58
|
+
/** Key reference passed to encryptionKeyResolver when encrypting two-factor secrets. */
|
|
59
|
+
encryptionKeyRef?: string;
|
|
60
|
+
/** Resolves the encryption key used for two-factor secret storage. */
|
|
61
|
+
encryptionKeyResolver?: (keyRef: string) => Promise<Buffer> | Buffer;
|
|
62
|
+
}
|
|
63
|
+
export interface NativeHandoffPluginConfig extends AuthFnBundledPluginConfig {
|
|
64
|
+
codeTtlSeconds?: number;
|
|
65
|
+
}
|
|
66
|
+
export interface NativeHandoffPluginRuntimeConfig {
|
|
67
|
+
/** Clock source used for native handoff code timestamps and expiry checks; primarily useful for tests. */
|
|
68
|
+
now?: () => Date;
|
|
69
|
+
}
|
|
70
|
+
export interface AuthFnMultiRegionRegionConfig {
|
|
71
|
+
regionId: string;
|
|
72
|
+
authority: string;
|
|
73
|
+
domain?: string;
|
|
74
|
+
hosts?: string[];
|
|
75
|
+
issuer?: string;
|
|
76
|
+
baseUrl?: string;
|
|
77
|
+
cookie?: Partial<AuthFnCookieConfig>;
|
|
78
|
+
oauth?: AuthFnEnvironment['oauth'];
|
|
79
|
+
}
|
|
80
|
+
export interface AuthFnMultiRegionLookupInput {
|
|
81
|
+
identifier: string;
|
|
82
|
+
request?: Request;
|
|
83
|
+
environment: AuthFnEnvironment;
|
|
84
|
+
}
|
|
85
|
+
export interface AuthFnMultiRegionLookupResult {
|
|
86
|
+
userId?: string;
|
|
87
|
+
regionId: string;
|
|
88
|
+
authority: string;
|
|
89
|
+
domain?: string;
|
|
90
|
+
}
|
|
91
|
+
export interface AuthFnMultiRegionRegistrationInput {
|
|
92
|
+
userId: string;
|
|
93
|
+
primaryEmail?: string;
|
|
94
|
+
regionId: string;
|
|
95
|
+
authority: string;
|
|
96
|
+
domain?: string;
|
|
97
|
+
request?: Request;
|
|
98
|
+
environment: AuthFnEnvironment;
|
|
99
|
+
}
|
|
100
|
+
export interface AuthFnRegionLookupRecord {
|
|
101
|
+
identifier: string;
|
|
102
|
+
userId?: string;
|
|
103
|
+
regionId: string;
|
|
104
|
+
authority: string;
|
|
105
|
+
domain?: string;
|
|
106
|
+
createdAt: Date | string;
|
|
107
|
+
updatedAt: Date | string;
|
|
108
|
+
}
|
|
109
|
+
export type AuthFnIdentityPlacementState = 'active' | 'moving' | 'deleting' | 'tombstoned';
|
|
110
|
+
/**
|
|
111
|
+
* Canonical ownership record for an AuthFn identity. The directory owns only
|
|
112
|
+
* placement metadata; cell destinations remain private to the cell registry.
|
|
113
|
+
*/
|
|
114
|
+
export interface AuthFnIdentityPlacement {
|
|
115
|
+
identityKey: string;
|
|
116
|
+
regionId: string;
|
|
117
|
+
epoch: number;
|
|
118
|
+
state: AuthFnIdentityPlacementState;
|
|
119
|
+
movingToRegionId?: string;
|
|
120
|
+
previousRegionId?: string;
|
|
121
|
+
updatedAt: Date | string;
|
|
122
|
+
}
|
|
123
|
+
export interface AuthFnIdentityPlacementDirectoryAdapter {
|
|
124
|
+
/** Strongly consistent with every successful claim and epoch transition. */
|
|
125
|
+
get(identityKey: string): Promise<AuthFnIdentityPlacement | null>;
|
|
126
|
+
/** Globally atomic across every gateway writer for the same identity key. */
|
|
127
|
+
putIfAbsent(placement: AuthFnIdentityPlacement): Promise<{
|
|
128
|
+
inserted: boolean;
|
|
129
|
+
existing?: AuthFnIdentityPlacement;
|
|
130
|
+
}>;
|
|
131
|
+
/** Globally atomic across every writer and fenced by both epoch and state. */
|
|
132
|
+
compareAndSet(input: {
|
|
133
|
+
identityKey: string;
|
|
134
|
+
expectedEpoch: number;
|
|
135
|
+
expectedState: AuthFnIdentityPlacementState;
|
|
136
|
+
placement: AuthFnIdentityPlacement;
|
|
137
|
+
}): Promise<{
|
|
138
|
+
updated: boolean;
|
|
139
|
+
existing?: AuthFnIdentityPlacement;
|
|
140
|
+
}>;
|
|
141
|
+
}
|
|
142
|
+
export interface AuthFnRoutingSigningKey {
|
|
143
|
+
keyId: string;
|
|
144
|
+
secret: string | Uint8Array;
|
|
145
|
+
}
|
|
146
|
+
export interface AuthFnRoutingKeyring {
|
|
147
|
+
active: AuthFnRoutingSigningKey;
|
|
148
|
+
/** Verification-only keys retained during rotation. */
|
|
149
|
+
previous?: AuthFnRoutingSigningKey[];
|
|
150
|
+
}
|
|
151
|
+
export interface AuthFnRoutingReplayStore {
|
|
152
|
+
/** Atomically claims a nonce until its expiry. False means replay. */
|
|
153
|
+
claim(nonce: string, expiresAt: number): Promise<boolean>;
|
|
154
|
+
}
|
|
155
|
+
interface AuthFnCanonicalRoutingBase {
|
|
156
|
+
/** Stable public issuer/base URL used for discovery, OAuth, and cookies. */
|
|
157
|
+
publicAuthority?: string;
|
|
158
|
+
/** Canonical cookie policy. In gateway mode this must not vary by cell. */
|
|
159
|
+
canonicalCookie?: Partial<AuthFnCookieConfig>;
|
|
160
|
+
/** Canonical OAuth policy merged independently of the execution cell. */
|
|
161
|
+
canonicalOAuth?: AuthFnEnvironment['oauth'];
|
|
162
|
+
}
|
|
163
|
+
export interface AuthFnDirectRoutingConfig extends AuthFnCanonicalRoutingBase {
|
|
164
|
+
mode: 'direct';
|
|
165
|
+
placementDirectory?: never;
|
|
166
|
+
identityKeyForIdentifier?: never;
|
|
167
|
+
identityKeyForUserId?: never;
|
|
168
|
+
cell?: never;
|
|
169
|
+
}
|
|
170
|
+
export interface AuthFnGatewayRoutingConfig extends AuthFnCanonicalRoutingBase {
|
|
171
|
+
mode: 'gateway';
|
|
172
|
+
publicAuthority: string;
|
|
173
|
+
/** Required in gateway mode on both the gateway and regional cells. */
|
|
174
|
+
placementDirectory: AuthFnIdentityPlacementDirectoryAdapter;
|
|
175
|
+
/** Maps normalized public identifiers to the same stable key used by the gateway. */
|
|
176
|
+
identityKeyForIdentifier: (identifier: string) => string;
|
|
177
|
+
/** Resolves persisted user IDs to the exact stable placement key used by the gateway. */
|
|
178
|
+
identityKeyForUserId: (userId: string) => string | Promise<string>;
|
|
179
|
+
/** Required by a regional cell to validate gateway assertions. */
|
|
180
|
+
cell?: {
|
|
181
|
+
regionId: string;
|
|
182
|
+
audience: string;
|
|
183
|
+
keyring: AuthFnRoutingKeyring;
|
|
184
|
+
replayStore: AuthFnRoutingReplayStore;
|
|
185
|
+
clockSkewSeconds?: number;
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
export type AuthFnCanonicalRoutingConfig = AuthFnDirectRoutingConfig | AuthFnGatewayRoutingConfig;
|
|
189
|
+
export interface MultiRegionPluginConfig extends AuthFnBundledPluginConfig {
|
|
190
|
+
}
|
|
191
|
+
export interface MultiRegionPluginRuntimeConfig {
|
|
192
|
+
/** Region definitions available to the multi-region resolver and routing plugin. */
|
|
193
|
+
regions?: AuthFnMultiRegionRegionConfig[];
|
|
194
|
+
/** Region ID used when no request host or identifier lookup selects another region. */
|
|
195
|
+
defaultRegionId?: string;
|
|
196
|
+
/** Store used to look up and register the region that owns a user identifier. */
|
|
197
|
+
lookupStore?: ConditionalKVStoreAdapter;
|
|
198
|
+
/** Observability sink for multi-region lookup, registration, and conflict events. */
|
|
199
|
+
observability?: ObservabilityInput<AuthFnEvent>;
|
|
200
|
+
/** Canonical-gateway routing. Omit (or use direct) for legacy regional clients. */
|
|
201
|
+
routing?: AuthFnCanonicalRoutingConfig;
|
|
202
|
+
}
|
|
203
|
+
export type AuthFnPluginFactory<TConfig extends AuthFnBundledPluginConfig> = (config?: TConfig) => AuthFnPlugin;
|
|
204
|
+
export {};
|
|
205
|
+
//# sourceMappingURL=plugin-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
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AAE,OAAO,KACb,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin-types.js","sourceRoot":"","sources":["../src/plugin-types.ts"],"names":[],"mappings":""}
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Schema definition for authfn
|
|
3
|
-
* Used by @superfunctions/cli to generate migrations
|
|
4
|
-
*/
|
|
5
1
|
import type { TableSchema } from '@superfunctions/db';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
2
|
+
import type { AuthFnConfig, AuthFnSchemaDefinition } from './types.js';
|
|
3
|
+
export declare const AUTHFN_SCHEMA_VERSION = 1;
|
|
4
|
+
export declare function getSchema(config: AuthFnConfig): AuthFnSchemaDefinition;
|
|
5
|
+
export declare function createCoreTables(): TableSchema[];
|
|
6
|
+
export declare function normalizeTableSchema(table: TableSchema): TableSchema;
|
|
12
7
|
//# sourceMappingURL=schema.d.ts.map
|
package/dist/schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEtD,OAAO,KAAK,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAEvE,eAAO,MAAM,qBAAqB,IAAI,CAAC;AAEvC,wBAAgB,SAAS,CAAC,MAAM,EAAE,YAAY,GAAG,sBAAsB,CAetE;AAED,wBAAgB,gBAAgB,IAAI,WAAW,EAAE,CAyDhD;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,WAAW,GAAG,WAAW,CAepE"}
|
package/dist/schema.js
CHANGED
|
@@ -1,77 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* Used by @superfunctions/cli to generate migrations
|
|
4
|
-
*/
|
|
1
|
+
import { AuthFnConfigError, AuthFnConflictError } from './types.js';
|
|
2
|
+
export const AUTHFN_SCHEMA_VERSION = 1;
|
|
5
3
|
export function getSchema(config) {
|
|
6
|
-
|
|
4
|
+
if (!Array.isArray(config.plugins)) {
|
|
5
|
+
throw new AuthFnConfigError('authfn plugins must be provided as an array');
|
|
6
|
+
}
|
|
7
|
+
const baseTables = createCoreTables();
|
|
8
|
+
const pluginTables = config.plugins.flatMap((plugin) => plugin.schema?.(config) ?? []);
|
|
9
|
+
const orderedTables = [...baseTables, ...pluginTables].map((table) => normalizeTableSchema(table));
|
|
10
|
+
assertNoTableConflicts(orderedTables);
|
|
7
11
|
return {
|
|
8
|
-
version:
|
|
9
|
-
schemas:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
type: 'json',
|
|
42
|
-
required: false,
|
|
43
|
-
fieldName: 'metadata',
|
|
44
|
-
},
|
|
45
|
-
expiresAt: {
|
|
46
|
-
type: 'date',
|
|
47
|
-
required: false,
|
|
48
|
-
fieldName: 'expires_at',
|
|
49
|
-
},
|
|
50
|
-
lastUsedAt: {
|
|
51
|
-
type: 'date',
|
|
52
|
-
required: false,
|
|
53
|
-
fieldName: 'last_used_at',
|
|
54
|
-
},
|
|
55
|
-
revokedAt: {
|
|
56
|
-
type: 'date',
|
|
57
|
-
required: false,
|
|
58
|
-
fieldName: 'revoked_at',
|
|
59
|
-
},
|
|
60
|
-
createdAt: {
|
|
61
|
-
type: 'date',
|
|
62
|
-
required: true,
|
|
63
|
-
fieldName: 'created_at',
|
|
64
|
-
},
|
|
12
|
+
version: AUTHFN_SCHEMA_VERSION,
|
|
13
|
+
schemas: orderedTables
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function createCoreTables() {
|
|
17
|
+
return [
|
|
18
|
+
{
|
|
19
|
+
modelName: 'users',
|
|
20
|
+
fields: {
|
|
21
|
+
id: { type: 'string', required: true, fieldName: 'id' },
|
|
22
|
+
primaryEmail: { type: 'string', required: false, fieldName: 'primary_email' },
|
|
23
|
+
emailVerifiedAt: { type: 'date', required: false, fieldName: 'email_verified_at' },
|
|
24
|
+
metadata: { type: 'json', required: false, fieldName: 'metadata' },
|
|
25
|
+
createdAt: { type: 'date', required: true, fieldName: 'created_at' },
|
|
26
|
+
updatedAt: { type: 'date', required: true, fieldName: 'updated_at' }
|
|
27
|
+
},
|
|
28
|
+
indexes: [
|
|
29
|
+
{
|
|
30
|
+
name: 'idx_authfn_users_primary_email',
|
|
31
|
+
fields: ['primaryEmail'],
|
|
32
|
+
unique: true
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
modelName: 'sessions',
|
|
38
|
+
fields: {
|
|
39
|
+
id: { type: 'string', required: true, fieldName: 'id' },
|
|
40
|
+
userId: {
|
|
41
|
+
type: 'string',
|
|
42
|
+
required: true,
|
|
43
|
+
fieldName: 'user_id',
|
|
44
|
+
references: { model: 'users', field: 'id', onDelete: 'cascade' }
|
|
65
45
|
},
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
46
|
+
tokenHash: { type: 'string', required: true, fieldName: 'token_hash' },
|
|
47
|
+
csrfHash: { type: 'string', required: false, fieldName: 'csrf_hash' },
|
|
48
|
+
methods: { type: 'json', required: true, fieldName: 'methods' },
|
|
49
|
+
metadata: { type: 'json', required: false, fieldName: 'metadata' },
|
|
50
|
+
expiresAt: { type: 'date', required: true, fieldName: 'expires_at' },
|
|
51
|
+
revokedAt: { type: 'date', required: false, fieldName: 'revoked_at' },
|
|
52
|
+
createdAt: { type: 'date', required: true, fieldName: 'created_at' },
|
|
53
|
+
updatedAt: { type: 'date', required: true, fieldName: 'updated_at' },
|
|
54
|
+
lastAuthenticatedAt: { type: 'date', required: false, fieldName: 'last_authenticated_at' }
|
|
73
55
|
},
|
|
74
|
-
|
|
56
|
+
indexes: [
|
|
57
|
+
{
|
|
58
|
+
name: 'idx_authfn_sessions_expires_at',
|
|
59
|
+
fields: ['expiresAt']
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'idx_authfn_sessions_token_hash',
|
|
63
|
+
fields: ['tokenHash'],
|
|
64
|
+
unique: true
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: 'idx_authfn_sessions_user_id_created_at',
|
|
68
|
+
fields: ['userId', 'createdAt']
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
export function normalizeTableSchema(table) {
|
|
75
|
+
return {
|
|
76
|
+
...table,
|
|
77
|
+
fields: Object.fromEntries(Object.entries(table.fields)
|
|
78
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
79
|
+
.map(([fieldName, field]) => [fieldName, normalizeFieldSchema(field)])),
|
|
80
|
+
indexes: table.indexes
|
|
81
|
+
? [...table.indexes].sort((left, right) => left.name.localeCompare(right.name))
|
|
82
|
+
: undefined,
|
|
83
|
+
constraints: table.constraints
|
|
84
|
+
? [...table.constraints].sort((left, right) => left.name.localeCompare(right.name))
|
|
85
|
+
: undefined
|
|
75
86
|
};
|
|
76
87
|
}
|
|
88
|
+
function normalizeFieldSchema(field) {
|
|
89
|
+
if (field.type !== 'date' && field.type !== 'datetime') {
|
|
90
|
+
return field;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
...field,
|
|
94
|
+
dateValueType: field.dateValueType ?? 'date',
|
|
95
|
+
dateStorageType: field.dateStorageType ?? 'timestamptz'
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function assertNoTableConflicts(tables) {
|
|
99
|
+
const seenTableNames = new Set();
|
|
100
|
+
for (const table of tables) {
|
|
101
|
+
if (seenTableNames.has(table.modelName)) {
|
|
102
|
+
throw new AuthFnConflictError(`duplicate authfn table schema: ${table.modelName}`, {
|
|
103
|
+
tableName: table.modelName
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
seenTableNames.add(table.modelName);
|
|
107
|
+
const seenColumnNames = new Set();
|
|
108
|
+
for (const [fieldName, fieldSchema] of Object.entries(table.fields)) {
|
|
109
|
+
const columnName = fieldSchema.fieldName ?? fieldName;
|
|
110
|
+
if (seenColumnNames.has(columnName)) {
|
|
111
|
+
throw new AuthFnConflictError(`duplicate authfn column mapping: ${table.modelName}.${columnName}`, {
|
|
112
|
+
tableName: table.modelName,
|
|
113
|
+
columnName
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
seenColumnNames.add(columnName);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
77
120
|
//# sourceMappingURL=schema.js.map
|