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
use async_trait::async_trait;
use serde::Serialize;
use crate::user_management::{
AuthenticateWithSessionCookieError, AuthenticateWithSessionCookieResponse, UserManagement,
};
/// The parameters for [`AuthenticateWithSessionCookie`].
#[derive(Debug, Serialize)]
pub struct AuthenticateWithSessionCookieOptions<'a> {
/// WorkOS session cookie value from the user's browser.
pub session_data: &'a str,
/// Password used to unseal the session cookie.
///
/// Must be the same as the password used to seal the cookie.
pub cookie_password: &'a str,
}
/// [WorkOS Docs: Authenticate with session cookie](https://workos.com/docs/reference/user-management/authentication/session-cookie)
#[async_trait]
pub trait AuthenticateWithSessionCookie {
/// Authenticates a user using an AuthKit session cookie.
///
/// [WorkOS Docs: Authenticate with session cookie](https://workos.com/docs/reference/user-management/authentication/session-cookie)
///
/// # Examples
///
/// ```
/// # use workos::user_management::*;
/// use workos::{ApiKey, WorkOs};
///
/// # async fn run() -> Result<(), AuthenticateWithSessionCookieError> {
/// let workos = WorkOs::new(&ApiKey::from("sk_example_123456789"));
///
/// let AuthenticateWithSessionCookieResponse { user, .. } = workos
/// .user_management()
/// .authenticate_with_session_cookie(&AuthenticateWithSessionCookieOptions {
/// session_data: "sealed_session_cookie_data",
/// cookie_password: "password_previously_used_to_seal_session_cookie",
/// })
/// .await?;
/// # Ok(())
/// # }
/// ```
async fn authenticate_with_session_cookie(
&self,
options: &AuthenticateWithSessionCookieOptions<'_>,
) -> Result<AuthenticateWithSessionCookieResponse, AuthenticateWithSessionCookieError>;
}
#[async_trait]
impl AuthenticateWithSessionCookie for UserManagement<'_> {
async fn authenticate_with_session_cookie(
&self,
options: &AuthenticateWithSessionCookieOptions<'_>,
) -> Result<AuthenticateWithSessionCookieResponse, AuthenticateWithSessionCookieError> {
let session = self.load_sealed_session(options.session_data, options.cookie_password);
session.authenticate().await
}
}