|
| 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