@draftlab/auth 0.0.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.
- package/dist/adapters/node.d.ts +18 -0
- package/dist/adapters/node.js +71 -0
- package/dist/allow-CixonwTW.d.ts +59 -0
- package/dist/allow-DX5cehSc.js +63 -0
- package/dist/allow.d.ts +2 -0
- package/dist/allow.js +4 -0
- package/dist/base-DRutbxgL.js +422 -0
- package/dist/client.d.ts +413 -0
- package/dist/client.js +209 -0
- package/dist/code-l_uvMR1j.d.ts +212 -0
- package/dist/core-8WTqfnb4.d.ts +129 -0
- package/dist/core-CncE5rPg.js +498 -0
- package/dist/core.d.ts +9 -0
- package/dist/core.js +14 -0
- package/dist/error-CWAdNAzm.d.ts +243 -0
- package/dist/error-DgAKK7b2.js +237 -0
- package/dist/error.d.ts +2 -0
- package/dist/error.js +3 -0
- package/dist/form-6XKM_cOk.js +61 -0
- package/dist/icon-Ci5uqGB_.js +192 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +14 -0
- package/dist/keys-EEfxEGfO.js +140 -0
- package/dist/keys.d.ts +67 -0
- package/dist/keys.js +5 -0
- package/dist/oauth2-B7-6Z7Lc.js +155 -0
- package/dist/oauth2-DtKwtl8p.d.ts +176 -0
- package/dist/password-Cm0dRMwa.d.ts +385 -0
- package/dist/pkce-276Za_rZ.js +162 -0
- package/dist/pkce.d.ts +72 -0
- package/dist/pkce.js +3 -0
- package/dist/provider/code.d.ts +4 -0
- package/dist/provider/code.js +145 -0
- package/dist/provider/facebook.d.ts +137 -0
- package/dist/provider/facebook.js +85 -0
- package/dist/provider/github.d.ts +141 -0
- package/dist/provider/github.js +88 -0
- package/dist/provider/google.d.ts +113 -0
- package/dist/provider/google.js +62 -0
- package/dist/provider/oauth2.d.ts +4 -0
- package/dist/provider/oauth2.js +7 -0
- package/dist/provider/password.d.ts +4 -0
- package/dist/provider/password.js +366 -0
- package/dist/provider/provider.d.ts +3 -0
- package/dist/provider/provider.js +44 -0
- package/dist/provider-CwWMG-1l.d.ts +227 -0
- package/dist/random-SXMYlaVr.js +87 -0
- package/dist/random.d.ts +66 -0
- package/dist/random.js +3 -0
- package/dist/select-BjySLL8I.js +280 -0
- package/dist/storage/memory.d.ts +82 -0
- package/dist/storage/memory.js +127 -0
- package/dist/storage/storage.d.ts +2 -0
- package/dist/storage/storage.js +3 -0
- package/dist/storage/turso.d.ts +31 -0
- package/dist/storage/turso.js +117 -0
- package/dist/storage/unstorage.d.ts +38 -0
- package/dist/storage/unstorage.js +97 -0
- package/dist/storage-BEaqEPNQ.js +62 -0
- package/dist/storage-CxKerLlc.d.ts +162 -0
- package/dist/subject-DiQdRWGt.d.ts +62 -0
- package/dist/subject.d.ts +3 -0
- package/dist/subject.js +36 -0
- package/dist/theme-C9by7VXf.d.ts +209 -0
- package/dist/theme-CswaLtbW.js +120 -0
- package/dist/themes/theme.d.ts +2 -0
- package/dist/themes/theme.js +3 -0
- package/dist/types.d.ts +94 -0
- package/dist/types.js +0 -0
- package/dist/ui/base.d.ts +43 -0
- package/dist/ui/base.js +4 -0
- package/dist/ui/code.d.ts +158 -0
- package/dist/ui/code.js +197 -0
- package/dist/ui/form.d.ts +31 -0
- package/dist/ui/form.js +3 -0
- package/dist/ui/icon.d.ts +98 -0
- package/dist/ui/icon.js +3 -0
- package/dist/ui/password.d.ts +54 -0
- package/dist/ui/password.js +300 -0
- package/dist/ui/select.d.ts +233 -0
- package/dist/ui/select.js +6 -0
- package/dist/util-CSdHUFOo.js +108 -0
- package/dist/util-ChlgVqPN.d.ts +72 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +3 -0
- package/package.json +63 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { Provider } from "./provider-CwWMG-1l.js";
|
|
2
|
+
|
|
3
|
+
//#region src/provider/oauth2.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for the OAuth 2.0 provider.
|
|
7
|
+
*/
|
|
8
|
+
interface Oauth2Config {
|
|
9
|
+
/**
|
|
10
|
+
* Provider type identifier for internal use.
|
|
11
|
+
* @internal
|
|
12
|
+
* @default "oauth2"
|
|
13
|
+
*/
|
|
14
|
+
readonly type?: string;
|
|
15
|
+
/**
|
|
16
|
+
* The client ID registered with the OAuth 2.0 provider.
|
|
17
|
+
* This public identifier is used in authorization requests.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* {
|
|
22
|
+
* clientID: "github-app-12345"
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
readonly clientID: string;
|
|
27
|
+
/**
|
|
28
|
+
* The client secret for authenticating with the OAuth 2.0 provider.
|
|
29
|
+
* This private credential must be kept secure and not exposed to clients.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* {
|
|
34
|
+
* clientSecret: process.env.OAUTH_CLIENT_SECRET
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
readonly clientSecret: string;
|
|
39
|
+
/**
|
|
40
|
+
* OAuth 2.0 endpoint URLs for the authorization and token flows.
|
|
41
|
+
*/
|
|
42
|
+
readonly endpoint: {
|
|
43
|
+
/**
|
|
44
|
+
* The authorization endpoint where users are redirected for authentication.
|
|
45
|
+
*
|
|
46
|
+
* @example "https://github.com/login/oauth/authorize"
|
|
47
|
+
*/
|
|
48
|
+
readonly authorization: string;
|
|
49
|
+
/**
|
|
50
|
+
* The token endpoint for exchanging authorization codes for access tokens.
|
|
51
|
+
*
|
|
52
|
+
* @example "https://github.com/login/oauth/access_token"
|
|
53
|
+
*/
|
|
54
|
+
readonly token: string;
|
|
55
|
+
/**
|
|
56
|
+
* Optional JWKS endpoint for verifying ID tokens.
|
|
57
|
+
* Required only if the provider returns ID tokens that need verification.
|
|
58
|
+
*
|
|
59
|
+
* @example "https://provider.com/.well-known/jwks.json"
|
|
60
|
+
*/
|
|
61
|
+
readonly jwks?: string;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* OAuth 2.0 scopes to request during authorization.
|
|
65
|
+
* Scopes define the level of access being requested.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* {
|
|
70
|
+
* scopes: ["user:email", "read:user", "repo"]
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
readonly scopes: string[];
|
|
75
|
+
/**
|
|
76
|
+
* Whether to use PKCE (Proof Key for Code Exchange) for enhanced security.
|
|
77
|
+
* Recommended for public clients and required by some providers.
|
|
78
|
+
*
|
|
79
|
+
* @default false
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* {
|
|
84
|
+
* pkce: true // Required for Twitter/X, recommended for mobile apps
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
readonly pkce?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Additional query parameters to include in the authorization request.
|
|
91
|
+
* Useful for provider-specific parameters or customizing the auth flow.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* {
|
|
96
|
+
* query: {
|
|
97
|
+
* access_type: "offline", // Request refresh token
|
|
98
|
+
* prompt: "consent", // Force consent screen
|
|
99
|
+
* hd: "mycompany.com" // Google Workspace domain
|
|
100
|
+
* }
|
|
101
|
+
* }
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
readonly query?: Record<string, string>;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* OAuth 2.0 configuration without endpoint-specific fields.
|
|
108
|
+
* Used internally for provider wrapping.
|
|
109
|
+
* @internal
|
|
110
|
+
*/
|
|
111
|
+
type Oauth2WrappedConfig = Omit<Oauth2Config, "endpoint" | "name">;
|
|
112
|
+
/**
|
|
113
|
+
* OAuth 2.0 token response containing access tokens and metadata.
|
|
114
|
+
* Provides a structured interface for token data with lazy property access.
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
interface Oauth2Token {
|
|
118
|
+
/** Access token for making authenticated API requests */
|
|
119
|
+
readonly access: string;
|
|
120
|
+
/** Refresh token for obtaining new access tokens (if provided) */
|
|
121
|
+
readonly refresh: string;
|
|
122
|
+
/** Token expiration time in seconds (if provided) */
|
|
123
|
+
readonly expiry: number;
|
|
124
|
+
/** Raw token response from the provider */
|
|
125
|
+
readonly raw: Record<string, unknown>;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* User data returned by successful OAuth 2.0 authentication.
|
|
129
|
+
*/
|
|
130
|
+
interface Oauth2UserData {
|
|
131
|
+
/** Token set containing access token, refresh token, and metadata */
|
|
132
|
+
readonly tokenset: Oauth2Token;
|
|
133
|
+
/** Client ID used for this authentication */
|
|
134
|
+
readonly clientID: string;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Creates an OAuth 2.0 authentication provider.
|
|
138
|
+
* Implements the Authorization Code Grant flow with optional PKCE support.
|
|
139
|
+
*
|
|
140
|
+
* @param config - OAuth 2.0 provider configuration
|
|
141
|
+
* @returns Provider instance implementing OAuth 2.0 authentication
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* // GitHub provider with basic configuration
|
|
146
|
+
* const githubProvider = Oauth2Provider({
|
|
147
|
+
* clientID: process.env.GITHUB_CLIENT_ID,
|
|
148
|
+
* clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
|
149
|
+
* endpoint: {
|
|
150
|
+
* authorization: "https://github.com/login/oauth/authorize",
|
|
151
|
+
* token: "https://github.com/login/oauth/access_token"
|
|
152
|
+
* },
|
|
153
|
+
* scopes: ["user:email", "read:user"]
|
|
154
|
+
* })
|
|
155
|
+
*
|
|
156
|
+
* // Provider with PKCE and custom parameters
|
|
157
|
+
* const customProvider = Oauth2Provider({
|
|
158
|
+
* clientID: "my-client-id",
|
|
159
|
+
* clientSecret: "my-client-secret",
|
|
160
|
+
* endpoint: {
|
|
161
|
+
* authorization: "https://provider.com/oauth/authorize",
|
|
162
|
+
* token: "https://provider.com/oauth/token",
|
|
163
|
+
* jwks: "https://provider.com/.well-known/jwks.json"
|
|
164
|
+
* },
|
|
165
|
+
* scopes: ["read", "write"],
|
|
166
|
+
* pkce: true,
|
|
167
|
+
* query: {
|
|
168
|
+
* prompt: "consent",
|
|
169
|
+
* access_type: "offline"
|
|
170
|
+
* }
|
|
171
|
+
* })
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
declare const Oauth2Provider: (config: Oauth2Config) => Provider<Oauth2UserData>;
|
|
175
|
+
//#endregion
|
|
176
|
+
export { Oauth2Config, Oauth2Provider, Oauth2Token, Oauth2UserData, Oauth2WrappedConfig };
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import { Provider } from "./provider-CwWMG-1l.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
|
|
385
|
+
export { PBKDF2Hasher, PasswordChangeError, PasswordChangeState, PasswordConfig, PasswordHasher, PasswordLoginError, PasswordProvider, PasswordRegisterError, PasswordRegisterState, PasswordUserData, ScryptHasher };
|