Skip to content
This repository was archived by the owner on May 13, 2026. It is now read-only.

Commit e2f928e

Browse files
feat(user-management): authenticate with session cookie
1 parent 6a69a6c commit e2f928e

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/user_management/operations.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod authenticate_with_magic_auth;
66
mod authenticate_with_organization_selection;
77
mod authenticate_with_password;
88
mod authenticate_with_refresh_token;
9+
mod authenticate_with_session_cookie;
910
mod authenticate_with_totp;
1011
mod create_magic_auth;
1112
mod create_organization_membership;
@@ -50,6 +51,7 @@ pub use authenticate_with_magic_auth::*;
5051
pub use authenticate_with_organization_selection::*;
5152
pub use authenticate_with_password::*;
5253
pub use authenticate_with_refresh_token::*;
54+
pub use authenticate_with_session_cookie::*;
5355
pub use authenticate_with_totp::*;
5456
pub use create_magic_auth::*;
5557
pub use create_organization_membership::*;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use async_trait::async_trait;
2+
use serde::Serialize;
3+
4+
use crate::user_management::{
5+
AuthenticateWithSessionCookieError, AuthenticateWithSessionCookieResponse, UserManagement,
6+
};
7+
8+
/// The parameters for [`AuthenticateWithSessionCookie`].
9+
#[derive(Debug, Serialize)]
10+
pub struct AuthenticateWithSessionCookieOptions<'a> {
11+
/// WorkOS session cookie value from the user's browser.
12+
pub session_data: &'a str,
13+
14+
/// Password used to unseal the session cookie.
15+
///
16+
/// Must be the same as the password used to seal the cookie.
17+
pub cookie_password: &'a str,
18+
}
19+
20+
/// [WorkOS Docs: Authenticate with session cookie](https://workos.com/docs/reference/user-management/authentication/session-cookie)
21+
#[async_trait]
22+
pub trait AuthenticateWithSessionCookie {
23+
/// Authenticates a user using an AuthKit session cookie.
24+
///
25+
/// [WorkOS Docs: Authenticate with session cookie](https://workos.com/docs/reference/user-management/authentication/session-cookie)
26+
///
27+
/// # Examples
28+
///
29+
/// ```
30+
/// # use workos::user_management::*;
31+
/// use workos::{ApiKey, WorkOs};
32+
///
33+
/// # async fn run() -> Result<(), AuthenticateWithSessionCookieError> {
34+
/// let workos = WorkOs::new(&ApiKey::from("sk_example_123456789"));
35+
///
36+
/// let AuthenticateWithSessionCookieResponse { user, .. } = workos
37+
/// .user_management()
38+
/// .authenticate_with_session_cookie(&AuthenticateWithSessionCookieOptions {
39+
/// session_data: "sealed_session_cookie_data",
40+
/// cookie_password: "password_previously_used_to_seal_session_cookie",
41+
/// })
42+
/// .await?;
43+
/// # Ok(())
44+
/// # }
45+
/// ```
46+
async fn authenticate_with_session_cookie(
47+
&self,
48+
options: &AuthenticateWithSessionCookieOptions<'_>,
49+
) -> Result<AuthenticateWithSessionCookieResponse, AuthenticateWithSessionCookieError>;
50+
}
51+
52+
#[async_trait]
53+
impl AuthenticateWithSessionCookie for UserManagement<'_> {
54+
async fn authenticate_with_session_cookie(
55+
&self,
56+
options: &AuthenticateWithSessionCookieOptions<'_>,
57+
) -> Result<AuthenticateWithSessionCookieResponse, AuthenticateWithSessionCookieError> {
58+
let session = self.load_sealed_session(options.session_data, options.cookie_password);
59+
60+
session.authenticate().await
61+
}
62+
}

0 commit comments

Comments
 (0)