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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Paired with contracts/hook-matcher-input.test.ts through the shared native dispatcher.

#![expect(
    clippy::panic_in_result_fn,
    reason = "Contract assertions compare native error identities, callback order, and complete storage state."
)]

use super::*;
use crate::plugins::{
    api_key::{ApiKeyEndpoint, ApiKeyGetter, ApiKeyPlugin, CreateKeyRequest},
    test_helpers,
};
use async_trait::async_trait;
use better_auth_core::{
    ApiErrorClass, AuthPlugin, AuthRoute, BeforeRequestAction, FieldMap, FieldValue, HttpMethod,
    endpoint_dispatch::EndpointDispatcher,
    endpoint_input::ValidatedBody,
    field_value::{FieldObject, FieldObjectSource, FieldPropertyDescriptor},
    observability::{AfterEndpointHook, EndpointHooks, LogArgument, LogLevel, LogSink},
    store::{EphemeralStore, StatelessSchema as S, schema::EntityRole},
};
use std::sync::Mutex;

const MATCHER_MESSAGE: &str =
    "An error occurred during hook matcher execution. Check the logs for more details.";

struct LoggedError {
    level: LogLevel,
    raw: Option<FieldValue>,
    type_error: bool,
}

#[derive(Default)]
struct Trace {
    events: Mutex<Vec<String>>,
    logs: Mutex<Vec<LoggedError>>,
    source: &'static str,
}

fn lock<T>(value: &Mutex<T>) -> AuthResult<std::sync::MutexGuard<'_, T>> {
    value
        .lock()
        .map_err(|_| AuthError::internal("Matcher contract trace lock poisoned"))
}

impl LogSink for Trace {
    #[expect(
        clippy::expect_used,
        reason = "The synchronous log callback must fail immediately when the original error or trace lock is unavailable."
    )]
    fn log(
        &self,
        level: LogLevel,
        message: LogArgument<'_>,
        arguments: &[LogArgument<'_>],
    ) -> better_auth_core::AuthResult<()> {
        if message.to_string()
            != format!(
                "An error occurred during {} hook matcher execution:",
                self.source
            )
        {
            return Ok(());
        }
        assert_eq!(arguments.len(), 1);
        let error = match arguments.first() {
            Some(LogArgument::Error(error)) => error.downcast_ref::<AuthError>(),
            _ => None,
        }
        .expect("Matcher logs must retain the original AuthError");
        self.logs
            .lock()
            .expect("Matcher log lock is available")
            .push(LoggedError {
                level,
                raw: error.raw_value().cloned(),
                type_error: matches!(error, AuthError::TypeError(_)),
            });
        Ok(())
    }
}

struct BodySource {
    case: &'static str,
    trace: Arc<Trace>,
    sentinel: FieldValue,
}

impl FieldObjectSource for BodySource {
    fn own_keys(&self) -> AuthResult<Vec<String>> {
        Ok(if self.case == "unread" {
            vec!["unread".into()]
        } else {
            Vec::new()
        })
    }

    fn own_property_descriptor(&self, name: &str) -> AuthResult<Option<FieldPropertyDescriptor>> {
        Ok((self.case == "unread" && name == "unread")
            .then_some(FieldPropertyDescriptor { enumerable: true }))
    }

    fn has_property(&self, name: &str) -> AuthResult<bool> {
        if self.case == "unread" {
            return Ok(name == "unread");
        }
        lock(&self.trace.events)?.push(format!("has:{name}"));
        if self.case == "has-throw" {
            return Err(AuthError::from_thrown(self.sentinel.clone()));
        }
        Ok(true)
    }

    fn get(&self, name: &str) -> AuthResult<Option<FieldValue>> {
        if self.case == "unread" {
            if name == "unread" {
                return Err(AuthError::from_thrown(self.sentinel.clone()));
            }
            return Ok(None);
        }
        lock(&self.trace.events)?.push(format!("get:{name}"));
        Err(AuthError::from_thrown(self.sentinel.clone()))
    }

    fn set(&self, _: String, _: FieldValue) -> AuthResult<Option<FieldValue>> {
        Err(AuthError::type_error("The matcher input is read-only"))
    }

    fn delete(&self, _: &str) -> AuthResult<Option<FieldValue>> {
        Err(AuthError::type_error("The matcher input is read-only"))
    }

    fn snapshot_fields(&self) -> AuthResult<FieldMap> {
        self.own_keys()?
            .into_iter()
            .map(|name| Ok((name.clone(), self.get(&name)?.unwrap_or_default())))
            .collect()
    }

    fn identity(&self) -> usize {
        std::ptr::from_ref(self).addr()
    }
}

#[derive(Clone)]
struct Following {
    trace: Arc<Trace>,
    body: FieldValue,
    blocker: FieldValue,
}

