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
use async_trait::async_trait;
use serde::Deserialize;

#[cfg(test)]
use better_auth_core::config::AuthConfig;
use better_auth_core::session::NativeSessionData;
use better_auth_core::store::JoinValue;
use better_auth_core::wire::SessionView;
use better_auth_core::{AuthContext, AuthPlugin, AuthRoute};

use better_auth_core::{AuthError, AuthResult};
use better_auth_core::{AuthRequest, AuthResponse, FieldMap, FieldValue, HttpMethod};

use super::StatusResponse;
use super::helpers::admin_plugin_enabled;
use better_auth_core::SuccessResponse;

/// Session management plugin for handling session operations
pub struct SessionManagementPlugin {
    config: SessionManagementConfig,
}

#[derive(Debug, Clone, better_auth_core::PluginConfig)]
#[plugin(name = "SessionManagementPlugin")]
pub struct SessionManagementConfig {
    #[config(default = true)]
    pub enable_session_listing: bool,
    #[config(default = true)]
    pub enable_session_revocation: bool,
    #[config(default = true)]
    pub require_authentication: bool,
}

// Request structures for session endpoints
#[derive(Debug, Clone, Deserialize)]
struct RevokeSessionRequest {
    token: String,
}

fn revoke_session_body(
    req: &AuthRequest,
) -> AuthResult<better_auth_core::endpoint_input::ValidatedBody> {
    let (typed, projection) =
        super::json_body::string_input::<RevokeSessionRequest>(req, &[("token", true)])?;
    Ok(better_auth_core::endpoint_input::ValidatedBody::new(
        Some(projection),
        typed,
    ))
}

#[async_trait]
impl<S: better_auth_core::AuthSchema> AuthPlugin<S> for SessionManagementPlugin {
    fn name(&self) -> &'static str {
        "session-management"
    }

    fn telemetry_plugin_id(&self) -> Option<&'static str> {
        None
    }

    fn routes(&self) -> Vec<AuthRoute> {
        vec![
            AuthRoute::get("/get-session", "getSession")
                .require_headers(true)
                .query_validator_native(better_auth_core::query::session_query_native),
            // Upstream declares `/get-session` as `method: ["GET", "POST"]`;
            // the POST form requires `session.defer_session_refresh`.
            AuthRoute::post("/get-session", "getSession")
                .require_headers(true)
                .query_validator_native(better_auth_core::query::session_query_native),
            AuthRoute::post("/sign-out", "signOut")
                .require_headers(true)
                .body_validator(super::json_body::sign_out_body),
            AuthRoute::post("/update-session", "updateSession")
                .body_validator(better_auth_core::endpoint_input::record_body),
            AuthRoute::get("/list-sessions", "listUserSessions").require_headers(true),
            AuthRoute::post("/revoke-session", "revokeSession")
                .require_headers(true)
                .body_validator(revoke_session_body),
            AuthRoute::post("/revoke-sessions", "revokeSessions").require_headers(true),
            AuthRoute::post("/revoke-other-sessions", "revokeOtherSessions").require_headers(true),
        ]
    }

    async fn on_request(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<S>,
    ) -> AuthResult<Option<AuthResponse>> {
        if req.path() == "/get-session"
            && let Some(custom) = ctx
                .extensions
                .get::<super::custom_session::CustomSessionPlugin<S>>()
        {
            return custom.on_request(req, ctx).await;
        }
        if req.path() == "/get-session" {
            req.append_response_header("Cache-Control", "no-store".into())?;
            req.append_response_header("Pragma", "no-cache".into())?;
        }
        match (req.method(), req.path()) {
            (HttpMethod::Get, "/get-session") => Ok(Some(self.handle_get_session(req, ctx).await?)),
            (HttpMethod::Post, "/get-session") => {
                if !ctx.config.session.defer_session_refresh {
                    return Err(AuthError::from_code(
                        "METHOD_NOT_ALLOWED",
                        "METHOD_NOT_ALLOWED_DEFER_SESSION_REQUIRED",
                        "POST method requires deferSessionRefresh to be enabled in session config",
                    ));
                }
                Ok(Some(self.handle_get_session(req, ctx).await?))
            }
            (HttpMethod::Post, "/update-session") => {
                Ok(Some(super::session_update::handle(req, ctx).await?))
            }
            (HttpMethod::Post, "/sign-out") => {
                Ok(Some(super::oauth::handle_sign_out(req, ctx).await?))
            }
            (HttpMethod::Get, "/list-sessions") if self.config.enable_session_listing => {
                Ok(Some(self.handle_list_sessions(req, ctx).await?))
            }
            (HttpMethod::Post, "/revoke-session") if self.config.enable_session_revocation => {
                Ok(Some(self.handle_revoke_session(req, ctx).await?))
            }
            (HttpMethod::Post, "/revoke-sessions") if self.config.enable_session_revocation => {
                Ok(Some(self.handle_revoke_sessions(req, ctx).await?))
            }
            (HttpMethod::Post, "/revoke-other-sessions")
                if self.config.enable_session_revocation =>
            {
                Ok(Some(self.handle_revoke_other_sessions(req, ctx).await?))
            }
            _ => Ok(None),
        }
    }
}

