Skip to main content

alibi_core/store/stateless/
sessions.rs

1use super::*;
2#[async_trait]
3impl SessionStore<StatelessSchema> for StatelessStore {
4    async fn prepare_secondary_session_creation(
5        &self,
6        input: CreateSession,
7        persist: bool,
8    ) -> AuthResult<SessionView> {
9        if persist {
10            return Err(AuthError::config("StatelessStore cannot persist sessions"));
11        }
12        SessionStore::<StatelessSchema>::create_session(self, input).await
13    }
14    async fn complete_secondary_session_creation(&self, _session: &SessionView) -> AuthResult<()> {
15        Ok(())
16    }
17    async fn create_session(&self, mut input: CreateSession) -> AuthResult<SessionView> {
18        input
19            .additional_fields
20            .apply_adapter_transforms_async()
21            .await?;
22        let now = Utc::now();
23        Ok(SessionView {
24            id: uuid::Uuid::new_v4().to_string(),
25            token: input
26                .token
27                .unwrap_or_else(crate::utils::sessions::generate_session_token),
28            user_id: input.user_id,
29            expires_at: input.expires_at,
30            created_at: now,
31            updated_at: now,
32            ip_address: input.ip_address.or_else(|| Some(String::new())),
33            user_agent: input.user_agent.or_else(|| Some(String::new())),
34            active_organization_id: input.active_organization_id,
35            active_team_id: input.active_team_id,
36            impersonated_by: input.impersonated_by,
37            extension_fields: input
38                .additional_fields
39                .into_iter()
40                .map(|(key, value)| value.to_json_value().map(|value| (key, value)))
41                .collect::<Result<_, _>>()?,
42            active: true,
43            omitted_fields: Default::default(),
44        })
45    }
46    async fn prepare_secondary_session_update(
47        &self,
48        mut session: SessionView,
49        expires_at: Option<DateTime<Utc>>,
50        mut fields: crate::field_policy::FieldValues,
51    ) -> AuthResult<Option<(SessionView, crate::field_policy::FieldValues)>> {
52        fields.apply_adapter_transforms_async().await?;
53        for (key, value) in &fields {
54            match key.as_str() {
55                "activeOrganizationId" => {
56                    session.active_organization_id = value.as_str().map(str::to_owned)
57                }
58                "activeTeamId" => session.active_team_id = value.as_str().map(str::to_owned),
59                "impersonatedBy" => session.impersonated_by = value.as_str().map(str::to_owned),
60                _ => {
61                    drop(
62                        session
63                            .extension_fields
64                            .insert(key.clone(), value.to_json_value()?),
65                    );
66                }
67            }
68        }
69        if let Some(expires_at) = expires_at {
70            session.expires_at = expires_at;
71        }
72        session.updated_at = Utc::now();
73        Ok(Some((session, fields)))
74    }
75    async fn complete_secondary_session_update(
76        &self,
77        session: SessionView,
78        _expires_at: Option<DateTime<Utc>>,
79        _fields: crate::field_policy::FieldValues,
80        persist: bool,
81    ) -> AuthResult<Option<SessionView>> {
82        if persist {
83            return Err(AuthError::config(
84                "No-database store cannot persist sessions",
85            ));
86        }
87        Ok(Some(session))
88    }
89    async fn get_session(&self, _token: &str) -> AuthResult<Option<SessionView>> {
90        Ok(None)
91    }
92    async fn get_user_sessions(&self, _user: &str) -> AuthResult<Vec<SessionView>> {
93        Ok(Vec::new())
94    }
95    async fn update_session_expiry(
96        &self,
97        _token: &str,
98        _expires: chrono::DateTime<Utc>,
99    ) -> AuthResult<()> {
100        Err(AuthError::SessionNotFound)
101    }
102    async fn update_session_active_organization(
103        &self,
104        _token: &str,
105        _id: Option<&str>,
106    ) -> AuthResult<SessionView> {
107        Err(AuthError::SessionNotFound)
108    }
109    async fn delete_session(&self, _token: &str) -> AuthResult<()> {
110        Ok(())
111    }
112    async fn delete_user_sessions(&self, _user: &str) -> AuthResult<()> {
113        Ok(())
114    }
115    async fn delete_expired_sessions(&self) -> AuthResult<usize> {
116        Ok(0)
117    }
118}