@authfn/email-otp 0.1.1

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.
@@ -0,0 +1,5 @@
1
+ import type { EmailOtpPluginConfig, EmailOtpPluginRuntimeConfig } from 'authfn/plugin-types';
2
+ import type { AuthFnPlugin } from 'authfn';
3
+ export type { EmailOtpPluginConfig, EmailOtpPluginRuntimeConfig } from 'authfn/plugin-types';
4
+ export declare function authFnEmailOtpPlugin(config?: EmailOtpPluginConfig): AuthFnPlugin<'emailOtp', EmailOtpPluginRuntimeConfig, true>;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,oBAAoB,EACpB,2BAA2B,EAC5B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAsD,MAAM,QAAQ,CAAC;AAgB/F,YAAY,EACV,oBAAoB,EACpB,2BAA2B,EAC5B,MAAM,qBAAqB,CAAC;AAE7B,wBAAgB,oBAAoB,CAClC,MAAM,GAAE,oBAAyB,GAChC,YAAY,CAAC,UAAU,EAAE,2BAA2B,EAAE,IAAI,CAAC,CAM7D"}
package/dist/index.js ADDED
@@ -0,0 +1,149 @@
1
+ import { createAuthFnRouteMeta, readOptionalJson } from 'authfn/http/router';
2
+ import { issueSession } from 'authfn/core/sessions';
3
+ import { jsonSuccess } from 'authfn/http/envelopes';
4
+ import { emitAuthEvent, eventRequestId } from 'authfn/core/observability';
5
+ import { sendOtpChallenge, verifyOtpChallenge } from 'authfn/core/verifications';
6
+ import { resolveEnvironment } from 'authfn/core/environment';
7
+ import { emitAccountLinkedEvent } from 'authfn/core/account-linking';
8
+ import { buildSessionResponse } from 'authfn/core/session-responses';
9
+ import { beginTwoFactorChallenge, createPendingTwoFactorResponse, getTwoFactorPluginConfig } from 'authfn/core/two-factor';
10
+ import { readPluginRuntimeConfig } from 'authfn/core/plugin-runtime';
11
+ export function authFnEmailOtpPlugin(config = {}) {
12
+ return {
13
+ name: 'emailOtp',
14
+ schema: () => config.schema ?? createOtpSchema(),
15
+ routes: (ctx) => createOtpRoutes(ctx)
16
+ };
17
+ }
18
+ function createOtpSchema() {
19
+ return [
20
+ {
21
+ modelName: 'otp_challenges',
22
+ fields: {
23
+ id: { type: 'string', required: true, fieldName: 'id' },
24
+ purpose: { type: 'string', required: true, fieldName: 'purpose' },
25
+ email: { type: 'string', required: true, fieldName: 'email' },
26
+ codeHash: { type: 'string', required: true, fieldName: 'code_hash' },
27
+ attemptCount: { type: 'number', required: true, fieldName: 'attempt_count' },
28
+ deliveryMetadata: { type: 'json', required: false, fieldName: 'delivery_metadata' },
29
+ expiresAt: { type: 'date', required: true, fieldName: 'expires_at' },
30
+ consumedAt: { type: 'date', required: false, fieldName: 'consumed_at' },
31
+ createdAt: { type: 'date', required: true, fieldName: 'created_at' },
32
+ updatedAt: { type: 'date', required: true, fieldName: 'updated_at' }
33
+ },
34
+ indexes: [
35
+ {
36
+ name: 'idx_authfn_otp_challenges_email_purpose_created_at',
37
+ fields: ['email', 'purpose', 'createdAt']
38
+ },
39
+ {
40
+ name: 'idx_authfn_otp_challenges_expires_at',
41
+ fields: ['expiresAt']
42
+ }
43
+ ]
44
+ }
45
+ ];
46
+ }
47
+ function createOtpRoutes(ctx) {
48
+ const runtimeConfig = readPluginRuntimeConfig(ctx, 'emailOtp');
49
+ return [
50
+ {
51
+ method: 'POST',
52
+ path: '/otp/send',
53
+ meta: createAuthFnRouteMeta('sendOtp', 'Send an email OTP challenge', {
54
+ mode: 'none'
55
+ }),
56
+ handler: async (request) => {
57
+ const body = await readOptionalJson(request);
58
+ const result = await sendOtpChallenge(ctx.config, ctx.hooks, runtimeConfig, {
59
+ request,
60
+ purpose: body.purpose ?? 'verify-email',
61
+ email: body.email ?? '',
62
+ metadata: body.metadata
63
+ });
64
+ return jsonSuccess(request, {
65
+ challengeId: result.challenge.id,
66
+ sent: result.delivery.sent
67
+ });
68
+ }
69
+ },
70
+ {
71
+ method: 'POST',
72
+ path: '/otp/verify',
73
+ meta: createAuthFnRouteMeta('verifyOtp', 'Verify an email OTP challenge', {
74
+ mode: 'none'
75
+ }),
76
+ handler: async (request) => {
77
+ const body = await readOptionalJson(request);
78
+ const verification = await verifyOtpChallenge(ctx.config, ctx.hooks, runtimeConfig, {
79
+ request,
80
+ purpose: body.purpose ?? 'verify-email',
81
+ email: body.email ?? '',
82
+ code: body.code ?? '',
83
+ profile: body.profile
84
+ });
85
+ if ((body.purpose === 'sign-in' || body.purpose === 'sign-up') && verification.user) {
86
+ if (verification.createdUser) {
87
+ const runtime = await resolveEnvironment(ctx.config, request);
88
+ await emitAuthEvent(ctx.config, {
89
+ type: 'authfn.user.created',
90
+ requestId: eventRequestId(request),
91
+ actorId: verification.user.id,
92
+ userId: verification.user.id,
93
+ regionId: runtime.regionId,
94
+ outcome: 'created',
95
+ metadata: {
96
+ email: verification.user.primaryEmail,
97
+ method: 'email-otp'
98
+ }
99
+ });
100
+ }
101
+ else if (verification.linkedExistingUser) {
102
+ const runtime = await resolveEnvironment(ctx.config, request);
103
+ await emitAccountLinkedEvent(ctx.config, {
104
+ request,
105
+ user: verification.user,
106
+ method: 'email-otp',
107
+ regionId: runtime.regionId,
108
+ metadata: {
109
+ purpose: body.purpose ?? 'verify-email'
110
+ }
111
+ });
112
+ }
113
+ const twoFactorConfig = getTwoFactorPluginConfig(ctx.config);
114
+ if (twoFactorConfig) {
115
+ const challenge = await beginTwoFactorChallenge(ctx.config, verification.user, 'email-otp', twoFactorConfig);
116
+ if (challenge) {
117
+ await emitAuthEvent(ctx.config, {
118
+ type: 'authfn.2fa.challenged',
119
+ requestId: eventRequestId(request),
120
+ actorId: verification.user.id,
121
+ userId: verification.user.id,
122
+ outcome: 'required',
123
+ metadata: {
124
+ challengeId: challenge.challenge.id,
125
+ primaryMethod: 'email-otp'
126
+ }
127
+ });
128
+ throw createPendingTwoFactorResponse(challenge.challenge);
129
+ }
130
+ }
131
+ const issued = await issueSession(ctx.config, ctx.hooks, {
132
+ request,
133
+ userId: verification.user.id,
134
+ primaryEmail: verification.user.primaryEmail,
135
+ methods: ['email-otp']
136
+ });
137
+ const response = buildSessionResponse(issued, body.sessionMode);
138
+ return jsonSuccess(request, response.data, {
139
+ setCookies: response.setCookies
140
+ });
141
+ }
142
+ return jsonSuccess(request, {
143
+ verified: true
144
+ });
145
+ }
146
+ }
147
+ ];
148
+ }
149
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC7E,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAkC,MAAM,+BAA+B,CAAC;AACrG,OAAO,EACL,uBAAuB,EACvB,8BAA8B,EAC9B,wBAAwB,EACzB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAOrE,MAAM,UAAU,oBAAoB,CAClC,SAA+B,EAAE;IAEjC,OAAO;QACL,IAAI,EAAE,UAAU;QAChB,MAAM,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,IAAI,eAAe,EAAE;QAChD,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,eAAe,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EAAE,QAAQ,CAAC,IAAI,EAAE;wBACzC,UAAU,EAAE,QAAQ,CAAC,UAAU;qBAChC,CAAC,CAAC;gBACL,CAAC;gBAED,OAAO,WAAW,CAAC,OAAO,EAAE;oBAC1B,QAAQ,EAAE,IAAI;iBACf,CAAC,CAAC;YACL,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@authfn/email-otp",
3
+ "version": "0.1.1",
4
+ "description": "Email OTP authentication plugin for AuthFn",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "test": "vitest run --passWithNoTests",
20
+ "typecheck": "tsc --noEmit",
21
+ "clean": "rm -rf dist"
22
+ },
23
+ "dependencies": {
24
+ "authfn": "0.2.0",
25
+ "@superfunctions/http": "0.2.0"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^22.0.0",
29
+ "typescript": "^5.6.0",
30
+ "vitest": "^3.2.4"
31
+ }
32
+ }