@authfn/password 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 { PasswordPluginConfig, PasswordPluginRuntimeConfig } from 'authfn/plugin-types';
2
+ import type { AuthFnPlugin } from 'authfn';
3
+ export type { PasswordPluginConfig, PasswordPluginRuntimeConfig } from 'authfn/plugin-types';
4
+ export declare function authFnPasswordPlugin(config?: PasswordPluginConfig): AuthFnPlugin<'password', PasswordPluginRuntimeConfig, false>;
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;AAiB/F,YAAY,EACV,oBAAoB,EACpB,2BAA2B,EAC5B,MAAM,qBAAqB,CAAC;AAE7B,wBAAgB,oBAAoB,CAClC,MAAM,GAAE,oBAAyB,GAChC,YAAY,CAAC,UAAU,EAAE,2BAA2B,EAAE,KAAK,CAAC,CAM9D"}
package/dist/index.js ADDED
@@ -0,0 +1,190 @@
1
+ import { createAuthFnRouteMeta, readOptionalJson } from 'authfn/http/router';
2
+ import { authenticateRequest, issueSession } from 'authfn/core/sessions';
3
+ import { resolveEnvironment } from 'authfn/core/environment';
4
+ import { signInWithPassword, signUpWithPassword } from 'authfn/core/passwords';
5
+ import { jsonSuccess } from 'authfn/http/envelopes';
6
+ import { completeResetPassword, sendOtpChallenge } from 'authfn/core/verifications';
7
+ import { emitAuthEvent, eventRequestId } from 'authfn/core/observability';
8
+ import { emitAccountLinkedEvent } from 'authfn/core/account-linking';
9
+ import { buildSessionResponse } from 'authfn/core/session-responses';
10
+ import { beginTwoFactorChallenge, createPendingTwoFactorResponse, getTwoFactorPluginConfig } from 'authfn/core/two-factor';
11
+ import { readPluginRuntimeConfig } from 'authfn/core/plugin-runtime';
12
+ export function authFnPasswordPlugin(config = {}) {
13
+ return {
14
+ name: 'password',
15
+ schema: () => config.schema ?? createPasswordSchema(),
16
+ routes: (ctx) => createPasswordRoutes(ctx, config)
17
+ };
18
+ }
19
+ function createPasswordSchema() {
20
+ return [
21
+ {
22
+ modelName: 'password_credentials',
23
+ fields: {
24
+ id: { type: 'string', required: true, fieldName: 'id' },
25
+ userId: { type: 'string', required: true, fieldName: 'user_id' },
26
+ passwordHash: { type: 'string', required: true, fieldName: 'password_hash' },
27
+ createdAt: { type: 'date', required: true, fieldName: 'created_at' },
28
+ updatedAt: { type: 'date', required: true, fieldName: 'updated_at' }
29
+ },
30
+ indexes: [
31
+ {
32
+ name: 'idx_authfn_password_credentials_user_id',
33
+ fields: ['userId'],
34
+ unique: true
35
+ }
36
+ ]
37
+ }
38
+ ];
39
+ }
40
+ function createPasswordRoutes(ctx, config) {
41
+ const runtimeConfig = readPluginRuntimeConfig(ctx, 'password');
42
+ const otpConfig = {
43
+ ...config.otp,
44
+ ...runtimeConfig.otp
45
+ };
46
+ return [
47
+ {
48
+ method: 'POST',
49
+ path: '/sign-up/password',
50
+ meta: createAuthFnRouteMeta('signUpWithPassword', 'Create a user and session using email/password', {
51
+ mode: 'none'
52
+ }),
53
+ handler: async (request) => {
54
+ const body = await readOptionalJson(request);
55
+ const runtime = await resolveEnvironment(ctx.config, request);
56
+ const authenticatedSession = await authenticateRequest(ctx.config, request);
57
+ const result = await signUpWithPassword(ctx.config, ctx.hooks, {
58
+ email: body.email ?? '',
59
+ password: body.password ?? '',
60
+ profile: body.profile
61
+ }, {
62
+ request,
63
+ environment: runtime,
64
+ authenticatedSession,
65
+ policy: {
66
+ compromisedPasswordChecker: config.compromisedPasswordChecker
67
+ }
68
+ });
69
+ if (result.linkedExistingUser) {
70
+ await emitAccountLinkedEvent(ctx.config, {
71
+ request,
72
+ user: result.user,
73
+ method: 'password',
74
+ regionId: runtime.regionId
75
+ });
76
+ }
77
+ else {
78
+ await emitAuthEvent(ctx.config, {
79
+ type: 'authfn.user.created',
80
+ requestId: eventRequestId(request),
81
+ actorId: result.user.id,
82
+ userId: result.user.id,
83
+ regionId: runtime.regionId,
84
+ outcome: 'created',
85
+ metadata: {
86
+ email: result.user.primaryEmail
87
+ }
88
+ });
89
+ }
90
+ const issued = await issueSession(ctx.config, ctx.hooks, {
91
+ request,
92
+ userId: result.user.id,
93
+ primaryEmail: result.user.primaryEmail,
94
+ methods: ['password']
95
+ });
96
+ const response = buildSessionResponse(issued, body.sessionMode);
97
+ return jsonSuccess(request, response.data, {
98
+ setCookies: response.setCookies
99
+ });
100
+ }
101
+ },
102
+ {
103
+ method: 'POST',
104
+ path: '/password/reset/start',
105
+ meta: createAuthFnRouteMeta('startPasswordReset', 'Start an OTP-backed password reset flow', {
106
+ mode: 'none'
107
+ }),
108
+ handler: async (request) => {
109
+ const body = await readOptionalJson(request);
110
+ const result = await sendOtpChallenge(ctx.config, ctx.hooks, otpConfig, {
111
+ request,
112
+ purpose: 'reset-password',
113
+ email: body.email ?? ''
114
+ });
115
+ return jsonSuccess(request, {
116
+ challengeId: result.challenge.id,
117
+ sent: result.delivery.sent
118
+ });
119
+ }
120
+ },
121
+ {
122
+ method: 'POST',
123
+ path: '/password/reset/complete',
124
+ meta: createAuthFnRouteMeta('completePasswordReset', 'Complete a password reset with a valid OTP challenge', {
125
+ mode: 'none'
126
+ }),
127
+ handler: async (request) => {
128
+ const body = await request.json();
129
+ const result = await completeResetPassword(ctx.config, {
130
+ ...otpConfig,
131
+ passwordPolicy: {
132
+ compromisedPasswordChecker: config.compromisedPasswordChecker
133
+ }
134
+ }, {
135
+ purpose: 'reset-password',
136
+ email: body.email ?? '',
137
+ code: body.code ?? '',
138
+ newPassword: body.newPassword ?? '',
139
+ request
140
+ });
141
+ return jsonSuccess(request, result);
142
+ }
143
+ },
144
+ {
145
+ method: 'POST',
146
+ path: '/sign-in/password',
147
+ meta: createAuthFnRouteMeta('signInWithPassword', 'Sign in and issue a session using email/password', {
148
+ mode: 'none'
149
+ }),
150
+ handler: async (request) => {
151
+ const body = await request.json();
152
+ const result = await signInWithPassword(ctx.config, {
153
+ email: body.email ?? '',
154
+ password: body.password ?? ''
155
+ }, {
156
+ requireEmailVerifiedForSignIn: config.requireEmailVerifiedForSignIn
157
+ });
158
+ const twoFactorConfig = getTwoFactorPluginConfig(ctx.config);
159
+ if (twoFactorConfig) {
160
+ const challenge = await beginTwoFactorChallenge(ctx.config, result.user, 'password', twoFactorConfig);
161
+ if (challenge) {
162
+ await emitAuthEvent(ctx.config, {
163
+ type: 'authfn.2fa.challenged',
164
+ requestId: eventRequestId(request),
165
+ actorId: result.user.id,
166
+ userId: result.user.id,
167
+ outcome: 'required',
168
+ metadata: {
169
+ challengeId: challenge.challenge.id,
170
+ primaryMethod: 'password'
171
+ }
172
+ });
173
+ throw createPendingTwoFactorResponse(challenge.challenge);
174
+ }
175
+ }
176
+ const issued = await issueSession(ctx.config, ctx.hooks, {
177
+ request,
178
+ userId: result.user.id,
179
+ primaryEmail: result.user.primaryEmail,
180
+ methods: ['password']
181
+ });
182
+ const response = buildSessionResponse(issued, body.sessionMode);
183
+ return jsonSuccess(request, response.data, {
184
+ setCookies: response.setCookies
185
+ });
186
+ }
187
+ }
188
+ ];
189
+ }
190
+ //# 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,mBAAmB,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC/E,OAAO,EAAE,WAAW,EAAE,M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AM,GAAG,MAAM,YAAY,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,EAAE;oBACvD,OAAO;oBACP,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE;oBACtB,YAAY,EAAE,MAAM,CAAC,IAAI,CAAC,YAAY;oBACtC,OAAO,EAAE,CAAC,UAAU,CAAC;iBACtB,CAAC,CAAC;gBACH,MAAM,QAAQ,GAAG,oBAAoB,CAAC,MAAM,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;gBAEhE,OAAO,WAAW,CAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,EAAE;oBACzC,UAAU,EAAE,QAAQ,CAAC,UAAU;iBAChC,CAAC,CAAC;YACL,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@authfn/password",
3
+ "version": "0.1.1",
4
+ "description": "Password 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
+ }