@draftlab/auth 0.0.3 → 0.0.4
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/dist/allow.d.ts +58 -1
- package/dist/allow.js +61 -2
- package/dist/client.d.ts +2 -3
- package/dist/client.js +2 -2
- package/dist/core.d.ts +128 -8
- package/dist/core.js +496 -12
- package/dist/error.d.ts +242 -1
- package/dist/error.js +235 -1
- package/dist/index.d.ts +1 -8
- package/dist/index.js +1 -12
- package/dist/keys.d.ts +1 -1
- package/dist/keys.js +138 -3
- package/dist/pkce.js +160 -1
- package/dist/provider/code.d.ts +211 -3
- package/dist/provider/code.js +1 -1
- package/dist/provider/facebook.d.ts +2 -3
- package/dist/provider/facebook.js +1 -5
- package/dist/provider/github.d.ts +2 -3
- package/dist/provider/github.js +1 -5
- package/dist/provider/google.d.ts +2 -3
- package/dist/provider/google.js +1 -5
- package/dist/provider/oauth2.d.ts +175 -3
- package/dist/provider/oauth2.js +153 -5
- package/dist/provider/password.d.ts +384 -3
- package/dist/provider/password.js +4 -4
- package/dist/provider/provider.d.ts +226 -2
- package/dist/random.js +85 -1
- package/dist/storage/memory.d.ts +1 -1
- package/dist/storage/memory.js +1 -1
- package/dist/storage/storage.d.ts +161 -1
- package/dist/storage/storage.js +60 -1
- package/dist/storage/turso.d.ts +1 -1
- package/dist/storage/turso.js +1 -1
- package/dist/storage/unstorage.d.ts +1 -1
- package/dist/storage/unstorage.js +1 -1
- package/dist/subject.d.ts +61 -2
- package/dist/themes/theme.d.ts +208 -1
- package/dist/themes/theme.js +118 -1
- package/dist/ui/base.js +420 -2
- package/dist/ui/code.d.ts +1 -3
- package/dist/ui/code.js +3 -4
- package/dist/ui/form.js +59 -1
- package/dist/ui/icon.js +190 -1
- package/dist/ui/password.d.ts +1 -3
- package/dist/ui/password.js +2 -3
- package/dist/ui/select.js +278 -4
- package/dist/util.d.ts +71 -1
- package/dist/util.js +106 -1
- package/package.json +2 -2
- package/dist/allow-CixonwTW.d.ts +0 -59
- package/dist/allow-DX5cehSc.js +0 -63
- package/dist/base-DRutbxgL.js +0 -422
- package/dist/code-DJxdFR7p.d.ts +0 -212
- package/dist/core-BZHEAefX.d.ts +0 -129
- package/dist/core-CDM5o4rs.js +0 -498
- package/dist/error-CWAdNAzm.d.ts +0 -243
- package/dist/error-DgAKK7b2.js +0 -237
- package/dist/form-6XKM_cOk.js +0 -61
- package/dist/icon-Ci5uqGB_.js +0 -192
- package/dist/keys-EEfxEGfO.js +0 -140
- package/dist/oauth2-B7-6Z7Lc.js +0 -155
- package/dist/oauth2-CXHukHf2.d.ts +0 -176
- package/dist/password-C4KLmO0O.d.ts +0 -385
- package/dist/pkce-276Za_rZ.js +0 -162
- package/dist/provider-tndlqCzp.d.ts +0 -227
- package/dist/random-SXMYlaVr.js +0 -87
- package/dist/select-BjySLL8I.js +0 -280
- package/dist/storage-BEaqEPNQ.js +0 -62
- package/dist/storage-CxKerLlc.d.ts +0 -162
- package/dist/subject-DMIMVtaT.d.ts +0 -62
- package/dist/theme-C9by7VXf.d.ts +0 -209
- package/dist/theme-CswaLtbW.js +0 -120
- package/dist/util-CSdHUFOo.js +0 -108
- package/dist/util-DbSKG1Xm.d.ts +0 -72
package/dist/provider/oauth2.js
CHANGED
|
@@ -1,7 +1,155 @@
|
|
|
1
|
-
import "../util
|
|
2
|
-
import "../error
|
|
3
|
-
import "../pkce
|
|
4
|
-
import "../random
|
|
5
|
-
import { Oauth2Provider } from "../oauth2-B7-6Z7Lc.js";
|
|
1
|
+
import { getRelativeUrl } from "../util.js";
|
|
2
|
+
import { OauthError } from "../error.js";
|
|
3
|
+
import { generatePKCE } from "../pkce.js";
|
|
4
|
+
import { generateSecureToken, timingSafeCompare } from "../random.js";
|
|
6
5
|
|
|
6
|
+
//#region src/provider/oauth2.ts
|
|
7
|
+
/**
|
|
8
|
+
* Creates an OAuth 2.0 authentication provider.
|
|
9
|
+
* Implements the Authorization Code Grant flow with optional PKCE support.
|
|
10
|
+
*
|
|
11
|
+
* @param config - OAuth 2.0 provider configuration
|
|
12
|
+
* @returns Provider instance implementing OAuth 2.0 authentication
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* // GitHub provider with basic configuration
|
|
17
|
+
* const githubProvider = Oauth2Provider({
|
|
18
|
+
* clientID: process.env.GITHUB_CLIENT_ID,
|
|
19
|
+
* clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
|
20
|
+
* endpoint: {
|
|
21
|
+
* authorization: "https://github.com/login/oauth/authorize",
|
|
22
|
+
* token: "https://github.com/login/oauth/access_token"
|
|
23
|
+
* },
|
|
24
|
+
* scopes: ["user:email", "read:user"]
|
|
25
|
+
* })
|
|
26
|
+
*
|
|
27
|
+
* // Provider with PKCE and custom parameters
|
|
28
|
+
* const customProvider = Oauth2Provider({
|
|
29
|
+
* clientID: "my-client-id",
|
|
30
|
+
* clientSecret: "my-client-secret",
|
|
31
|
+
* endpoint: {
|
|
32
|
+
* authorization: "https://provider.com/oauth/authorize",
|
|
33
|
+
* token: "https://provider.com/oauth/token",
|
|
34
|
+
* jwks: "https://provider.com/.well-known/jwks.json"
|
|
35
|
+
* },
|
|
36
|
+
* scopes: ["read", "write"],
|
|
37
|
+
* pkce: true,
|
|
38
|
+
* query: {
|
|
39
|
+
* prompt: "consent",
|
|
40
|
+
* access_type: "offline"
|
|
41
|
+
* }
|
|
42
|
+
* })
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
const Oauth2Provider = (config) => {
|
|
46
|
+
const authQuery = config.query || {};
|
|
47
|
+
/**
|
|
48
|
+
* Handles the OAuth 2.0 callback logic for both GET and POST requests.
|
|
49
|
+
* Exchanges the authorization code for tokens and processes the response.
|
|
50
|
+
*/
|
|
51
|
+
const handleCallbackLogic = async (c, ctx, provider, code) => {
|
|
52
|
+
if (!(provider && code)) return c.redirect(getRelativeUrl(c, "./authorize"));
|
|
53
|
+
const tokenRequestBody = new URLSearchParams({
|
|
54
|
+
client_id: config.clientID,
|
|
55
|
+
client_secret: config.clientSecret,
|
|
56
|
+
code,
|
|
57
|
+
grant_type: "authorization_code",
|
|
58
|
+
redirect_uri: provider.redirect,
|
|
59
|
+
...provider.codeVerifier ? { code_verifier: provider.codeVerifier } : {}
|
|
60
|
+
});
|
|
61
|
+
try {
|
|
62
|
+
const response = await fetch(config.endpoint.token, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: {
|
|
65
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
66
|
+
Accept: "application/json"
|
|
67
|
+
},
|
|
68
|
+
body: tokenRequestBody.toString()
|
|
69
|
+
});
|
|
70
|
+
if (!response.ok) throw new Error(`Token request failed with status ${response.status}`);
|
|
71
|
+
const tokenData = await response.json();
|
|
72
|
+
if (tokenData.error) throw new OauthError(tokenData.error, tokenData.error_description || "");
|
|
73
|
+
return await ctx.success(c, {
|
|
74
|
+
clientID: config.clientID,
|
|
75
|
+
tokenset: {
|
|
76
|
+
get access() {
|
|
77
|
+
return tokenData.access_token;
|
|
78
|
+
},
|
|
79
|
+
get refresh() {
|
|
80
|
+
return tokenData.refresh_token || "";
|
|
81
|
+
},
|
|
82
|
+
get expiry() {
|
|
83
|
+
return tokenData.expires_in || 0;
|
|
84
|
+
},
|
|
85
|
+
get raw() {
|
|
86
|
+
return tokenData;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof OauthError) throw error;
|
|
92
|
+
throw new OauthError("server_error", `Token exchange failed: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
type: config.type || "oauth2",
|
|
97
|
+
init(routes, ctx) {
|
|
98
|
+
/**
|
|
99
|
+
* Initiates OAuth 2.0 authorization flow.
|
|
100
|
+
* Redirects user to the provider's authorization endpoint with proper parameters.
|
|
101
|
+
*/
|
|
102
|
+
routes.get("/authorize", async (c) => {
|
|
103
|
+
const state = generateSecureToken();
|
|
104
|
+
const pkce = config.pkce ? await generatePKCE() : void 0;
|
|
105
|
+
await ctx.set(c, "provider", 60 * 10, {
|
|
106
|
+
state,
|
|
107
|
+
redirect: getRelativeUrl(c, "./callback"),
|
|
108
|
+
codeVerifier: pkce?.verifier
|
|
109
|
+
});
|
|
110
|
+
const authorizationUrl = new URL(config.endpoint.authorization);
|
|
111
|
+
authorizationUrl.searchParams.set("client_id", config.clientID);
|
|
112
|
+
authorizationUrl.searchParams.set("redirect_uri", getRelativeUrl(c, "./callback"));
|
|
113
|
+
authorizationUrl.searchParams.set("response_type", "code");
|
|
114
|
+
authorizationUrl.searchParams.set("state", state);
|
|
115
|
+
authorizationUrl.searchParams.set("scope", config.scopes.join(" "));
|
|
116
|
+
if (pkce) {
|
|
117
|
+
authorizationUrl.searchParams.set("code_challenge", pkce.challenge);
|
|
118
|
+
authorizationUrl.searchParams.set("code_challenge_method", pkce.method);
|
|
119
|
+
}
|
|
120
|
+
for (const [key, value] of Object.entries(authQuery)) authorizationUrl.searchParams.set(key, value);
|
|
121
|
+
return c.redirect(authorizationUrl.toString());
|
|
122
|
+
});
|
|
123
|
+
/**
|
|
124
|
+
* Handles OAuth 2.0 callback via query parameters (GET request).
|
|
125
|
+
* Standard OAuth 2.0 callback method for most providers.
|
|
126
|
+
*/
|
|
127
|
+
routes.get("/callback", async (c) => {
|
|
128
|
+
const provider = await ctx.get(c, "provider");
|
|
129
|
+
const code = c.query("code");
|
|
130
|
+
const state = c.query("state");
|
|
131
|
+
const error = c.query("error");
|
|
132
|
+
if (error) throw new OauthError(error, c.query("error_description") || "");
|
|
133
|
+
if (!(provider && code) || provider.state && !timingSafeCompare(state || "", provider.state)) return c.redirect(getRelativeUrl(c, "./authorize"));
|
|
134
|
+
return await handleCallbackLogic(c, ctx, provider, code);
|
|
135
|
+
});
|
|
136
|
+
/**
|
|
137
|
+
* Handles OAuth 2.0 callback via form data (POST request).
|
|
138
|
+
* Alternative callback method supported by some providers.
|
|
139
|
+
*/
|
|
140
|
+
routes.post("/callback", async (c) => {
|
|
141
|
+
const provider = await ctx.get(c, "provider");
|
|
142
|
+
const formData = await c.formData();
|
|
143
|
+
const code = formData.get("code")?.toString();
|
|
144
|
+
const state = formData.get("state")?.toString();
|
|
145
|
+
const error = formData.get("error")?.toString();
|
|
146
|
+
if (error) throw new OauthError(error, formData.get("error_description")?.toString() || "");
|
|
147
|
+
if (!(provider && code) || provider.state && !timingSafeCompare(state || "", provider.state)) return c.redirect(getRelativeUrl(c, "./authorize"));
|
|
148
|
+
return await handleCallbackLogic(c, ctx, provider, code);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
//#endregion
|
|
7
155
|
export { Oauth2Provider };
|
|
@@ -1,4 +1,385 @@
|
|
|
1
|
-
import "
|
|
2
|
-
import "
|
|
3
|
-
|
|
1
|
+
import { Provider } from "./provider.js";
|
|
2
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
|
|
4
|
+
//#region src/provider/password.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Password-based authentication provider for Draft Auth.
|
|
8
|
+
* Supports user registration, login, and password changes with email verification.
|
|
9
|
+
*
|
|
10
|
+
* ## Quick Setup
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { PasswordUI } from "@draftauth/core/ui/password"
|
|
14
|
+
* import { PasswordProvider } from "@draftauth/core/provider/password"
|
|
15
|
+
*
|
|
16
|
+
* export default issuer({
|
|
17
|
+
* providers: {
|
|
18
|
+
* password: PasswordProvider(
|
|
19
|
+
* PasswordUI({
|
|
20
|
+
* copy: {
|
|
21
|
+
* error_email_taken: "This email is already taken."
|
|
22
|
+
* },
|
|
23
|
+
* sendCode: async (email, code) => {
|
|
24
|
+
* await sendEmail(email, `Your verification code: ${code}`)
|
|
25
|
+
* }
|
|
26
|
+
* })
|
|
27
|
+
* )
|
|
28
|
+
* }
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* ## Custom UI Implementation
|
|
33
|
+
*
|
|
34
|
+
* For full control over the user interface, implement the handlers directly:
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* PasswordProvider({
|
|
38
|
+
* login: async (req, form, error) => {
|
|
39
|
+
* return new Response(renderLoginPage(form, error))
|
|
40
|
+
* },
|
|
41
|
+
* register: async (req, state, form, error) => {
|
|
42
|
+
* return new Response(renderRegisterPage(state, form, error))
|
|
43
|
+
* },
|
|
44
|
+
* change: async (req, state, form, error) => {
|
|
45
|
+
* return new Response(renderChangePage(state, form, error))
|
|
46
|
+
* },
|
|
47
|
+
* sendCode: async (email, code) => {
|
|
48
|
+
* await yourEmailService.send(email, code)
|
|
49
|
+
* }
|
|
50
|
+
* })
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* ## Features
|
|
54
|
+
*
|
|
55
|
+
* - **Email verification**: Secure registration with email confirmation codes
|
|
56
|
+
* - **Password hashing**: Built-in Scrypt and PBKDF2 support with secure defaults
|
|
57
|
+
* - **Password validation**: Configurable password strength requirements
|
|
58
|
+
* - **Password reset**: Secure password change flow with email verification
|
|
59
|
+
* - **Session management**: Automatic invalidation on password changes
|
|
60
|
+
*
|
|
61
|
+
* @packageDocumentation
|
|
62
|
+
*/
|
|
63
|
+
/**
|
|
64
|
+
* Password hashing interface for secure password storage.
|
|
65
|
+
* Implement this interface to use custom password hashing algorithms.
|
|
66
|
+
*
|
|
67
|
+
* @template T - The hash storage format (usually an object with hash, salt, and params)
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
interface PasswordHasher<T> {
|
|
71
|
+
/**
|
|
72
|
+
* Hashes a plaintext password for secure storage.
|
|
73
|
+
*
|
|
74
|
+
* @param password - The plaintext password to hash
|
|
75
|
+
* @returns Promise resolving to the hash data structure
|
|
76
|
+
*/
|
|
77
|
+
hash(password: string): Promise<T>;
|
|
78
|
+
/**
|
|
79
|
+
* Verifies a plaintext password against a stored hash.
|
|
80
|
+
*
|
|
81
|
+
* @param password - The plaintext password to verify
|
|
82
|
+
* @param compare - The stored hash data to compare against
|
|
83
|
+
* @returns Promise resolving to true if password matches
|
|
84
|
+
*/
|
|
85
|
+
verify(password: string, compare: T): Promise<boolean>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Configuration for the password authentication provider.
|
|
89
|
+
*/
|
|
90
|
+
interface PasswordConfig {
|
|
91
|
+
/**
|
|
92
|
+
* Length of verification codes sent to users.
|
|
93
|
+
* @internal
|
|
94
|
+
* @default 6
|
|
95
|
+
*/
|
|
96
|
+
readonly length?: number;
|
|
97
|
+
/**
|
|
98
|
+
* Password hashing implementation to use.
|
|
99
|
+
* @internal
|
|
100
|
+
* @default ScryptHasher()
|
|
101
|
+
*/
|
|
102
|
+
readonly hasher?: PasswordHasher<unknown>;
|
|
103
|
+
/**
|
|
104
|
+
* Request handler for rendering the login screen.
|
|
105
|
+
* Receives the request, optional form data, and any login errors.
|
|
106
|
+
*
|
|
107
|
+
* @param req - The HTTP request object
|
|
108
|
+
* @param form - Form data from POST requests (if any)
|
|
109
|
+
* @param error - Login error to display (if any)
|
|
110
|
+
* @returns Promise resolving to the login page response
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* login: async (req, form, error) => {
|
|
115
|
+
* const html = renderLoginPage({
|
|
116
|
+
* email: form?.get('email'),
|
|
117
|
+
* error: error?.type
|
|
118
|
+
* })
|
|
119
|
+
* return new Response(html, {
|
|
120
|
+
* headers: { 'Content-Type': 'text/html' }
|
|
121
|
+
* })
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
login: (req: Request, form?: FormData, error?: PasswordLoginError) => Promise<Response>;
|
|
126
|
+
/**
|
|
127
|
+
* Request handler for rendering the registration screen.
|
|
128
|
+
* Handles both initial registration form and email verification.
|
|
129
|
+
*
|
|
130
|
+
* @param req - The HTTP request object
|
|
131
|
+
* @param state - Current registration state (start or code verification)
|
|
132
|
+
* @param form - Form data from POST requests (if any)
|
|
133
|
+
* @param error - Registration error to display (if any)
|
|
134
|
+
* @returns Promise resolving to the registration page response
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* register: async (req, state, form, error) => {
|
|
139
|
+
* if (state.type === 'start') {
|
|
140
|
+
* return new Response(renderRegistrationForm(error))
|
|
141
|
+
* } else {
|
|
142
|
+
* return new Response(renderCodeVerification(state.email, error))
|
|
143
|
+
* }
|
|
144
|
+
* }
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
register: (req: Request, state: PasswordRegisterState, form?: FormData, error?: PasswordRegisterError) => Promise<Response>;
|
|
148
|
+
/**
|
|
149
|
+
* Request handler for rendering the password change screen.
|
|
150
|
+
* Handles email entry, code verification, and password update steps.
|
|
151
|
+
*
|
|
152
|
+
* @param req - The HTTP request object
|
|
153
|
+
* @param state - Current password change state
|
|
154
|
+
* @param form - Form data from POST requests (if any)
|
|
155
|
+
* @param error - Password change error to display (if any)
|
|
156
|
+
* @returns Promise resolving to the password change page response
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* change: async (req, state, form, error) => {
|
|
161
|
+
* switch (state.type) {
|
|
162
|
+
* case 'start':
|
|
163
|
+
* return new Response(renderEmailForm(error))
|
|
164
|
+
* case 'code':
|
|
165
|
+
* return new Response(renderCodeForm(state.email, error))
|
|
166
|
+
* case 'update':
|
|
167
|
+
* return new Response(renderPasswordForm(error))
|
|
168
|
+
* }
|
|
169
|
+
* }
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
change: (req: Request, state: PasswordChangeState, form?: FormData, error?: PasswordChangeError) => Promise<Response>;
|
|
173
|
+
/**
|
|
174
|
+
* Callback for sending verification codes to users via email.
|
|
175
|
+
* Implement this to integrate with your email service provider.
|
|
176
|
+
*
|
|
177
|
+
* @param email - The recipient's email address
|
|
178
|
+
* @param code - The verification code to send
|
|
179
|
+
* @returns Promise that resolves when email is sent
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* sendCode: async (email, code) => {
|
|
184
|
+
* await emailService.send({
|
|
185
|
+
* to: email,
|
|
186
|
+
* subject: 'Your verification code',
|
|
187
|
+
* text: `Your verification code is: ${code}`
|
|
188
|
+
* })
|
|
189
|
+
* }
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
sendCode: (email: string, code: string) => Promise<void>;
|
|
193
|
+
/**
|
|
194
|
+
* Optional password validation function or schema.
|
|
195
|
+
* Can be either a validation function or a standard-schema validator.
|
|
196
|
+
*
|
|
197
|
+
* @param password - The password to validate
|
|
198
|
+
* @returns Error message if invalid, undefined if valid
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* // Function-based validation
|
|
203
|
+
* validatePassword: (password) => {
|
|
204
|
+
* if (password.length < 8) return "Password must be at least 8 characters"
|
|
205
|
+
* if (!/[A-Z]/.test(password)) return "Password must contain uppercase letter"
|
|
206
|
+
* return undefined
|
|
207
|
+
* }
|
|
208
|
+
*
|
|
209
|
+
* // Schema-based validation
|
|
210
|
+
* validatePassword: pipe(
|
|
211
|
+
* string(),
|
|
212
|
+
* minLength(8, "Password must be at least 8 characters"),
|
|
213
|
+
* regex(/[A-Z]/, "Password must contain uppercase letter")
|
|
214
|
+
* )
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
readonly validatePassword?: StandardSchemaV1 | ((password: string) => Promise<string | undefined> | string | undefined);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Registration flow states that determine which UI to show.
|
|
221
|
+
* The registration process moves through these states sequentially.
|
|
222
|
+
*/
|
|
223
|
+
type PasswordRegisterState = {
|
|
224
|
+
/** Initial state: user enters email and password */
|
|
225
|
+
readonly type: "start";
|
|
226
|
+
} | {
|
|
227
|
+
/** Code verification state: user enters emailed verification code */
|
|
228
|
+
readonly type: "code";
|
|
229
|
+
/** The verification code sent to the user */
|
|
230
|
+
readonly code: string;
|
|
231
|
+
/** The user's email address */
|
|
232
|
+
readonly email: string;
|
|
233
|
+
/** The hashed password (ready for storage) */
|
|
234
|
+
readonly password: unknown;
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* Possible errors during user registration.
|
|
238
|
+
*/
|
|
239
|
+
type PasswordRegisterError = {
|
|
240
|
+
/** The verification code entered is incorrect */
|
|
241
|
+
readonly type: "invalid_code";
|
|
242
|
+
} | {
|
|
243
|
+
/** The email address is already registered */
|
|
244
|
+
readonly type: "email_taken";
|
|
245
|
+
} | {
|
|
246
|
+
/** The email address format is invalid */
|
|
247
|
+
readonly type: "invalid_email";
|
|
248
|
+
} | {
|
|
249
|
+
/** The password does not meet requirements */
|
|
250
|
+
readonly type: "invalid_password";
|
|
251
|
+
} | {
|
|
252
|
+
/** Password and confirmation password don't match */
|
|
253
|
+
readonly type: "password_mismatch";
|
|
254
|
+
} | {
|
|
255
|
+
/** Custom validation error from validatePassword callback */
|
|
256
|
+
readonly type: "validation_error";
|
|
257
|
+
readonly message?: string;
|
|
258
|
+
};
|
|
259
|
+
/**
|
|
260
|
+
* Password change flow states that determine which UI to show.
|
|
261
|
+
*/
|
|
262
|
+
type PasswordChangeState = {
|
|
263
|
+
/** Initial state: user enters their email address */
|
|
264
|
+
readonly type: "start";
|
|
265
|
+
/** URL to redirect to after successful password change */
|
|
266
|
+
readonly redirect: string;
|
|
267
|
+
} | {
|
|
268
|
+
/** Code verification state: user enters emailed verification code */
|
|
269
|
+
readonly type: "code";
|
|
270
|
+
/** The verification code sent to the user */
|
|
271
|
+
readonly code: string;
|
|
272
|
+
/** The user's email address */
|
|
273
|
+
readonly email: string;
|
|
274
|
+
/** URL to redirect to after completion */
|
|
275
|
+
readonly redirect: string;
|
|
276
|
+
} | {
|
|
277
|
+
/** Password update state: user enters new password */
|
|
278
|
+
readonly type: "update";
|
|
279
|
+
/** URL to redirect to after completion */
|
|
280
|
+
readonly redirect: string;
|
|
281
|
+
/** The verified email address */
|
|
282
|
+
readonly email: string;
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* Possible errors during password changes.
|
|
286
|
+
*/
|
|
287
|
+
type PasswordChangeError = {
|
|
288
|
+
/** The email address format is invalid */
|
|
289
|
+
readonly type: "invalid_email";
|
|
290
|
+
} | {
|
|
291
|
+
/** The verification code entered is incorrect */
|
|
292
|
+
readonly type: "invalid_code";
|
|
293
|
+
} | {
|
|
294
|
+
/** The new password does not meet requirements */
|
|
295
|
+
readonly type: "invalid_password";
|
|
296
|
+
} | {
|
|
297
|
+
/** New password and confirmation don't match */
|
|
298
|
+
readonly type: "password_mismatch";
|
|
299
|
+
} | {
|
|
300
|
+
/** Custom validation error from validatePassword callback */
|
|
301
|
+
readonly type: "validation_error";
|
|
302
|
+
readonly message: string;
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* Possible errors during login attempts.
|
|
306
|
+
*/
|
|
307
|
+
type PasswordLoginError = {
|
|
308
|
+
/** The email address format is invalid */
|
|
309
|
+
readonly type: "invalid_email";
|
|
310
|
+
} | {
|
|
311
|
+
/** The password is incorrect or email not found */
|
|
312
|
+
readonly type: "invalid_password";
|
|
313
|
+
};
|
|
314
|
+
/**
|
|
315
|
+
* User data returned by successful password authentication.
|
|
316
|
+
*/
|
|
317
|
+
interface PasswordUserData {
|
|
318
|
+
/** The authenticated user's email address */
|
|
319
|
+
readonly email: string;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Creates a password authentication provider with email verification.
|
|
323
|
+
* Implements secure registration, login, and password change flows.
|
|
324
|
+
*
|
|
325
|
+
* @param config - Provider configuration including UI handlers and email service
|
|
326
|
+
* @returns Provider instance implementing password authentication
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```ts
|
|
330
|
+
* const provider = PasswordProvider({
|
|
331
|
+
* login: async (req, form, error) => {
|
|
332
|
+
* return new Response(renderLogin(form, error))
|
|
333
|
+
* },
|
|
334
|
+
* register: async (req, state, form, error) => {
|
|
335
|
+
* return new Response(renderRegister(state, form, error))
|
|
336
|
+
* },
|
|
337
|
+
* change: async (req, state, form, error) => {
|
|
338
|
+
* return new Response(renderChange(state, form, error))
|
|
339
|
+
* },
|
|
340
|
+
* sendCode: async (email, code) => {
|
|
341
|
+
* await emailService.send(email, `Code: ${code}`)
|
|
342
|
+
* },
|
|
343
|
+
* validatePassword: (pwd) => {
|
|
344
|
+
* return pwd.length >= 8 ? undefined : "Too short"
|
|
345
|
+
* }
|
|
346
|
+
* })
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
declare const PasswordProvider: (config: PasswordConfig) => Provider<PasswordUserData>;
|
|
350
|
+
/**
|
|
351
|
+
* PBKDF2 password hasher with configurable iterations.
|
|
352
|
+
* Good choice for compatibility but slower than Scrypt.
|
|
353
|
+
*
|
|
354
|
+
* @param opts - Configuration options
|
|
355
|
+
* @returns Password hasher using PBKDF2 algorithm
|
|
356
|
+
* @internal
|
|
357
|
+
*/
|
|
358
|
+
declare const PBKDF2Hasher: (opts?: {
|
|
359
|
+
iterations?: number;
|
|
360
|
+
}) => PasswordHasher<{
|
|
361
|
+
hash: string;
|
|
362
|
+
salt: string;
|
|
363
|
+
iterations: number;
|
|
364
|
+
}>;
|
|
365
|
+
/**
|
|
366
|
+
* Scrypt password hasher with secure defaults.
|
|
367
|
+
* Recommended choice for new applications due to memory-hard properties.
|
|
368
|
+
*
|
|
369
|
+
* @param opts - Scrypt parameters (N, r, p)
|
|
370
|
+
* @returns Password hasher using Scrypt algorithm
|
|
371
|
+
* @internal
|
|
372
|
+
*/
|
|
373
|
+
declare const ScryptHasher: (opts?: {
|
|
374
|
+
N?: number;
|
|
375
|
+
r?: number;
|
|
376
|
+
p?: number;
|
|
377
|
+
}) => PasswordHasher<{
|
|
378
|
+
hash: string;
|
|
379
|
+
salt: string;
|
|
380
|
+
N: number;
|
|
381
|
+
r: number;
|
|
382
|
+
p: number;
|
|
383
|
+
}>;
|
|
384
|
+
//#endregion
|
|
4
385
|
export { PBKDF2Hasher, PasswordChangeError, PasswordChangeState, PasswordConfig, PasswordHasher, PasswordLoginError, PasswordProvider, PasswordRegisterError, PasswordRegisterState, PasswordUserData, ScryptHasher };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { getRelativeUrl } from "../util
|
|
2
|
-
import { UnknownStateError } from "../error
|
|
3
|
-
import { generateUnbiasedDigits, timingSafeCompare } from "../random
|
|
4
|
-
import { Storage } from "../storage
|
|
1
|
+
import { getRelativeUrl } from "../util.js";
|
|
2
|
+
import { UnknownStateError } from "../error.js";
|
|
3
|
+
import { generateUnbiasedDigits, timingSafeCompare } from "../random.js";
|
|
4
|
+
import { Storage } from "../storage/storage.js";
|
|
5
5
|
import * as jose from "jose";
|
|
6
6
|
import { randomBytes, scrypt, timingSafeEqual } from "node:crypto";
|
|
7
7
|
import { TextEncoder } from "node:util";
|