better-auth-api 1.0.0-alpha.5

Plugin implementations for better-auth
1
2
3
4
5
6
7
8
9
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use chrono::Utc;
use url::Url;
use uuid::Uuid;

use better_auth_core::session::NativeSessionData;
use better_auth_core::utils::password::{self as password_utils};
use better_auth_core::{
    AuthContext, AuthError, AuthResult, AuthUser, CreateAccount, RequestMeta, UpdateAccount,
};

use crate::plugins::helpers::{
    SessionIssueError, get_credential_account, get_credential_password_hash,
    issue_selected_user_session_optional,
};

use super::types::*;
use super::{PasswordManagementConfig, StatusResponse};

const PASSWORD_RESET_SUCCESS_MESSAGE: &str =
    "If this email exists in our system, check your email for the reset link";

// ---------------------------------------------------------------------------
// Core functions (framework-agnostic business logic)
// ---------------------------------------------------------------------------

pub(crate) async fn request_password_reset_core(
    body: &RequestPasswordResetRequest,
    config: &PasswordManagementConfig,
    req: &better_auth_core::AuthRequest,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<RequestPasswordResetResponse> {
    if let Some(redirect_to) = &body.redirect_to {
        validate_redirect_target(redirect_to, ctx, "Invalid redirectURL")?;
    }

    super::callbacks::require_sender(config, ctx)?;

    let success = RequestPasswordResetResponse {
        status: true,
        message: PASSWORD_RESET_SUCCESS_MESSAGE.to_string(),
    };

    let user = match ctx.database.get_user_by_email(&body.email).await? {
        Some(user) => user,
        None => {
            let _ = Uuid::new_v4().simple().to_string();
            let _ = ctx
                .database
                .find_verification_value("dummy-verification-token")
                .await?;
            better_auth_core::observability::logger::current().error(
                "Reset Password: User not found",
                &[better_auth_core::observability::LogArgument::Value(
                    &serde_json::json!(body.email),
                )],
            )?;
            return Ok(success);
        }
    };

    let reset_token = Uuid::new_v4().simple().to_string();
    let expires_at = config.reset_token_expires_at(Utc::now())?;

    let _ = ctx
        .database
        .create_verification_optional(better_auth_core::CreateVerification {
            identifier: (format!("reset-password:{}", reset_token)).into(),
            value: user.id().into_owned(),
            expires_at: (expires_at).into(),
            ..Default::default()
        })
        .await?;

    let callback_url = body
        .redirect_to
        .as_deref()
        .map(urlencoding::encode)
        .unwrap_or_default();
    let reset_url = format!(
        "{}/reset-password/{}?callbackURL={}",
        ctx.base_url(),
        reset_token,
        callback_url
    );

    let mut parsed = serde_json::Map::from_iter([("email".into(), serde_json::json!(body.email))]);
    if let Some(redirect) = &body.redirect_to {
        let _ = parsed.insert("redirectTo".into(), serde_json::json!(redirect));
    }
    let endpoint = crate::plugins::endpoint_context::EndpointContext::new(
        Some(req),
        better_auth_core::FieldValue::from_json(parsed.into())?,
        ctx,
    );
    let task = super::callbacks::delivery(
        config,
        super::PasswordResetEmail {
            user: ctx.internal_user_view(&user).await?,
            url: reset_url,
            token: reset_token,
        },
        &endpoint,
    )?;
    better_auth_core::background::run_or_await(
        task,
        ctx.config.advanced.background_tasks.as_ref(),
        ctx.logger(),
    )
    .await?;

    Ok(success)
}

pub(crate) async fn reset_password_core(
    body: &ResetPasswordRequest,
    req: &better_auth_core::AuthRequest,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<StatusResponse> {
    let token = body.token.as_deref().unwrap_or("");
    if token.is_empty() {
        return Err(AuthError::from_code(
            "BAD_REQUEST",
            "INVALID_TOKEN",
            "Invalid token",
        ));
    }
    password_utils::validate_password(
        &body.new_password,
        ctx.password_policy.min_length,
        ctx.password_policy.max_length,
        ctx,
    )?;

    let verification = ctx
        .database
        .consume_verification_by_identifier(&format!("reset-password:{}", token))
        .await?
        .ok_or_else(|| AuthError::from_code("BAD_REQUEST", "INVALID_TOKEN", "Invalid token"))?;
    let user_id = verification.value.typed()?.clone();

    let user = ctx
        .database
        .get_user_by_id(&user_id)
        .await?
        .ok_or_else(|| AuthError::from_code("BAD_REQUEST", "USER_NOT_FOUND", "User not found"))?;

    let password_hash =
        password_utils::hash_password(ctx.password_policy.hasher.as_ref(), &body.new_password)
            .await?;

    if get_credential_account(ctx, user_id.as_str())
        .await?
        .is_some()
    {
        crate::plugins::helpers::update_password(ctx, &user_id.as_str().into(), password_hash)
            .await?;
    } else {
        let _ = ctx
            .database
            .create_account_optional(CreateAccount {
                user_id: (user_id.clone()).into(),
                account_id: (user_id.clone()).into(),
                provider_id: ("credential".to_string()).into(),
                access_token: Default::default(),
                refresh_token: Default::default(),
                id_token: Default::default(),
                access_token_expires_at: Default::default(),
                refresh_token_expires_at: Default::default(),
                scope: Default::default(),
                password: (Some(password_hash))
                    .map(|value| better_auth_core::SchemaValue::Typed(Some(value)))
                    .unwrap_or_default(),
                ..Default::default()
            })
            .await?;
    }

    if let Some(callback) = &ctx.password_policy.on_password_reset {
        callback(super::PasswordResetEvent {
            user: ctx.internal_user_view(&user).await?,
            request: Some(req.clone()),
        })
        .await?;
    }

    if ctx.password_policy.revoke_sessions_on_password_reset {
        ctx.database.delete_user_sessions(&user_id).await?;
    }

    Ok(StatusResponse { status: true })
}

pub(crate) async fn reset_password_token_core(
    token: &str,
    query: &ResetPasswordTokenQuery,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<ResetPasswordTokenResult> {
    if let Some(callback_url) = &query.callback_url {
        validate_redirect_target(callback_url, ctx, "Invalid callbackURL")?;
    }

    if token.is_empty() || query.callback_url.is_none() {
        return Ok(ResetPasswordTokenResult::Redirect(build_redirect_url(
            &ctx.base_url(),
            query.callback_url.as_deref(),
            &[("error", "INVALID_TOKEN")],
        )?));
    }

    let verification = ctx
        .database
        .find_verification_value(&format!("reset-password:{}", token))
        .await?;

    let expired = match &verification {
        Some(verification) => {
            better_auth_core::SchemaValue::<better_auth_core::FieldDate>::from_field(
                verification.model_property("expiresAt")?,
            )
            .is_before(ctx.now())?
        }
        None => true,
    };
    if expired {
        return Ok(ResetPasswordTokenResult::Redirect(build_redirect_url(
            &ctx.base_url(),
            query.callback_url.as_deref(),
            &[("error", "INVALID_TOKEN")],
        )?));
    }

    Ok(ResetPasswordTokenResult::Redirect(build_redirect_url(
        &ctx.base_url(),
        query.callback_url.as_deref(),
        &[("token", token)],
    )?))
}

/// Change the password before revoking sessions and issuing replacement credentials.
pub(crate) async fn change_password_core(
    body: &ChangePasswordRequest,
    data: &NativeSessionData,
    config: &PasswordManagementConfig,
    req: &better_auth_core::AuthRequest,
    meta: &RequestMeta,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<ChangePasswordResponse> {
    password_utils::validate_password(
        &body.new_password,
        ctx.password_policy.min_length,
        ctx.password_policy.max_length,
        ctx,
    )?;
    ctx.password_policy
        .validate_max_length(&body.current_password)?;
    let credential_account = get_credential_account(
        ctx,
        better_auth_core::SchemaValue::from_field(data.user_property("id")?),
    )
    .await?
    .filter(|account| account.password.field_value().is_truthy())
    .ok_or_else(|| {
        AuthError::from_code(
            "BAD_REQUEST",
            "CREDENTIAL_ACCOUNT_NOT_FOUND",
            "Credential account not found",
        )
    })?;
    let password_hash =
        password_utils::hash_password(ctx.password_policy.hasher.as_ref(), &body.new_password)
            .await?;
    if config.require_current_password {
        password_utils::verify_password_native(
            ctx.password_policy.hasher.as_ref(),
            &body.current_password,
            &credential_account.password.field_value(),
        )
        .await
        .map_err(|error| match error {
            AuthError::InvalidCredentials => {
                AuthError::from_code("BAD_REQUEST", "INVALID_PASSWORD", "Invalid password")
            }
            other => other,
        })?;
    }

    let _ = ctx
        .database
        .update_account_by_id_value(
            &credential_account.id.field_value(),
            UpdateAccount {
                password: (Some(password_hash))
                    .map(|value| better_auth_core::SchemaValue::Typed(Some(value)))
                    .unwrap_or_default(),
                ..Default::default()
            },
        )
        .await?;

    let new_token = if body.revoke_other_sessions == Some(true) {
        ctx.database
            .delete_user_sessions_by_user_value(&data.user_property("id")?)
            .await?;
        let issued = issue_selected_user_session_optional(
            ctx,
            data.user_value()?,
            meta,
            ctx.config.session.expires_in(),
        )
        .await
        .map_err(SessionIssueError::into_auth_error)?
        .ok_or_else(|| {
            AuthError::from_code(
                "INTERNAL_SERVER_ERROR",
                "FAILED_TO_GET_SESSION",
                "Failed to get session",
            )
        })?;
        let token = issued.session_property("token")?;
        ctx.session_manager()
            .set_native_session_cookie(req, issued, None)
            .await?;
        token
    } else {
        better_auth_core::FieldValue::Null
    };

    let response = ChangePasswordResponse {
        token: new_token,
        user: data.public_user(&ctx.config.user)?,
    };

    Ok(response)
}

pub(crate) async fn verify_password_core(
    body: &VerifyPasswordRequest,
    data: &NativeSessionData,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<StatusResponse> {
    ctx.password_policy.validate_max_length(&body.password)?;
    let stored_hash = get_credential_password_hash(
        ctx,
        better_auth_core::SchemaValue::from_field(data.user_property("id")?),
    )
    .await?
    .ok_or_else(|| AuthError::from_code("BAD_REQUEST", "INVALID_PASSWORD", "Invalid password"))?;

    password_utils::verify_password_native(
        ctx.password_policy.hasher.as_ref(),
        &body.password,
        &stored_hash,
    )
    .await
    .map_err(|error| match error {
        AuthError::InvalidCredentials => {
            AuthError::from_code("BAD_REQUEST", "INVALID_PASSWORD", "Invalid password")
        }
        other => other,
    })?;

    Ok(StatusResponse { status: true })
}

fn validate_redirect_target(
    target: &str,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
    error_message: &str,
) -> AuthResult<()> {
    if ctx.config.advanced.disable_origin_check {
        return Ok(());
    }
    if ctx.is_redirect_target_trusted(target) {
        Ok(())
    } else {
        Err(AuthError::forbidden(error_message.to_string()))
    }
}

fn build_redirect_url(
    base_url: &str,
    callback_url: Option<&str>,
    params: &[(&str, &str)],
) -> AuthResult<String> {
    let base = Url::parse(base_url)
        .map_err(|error| AuthError::internal(format!("Invalid base URL: {}", error)))?;
    let mut url = if let Some(callback_url) = callback_url {
        base.join(callback_url).or_else(|error| {
            // Upstream sends the bare message so the response carries the
            // INVALID_CALLBACK_URL code; keep the parse detail in the log.
            better_auth_core::observability::logger::current().warn(
                "Invalid callbackURL",
                &[
                    better_auth_core::observability::LogArgument::Error(&error),
                    better_auth_core::observability::LogArgument::Value(&serde_json::json!(
                        callback_url
                    )),
                ],
            )?;
            Err(AuthError::bad_request("Invalid callbackURL"))
        })?
    } else {
        base.join("/error")
            .map_err(|error| AuthError::internal(format!("Invalid error URL: {}", error)))?
    };

    {
        let mut pairs = url.query_pairs_mut();
        for (key, value) in params {
            let _ = pairs.append_pair(key, value);
        }
    }

    Ok(url.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::plugins::test_helpers;

    // Upstream reference: packages/better-auth/src/api/middlewares/origin-check.ts :: originCheck respects ctx.context.skipOriginCheck.
    #[tokio::test]
    async fn validate_redirect_target_respects_disable_origin_check() {
        let config = test_helpers::create_test_config().disable_origin_check(true);
        let ctx = test_helpers::create_test_context_with_config(config).await;

        assert!(
            validate_redirect_target("https://evil.com/phish", &ctx, "Invalid redirectURL").is_ok()
        );
    }

    // Upstream reference: packages/better-auth/src/api/middlewares/origin-check.ts :: originCheck rejects untrusted origins by default.
    #[tokio::test]
    async fn validate_redirect_target_rejects_untrusted_by_default() {
        let ctx = test_helpers::create_test_context().await;

        assert!(
            validate_redirect_target("https://evil.com/phish", &ctx, "Invalid redirectURL")
                .is_err()
        );
    }
}