#[async_trait]
impl AuthPlugin<S> for Following {
    fn name(&self) -> &'static str {
        "following-hook"
    }
    fn routes(&self) -> Vec<AuthRoute> {
        Vec::new()
    }

    async fn before_request(
        &self,
        request: &AuthRequest,
        _: &AuthContext<S>,
    ) -> AuthResult<Option<BeforeRequestAction>> {
        if request.path() == "/update-user" {
            lock(&self.trace.events)?.push("following".into());
            assert!(request.input_field_value()?.strict_equals(&self.body));
            return Err(AuthError::from_thrown(self.blocker.clone()));
        }
        Ok(None)
    }

    async fn on_request(
        &self,
        _: &AuthRequest,
        _: &AuthContext<S>,
    ) -> AuthResult<Option<AuthResponse>> {
        Ok(None)
    }
}

#[async_trait]
impl AfterEndpointHook<S> for Following {
    async fn after(
        &self,
        _: &AuthRequest,
        _: &mut AuthResponse,
        _: &AuthContext<S>,
    ) -> AuthResult<()> {
        lock(&self.trace.events)?.push("after".into());
        Ok(())
    }
}

fn sentinel(message: &str) -> AuthResult<FieldValue> {
    AuthError::from_status(
        "CONFLICT",
        FieldMap::from([("message".into(), message.into())]).into(),
    )
    .raw_value()
    .cloned()
    .ok_or_else(|| AuthError::internal("Expected the native matcher sentinel"))
}

fn assert_wrapper(error: &AuthError) -> AuthResult<()> {
    assert!(error.is_api_error_instance(ApiErrorClass::BetterAuth));
    assert_eq!(error.status_value()?, "INTERNAL_SERVER_ERROR".into());
    assert_eq!(error.status_code()?, 500);
    assert_eq!(
        error.body_value()?.enumerable_fields()?,
        FieldMap::from([("message".into(), MATCHER_MESSAGE.into())]),
    );
    Ok(())
}

#[tokio::test]
async fn native_phone_matcher_retains_input_presence_and_getter_order() -> AuthResult<()> {
    for case in [
        "missing",
        "null",
        "false",
        "number",
        "string",
        "array",
        "empty",
        "phone-null",
        "phone-undefined",
        "phone-value",
        "unread",
        "has-throw",
        "get-throw",
    ] {
        let trace = Arc::new(Trace {
            source: "plugin:phone-number",
            ..Default::default()
        });
        let blocker = sentinel("following hook")?;
        let sentinel = sentinel("matcher sentinel")?;
        let body = match case {
            "missing" => FieldValue::Undefined,
            "null" => FieldValue::Null,
            "false" => false.into(),
            "number" => 7.into(),
            "string" => "text".into(),
            "array" => Vec::<FieldValue>::new().into(),
            "empty" => FieldMap::new().into(),
            "phone-null" => FieldMap::from([("phoneNumber".into(), FieldValue::Null)]).into(),
            "phone-undefined" => {
                FieldMap::from([("phoneNumber".into(), FieldValue::Undefined)]).into()
            }
            "phone-value" => FieldMap::from([("phoneNumber".into(), "+15550000001".into())]).into(),
            _ => FieldObject::from_source(Arc::new(BodySource {
                case,
                trace: trace.clone(),
                sentinel: sentinel.clone(),
            }))
            .into(),
        };
        let following = Following {
            trace: trace.clone(),
            body: body.clone(),
            blocker,
        };
        let phone = PhoneNumberPlugin::new();
        let mut config = test_helpers::create_test_config();
        {
            let better_auth_core::LoggerSource::Config(logger_options) = &mut config.logger else {
                return Err(AuthError::config(
                    "Fixture requires authored logger options",
                ));
            };
            logger_options.level = Some(LogLevel::Debug);
            logger_options.log = Some(trace.clone());
        }
        let config = Arc::new(config);
        let store = Arc::new(EphemeralStore::new(config.clone()));
        let context =
            test_helpers::initialize_test_context(config, store.clone(), &[&phone, &following])
                .await?;
        let route = AuthRoute::post("/update-user", "updateUser");
        let dispatcher = EndpointDispatcher::new(
            Arc::new(vec![
                Box::new(phone) as Box<dyn AuthPlugin<S>>,
                Box::new(following.clone()),
            ]),
            EndpointHooks {
                after: Some(Arc::new(following.clone())),
                ..Default::default()
            },
            [route.clone()],
        );
        let mut request = AuthRequest::new(HttpMethod::Post, "/update-user");
        request.set_endpoint_body(ValidatedBody::unvalidated_native(body));
        let error = dispatcher
            .native(request, route, &context, |_, _| async {
                Err(AuthError::internal(
                    "The matcher contract must stop before its handler",
                ))
            })
            .await
            .err()
            .ok_or_else(|| AuthError::internal("Expected a matcher or following-hook failure"))?;
        let actual = error
            .raw_value()
            .ok_or_else(|| AuthError::internal("Expected a native endpoint error"))?;
        let logs = lock(&trace.logs)?;
        let expected_events = if matches!(
            case,
            "missing" | "null" | "false" | "number" | "string" | "has-throw" | "get-throw"
        ) {
            assert_wrapper(&error)?;
            assert!(!actual.strict_equals(&sentinel));
            assert_eq!(logs.len(), 1, "{case}");
            let logged = logs
                .first()
                .ok_or_else(|| AuthError::internal("Expected the original matcher error log"))?;
            assert_eq!(logged.level, LogLevel::Error);
            if matches!(case, "has-throw" | "get-throw") {
                assert!(
                    logged
                        .raw
                        .as_ref()
                        .is_some_and(|value| value.strict_equals(&sentinel))
                );
            } else {
                assert!(logged.type_error);
            }
            match case {
                "has-throw" => vec!["has:phoneNumber"],
                "get-throw" => vec!["has:phoneNumber", "get:phoneNumber"],
                _ => Vec::new(),
            }
        } else if matches!(case, "phone-undefined" | "phone-value") {
            test_helpers::assert_coded_api_error(
                &error,
                "BAD_REQUEST",
                400,
                "PHONE_NUMBER_CANNOT_BE_UPDATED",
                "Phone number cannot be updated",
            );
            assert!(logs.is_empty());
            Vec::new()
        } else {
            assert!(actual.strict_equals(&following.blocker));
            assert!(logs.is_empty());
            vec!["following"]
        };
        assert_eq!(*lock(&trace.events)?, expected_events, "{case}");
        for role in [
            EntityRole::User,
            EntityRole::Session,
            EntityRole::Account,
            EntityRole::Verification,
        ] {
            assert!(store.storage_rows(role)?.is_empty(), "{case}: {role:?}");
        }
    }
    Ok(())
}