// ---------------------------------------------------------------------------
// Core functions — framework-agnostic business logic
// ---------------------------------------------------------------------------

fn session_user_id(data: &NativeSessionData) -> AuthResult<FieldValue> {
    let user = data.user_value()?;
    let nullish = match &user {
        FieldValue::Null => "null",
        FieldValue::Undefined => "undefined",
        _ => return user.model_property("id"),
    };
    Err(AuthError::TypeError(format!(
        "{nullish} is not an object (evaluating 'ctx.context.session.user.id')"
    )))
}

pub(crate) async fn list_sessions_core(
    user_id: &FieldValue,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<Vec<SessionView>> {
    let mut sessions = Vec::new();
    for (session, cached) in ctx
        .database
        .get_user_session_snapshots_value(user_id, true)
        .await?
    {
        let mut session = if let Some(mut cached) = cached {
            cached.filter_returned_fields(&ctx.config.session)?;
            cached
        } else {
            session
        };
        if !session.expires_at.is_after(chrono::Utc::now())? {
            continue;
        }
        session.filter_returned_fields(&ctx.config.session)?;
        sessions.push(session);
    }
    Ok(sessions)
}

pub(crate) async fn revoke_session_core(
    data: &NativeSessionData,
    token: &str,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<StatusResponse> {
    let target = match ctx.database.get_session_snapshot(token).await? {
        Some((_, Some(snapshot))) => match snapshot.user {
            JoinValue::One(None) => None,
            _ => Some(snapshot.session),
        },
        Some((session, None)) => ctx
            .database
            .get_user_by_id_field(&session.user_id)
            .await?
            .map(|_| session),
        None => None,
    };
    let target_user = match target {
        Some(mut session) => {
            session.filter_returned_fields(&ctx.config.session)?;
            session.user_id.field_value()
        }
        None => FieldValue::Undefined,
    };
    if target_user.strict_equals(&session_user_id(data)?) {
        ctx.database
            .delete_session(token)
            .await
            .or_else(revocation_error)?;
    }
    Ok(StatusResponse { status: true })
}

pub(crate) async fn revoke_sessions_core(
    data: &NativeSessionData,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<StatusResponse> {
    let result = async {
        ctx.database
            .delete_user_sessions_by_user_value(&session_user_id(data)?)
            .await
    }
    .await;
    result.or_else(revocation_error)?;
    Ok(StatusResponse { status: true })
}

pub(crate) async fn revoke_other_sessions_core(
    data: &NativeSessionData,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<StatusResponse> {
    if !data.user_value()?.is_truthy() {
        return Err(AuthError::Unauthenticated);
    }
    let mut tokens = Vec::new();
    for (session, cached) in ctx
        .database
        .get_user_session_snapshots_value(&session_user_id(data)?, false)
        .await?
    {
        let session = if let Some(mut cached) = cached {
            cached.filter_returned_fields(&ctx.config.session)?;
            cached
        } else {
            session
        };
        if session.expires_at.is_after(chrono::Utc::now())?
            && !session
                .token
                .field_value()
                .strict_equals(&data.session_property("token")?)
        {
            tokens.push(session.token.field_value());
        }
    }
    let results = futures_util::future::join_all(
        tokens
            .iter()
            .map(|token| ctx.database.delete_session_by_token_value(token)),
    )
    .await;
    for result in results {
        result?;
    }
    Ok(StatusResponse { status: true })
}

fn revocation_error(error: AuthError) -> AuthResult<()> {
    better_auth_core::observability::logger::current().error(
        "Failed to revoke Sessions",
        &[better_auth_core::observability::LogArgument::Error(&error)],
    )?;
    Err(AuthError::from_code(
        "INTERNAL_SERVER_ERROR",
        "INTERNAL_SERVER_ERROR",
        "Internal Server Error",
    ))
}

// ---------------------------------------------------------------------------
// Old handler methods — delegate to core functions
// ---------------------------------------------------------------------------

impl SessionManagementPlugin {
    pub(crate) async fn handle_get_session(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl better_auth_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let resolved = ctx
            .session_manager()
            .resolve_native_for_endpoint(req, better_auth_core::session::SessionRead::Cached)
            .await?;
        let mut response = match resolved.data {
            Some(data) => {
                let value = if let Some(needs_refresh) = resolved.needs_refresh {
                    let mut fields = data.field_value().enumerable_fields()?;
                    let _ = fields.insert("needsRefresh".into(), needs_refresh.into());
                    fields.into()
                } else {
                    data.field_value()
                };
                AuthResponse::native(None, value)
            }
            None => AuthResponse::native(None, FieldValue::Null),
        };
        let _ = response.headers.insert("Cache-Control", "no-store");
        let _ = response.headers.insert("Pragma", "no-cache");
        for (name, value) in req.take_response_headers()? {
            response.headers.append(name, value);
        }
        Ok(response)
    }

    async fn handle_list_sessions(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl better_auth_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let data = ctx.require_native_session(req).await?;
        if !super::helpers::session_is_fresh(&data, &ctx.config)? {
            return Err(AuthError::from_code(
                "FORBIDDEN",
                "SESSION_NOT_FRESH",
                "Session is not fresh",
            ));
        }
        let result = async { list_sessions_core(&session_user_id(&data)?, ctx).await }.await;
        let mut sessions = result.or_else(|error| {
            better_auth_core::observability::logger::current().error(
                "Failed to list Sessions",
                &[better_auth_core::observability::LogArgument::Error(&error)],
            )?;
            Err(AuthError::try_api_error(better_auth_core::ApiErrorInit {
                status: "INTERNAL_SERVER_ERROR".into(),
                class: better_auth_core::ApiErrorClass::BetterCall,
                ..Default::default()
            })?)
        })?;
        if admin_plugin_enabled(ctx) {
            sessions.retain(|session| !session.impersonated_by.field_value().is_truthy());
        }
        Ok(AuthResponse::native(
            None,
            sessions
                .into_iter()
                .map(|session| FieldMap::from(session).into())
                .collect::<Vec<FieldValue>>()
                .into(),
        ))
    }

    async fn handle_revoke_session(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl better_auth_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let data = ctx.require_authoritative_native_session(req).await?;

        let revoke_req: RevokeSessionRequest = match req.validated_body::<RevokeSessionRequest>() {
            Some(body) => body.clone(),
            None => super::json_body::string_input(req, &[("token", true)])?.0,
        };

        let response = revoke_session_core(&data, &revoke_req.token, ctx).await?;
        AuthResponse::json(None, &response)
    }

    async fn handle_revoke_sessions(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl better_auth_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let data = ctx.require_authoritative_native_session(req).await?;
        let response = revoke_sessions_core(&data, ctx).await?;
        AuthResponse::json(None, &response)
    }

    async fn handle_revoke_other_sessions(
        &self,
        req: &AuthRequest,
        ctx: &AuthContext<impl better_auth_core::AuthSchema>,
    ) -> AuthResult<AuthResponse> {
        let data = ctx.require_authoritative_native_session(req).await?;
        let response = revoke_other_sessions_core(&data, ctx).await?;
        AuthResponse::json(None, &response)
    }
}

#[cfg(test)]
fn related_cookie_name(config: &AuthConfig, suffix: &str) -> String {
    better_auth_core::utils::cookie_utils::related_cookie_name(config, suffix)
}

/// Clear the local session before a provider-specific logout redirect is applied.
pub(crate) async fn handle_sign_out(
    req: &AuthRequest,
    ctx: &AuthContext<impl better_auth_core::AuthSchema>,
) -> AuthResult<AuthResponse> {
    if let Some(token) = ctx.session_manager().extract_session_token(req)
        && let Err(error) = ctx.database.delete_session(&token).await
    {
        // Upstream completes browser logout even when deleting the stored session fails.
        better_auth_core::observability::logger::current().error(
            "Failed to delete session during sign-out",
            &[better_auth_core::observability::LogArgument::Error(&error)],
        )?;
    }
    ctx.session_manager().clear_cookies(req)?;
    let mut response = AuthResponse::json(None, &SuccessResponse { success: true })?;
    for (name, value) in req.take_response_headers()? {
        response.headers.append(name, value);
    }
    Ok(response)
}

#[cfg(test)]
mod native_contract_tests;
#[cfg(test)]
mod native_query_tests;
#[cfg(test)]
mod tests;