struct FailingGetter {
    failure_call: usize,
    calls: Mutex<usize>,
    sentinel: FieldValue,
}

impl ApiKeyGetter for FailingGetter {
    fn get(&self, _: ApiKeyEndpoint<'_>) -> AuthResult<Option<String>> {
        let mut calls = lock(&self.calls)?;
        *calls += 1;
        if *calls == self.failure_call {
            return Err(AuthError::from_thrown(self.sentinel.clone()));
        }
        Ok(Some("x".repeat(64)))
    }
}

#[tokio::test]
async fn api_key_wraps_only_the_matcher_getter_for_native_and_typed_calls() -> AuthResult<()> {
    for typed in [false, true] {
        for failure_call in [1, 2] {
            let trace = Arc::new(Trace {
                source: "plugin:api-key",
                ..Default::default()
            });
            let getter = Arc::new(FailingGetter {
                failure_call,
                calls: Mutex::new(0),
                sentinel: sentinel("key getter failure")?,
            });
            let plugin = ApiKeyPlugin::builder()
                .enable_session_for_api_keys(true)
                .custom_api_key_getter(getter.clone())
                .build();
            let mut config = test_helpers::create_test_config();
            {
                let better_auth_core::LoggerSource::Config(logger_options) = &mut config.logger
                else {
                    return Err(AuthError::config(
                        "Fixture requires authored logger options",
                    ));
                };
                logger_options.level = Some(LogLevel::Debug);
                logger_options.log = Some(trace.clone());
            }
            let config = Arc::new(config);
            let store = Arc::new(EphemeralStore::new(config.clone()));
            let context =
                test_helpers::initialize_test_context(config, store.clone(), &[&plugin]).await?;
            let result = if typed {
                plugin
                    .create_key(
                        &context,
                        &CreateKeyRequest {
                            user_id: Some("owner".into()),
                            ..Default::default()
                        },
                    )
                    .await
                    .map(|_| ())
            } else {
                let route = AuthRoute::post("/sign-in/anonymous", "signInAnonymous");
                let dispatcher = EndpointDispatcher::new(
                    Arc::new(vec![Box::new(plugin) as Box<dyn AuthPlugin<S>>]),
                    EndpointHooks::default(),
                    [route.clone()],
                );
                dispatcher
                    .native(
                        AuthRequest::new(HttpMethod::Post, "/sign-in/anonymous"),
                        route,
                        &context,
                        |_, _| async {
                            Err(AuthError::internal(
                                "The API Key getter must stop before its handler",
                            ))
                        },
                    )
                    .await
                    .map(|_| ())
            };
            let error = result
                .err()
                .ok_or_else(|| AuthError::internal("Expected the API Key getter failure"))?;
            assert_eq!(*lock(&getter.calls)?, failure_call);
            let actual = error
                .raw_value()
                .ok_or_else(|| AuthError::internal("Expected the native getter error"))?;
            let logs = lock(&trace.logs)?;
            if failure_call == 1 {
                assert_wrapper(&error)?;
                assert!(!actual.strict_equals(&getter.sentinel));
                assert_eq!(logs.len(), 1);
                let logged = logs
                    .first()
                    .ok_or_else(|| AuthError::internal("Expected the matcher error log"))?;
                assert_eq!(logged.level, LogLevel::Error);
                assert!(
                    logged
                        .raw
                        .as_ref()
                        .is_some_and(|value| value.strict_equals(&getter.sentinel))
                );
            } else {
                assert!(actual.strict_equals(&getter.sentinel));
                assert!(logs.is_empty());
            }
            for role in [
                EntityRole::User,
                EntityRole::Session,
                EntityRole::Account,
                EntityRole::Verification,
                EntityRole::ApiKey,
            ] {
                assert!(store.storage_rows(role)?.is_empty());
            }
        }
    }
    Ok(())
}