Skip to main content

agentics_persistence/repositories/
sessions.rs

1use sqlx::PgPool;
2
3use crate::db;
4use crate::repositories::{
5    AdminServiceTokenRecord, AuthenticatedAdminServiceToken, AuthenticatedCreatorApiToken,
6    AuthenticatedHumanSession, ConsumedGithubSignInState, CreateAdminServiceTokenInput,
7    CreateCreatorApiTokenInput, CreateGithubSignInStateInput, CreateHumanSessionInput,
8    CreatorApiTokenRecord, DeleteHumanAccountOutcome, HumanRecord, ResolveGithubHumanInput,
9};
10use agentics_domain::models::ids::{AdminServiceTokenId, CreatorApiTokenId, HumanId};
11use agentics_error::Result;
12
13#[derive(Debug, Clone, Copy)]
14pub struct SessionsRepository<'a> {
15    pub(super) pool: &'a PgPool,
16}
17
18impl SessionsRepository<'_> {
19    pub async fn resolve_github_human(
20        &self,
21        input: &ResolveGithubHumanInput,
22    ) -> Result<HumanRecord> {
23        db::sessions::resolve_github_human(self.pool, input).await
24    }
25
26    pub async fn create_github_sign_in_state(
27        &self,
28        input: &CreateGithubSignInStateInput,
29    ) -> Result<()> {
30        db::sessions::create_github_sign_in_state(self.pool, input).await
31    }
32
33    pub async fn consume_github_sign_in_state(
34        &self,
35        state_hash: &str,
36        browser_nonce_hash: &str,
37    ) -> Result<Option<ConsumedGithubSignInState>> {
38        db::sessions::consume_github_sign_in_state(self.pool, state_hash, browser_nonce_hash).await
39    }
40
41    pub async fn create_human_session(&self, input: &CreateHumanSessionInput) -> Result<()> {
42        db::sessions::create_human_session(self.pool, input).await
43    }
44
45    pub async fn authenticate_human(
46        &self,
47        session_token: &str,
48    ) -> Result<Option<AuthenticatedHumanSession>> {
49        db::sessions::authenticate_human_session(self.pool, session_token).await
50    }
51
52    pub async fn complete_human_setup(
53        &self,
54        human_id: &HumanId,
55        code_hash: &str,
56    ) -> Result<HumanRecord> {
57        db::sessions::complete_human_setup(self.pool, human_id, code_hash).await
58    }
59
60    pub async fn delete_human_session_by_token(&self, session_token: &str) -> Result<()> {
61        db::sessions::delete_human_session_by_token(self.pool, session_token).await
62    }
63
64    pub async fn delete_human_account(
65        &self,
66        human_id: &HumanId,
67    ) -> Result<DeleteHumanAccountOutcome> {
68        db::sessions::delete_human_account(self.pool, human_id).await
69    }
70
71    pub async fn list_humans(&self) -> Result<Vec<HumanRecord>> {
72        db::sessions::list_humans(self.pool).await
73    }
74
75    pub async fn get_human_by_id(&self, human_id: &HumanId) -> Result<HumanRecord> {
76        db::sessions::get_human_by_id(self.pool, human_id).await
77    }
78
79    pub async fn grant_admin_role(
80        &self,
81        human_id: &HumanId,
82        granted_by_human_id: &HumanId,
83    ) -> Result<HumanRecord> {
84        db::sessions::grant_admin_role(self.pool, human_id, granted_by_human_id).await
85    }
86
87    pub async fn revoke_admin_role(
88        &self,
89        human_id: &HumanId,
90        revoked_by_human_id: &HumanId,
91    ) -> Result<HumanRecord> {
92        db::sessions::revoke_admin_role(self.pool, human_id, revoked_by_human_id).await
93    }
94
95    pub async fn create_admin_service_token(
96        &self,
97        input: &CreateAdminServiceTokenInput,
98    ) -> Result<AdminServiceTokenRecord> {
99        db::sessions::create_admin_service_token(self.pool, input).await
100    }
101
102    pub async fn list_admin_service_tokens(&self) -> Result<Vec<AdminServiceTokenRecord>> {
103        db::sessions::list_admin_service_tokens(self.pool).await
104    }
105
106    pub async fn revoke_admin_service_token(
107        &self,
108        id: &AdminServiceTokenId,
109        revoked_by_human_id: &HumanId,
110    ) -> Result<AdminServiceTokenRecord> {
111        db::sessions::revoke_admin_service_token(self.pool, id, revoked_by_human_id).await
112    }
113
114    pub async fn authenticate_admin_service_token(
115        &self,
116        token_hash: &str,
117    ) -> Result<Option<AuthenticatedAdminServiceToken>> {
118        db::sessions::authenticate_admin_service_token(self.pool, token_hash).await
119    }
120
121    pub async fn create_creator_api_token(
122        &self,
123        input: &CreateCreatorApiTokenInput,
124    ) -> Result<CreatorApiTokenRecord> {
125        db::sessions::create_creator_api_token(self.pool, input).await
126    }
127
128    pub async fn list_creator_api_tokens(
129        &self,
130        human_id: &HumanId,
131    ) -> Result<Vec<CreatorApiTokenRecord>> {
132        db::sessions::list_creator_api_tokens(self.pool, human_id).await
133    }
134
135    pub async fn revoke_creator_api_token(
136        &self,
137        human_id: &HumanId,
138        id: &CreatorApiTokenId,
139    ) -> Result<CreatorApiTokenRecord> {
140        db::sessions::revoke_creator_api_token(self.pool, human_id, id).await
141    }
142
143    pub async fn authenticate_creator_api_token(
144        &self,
145        token_hash: &str,
146    ) -> Result<Option<AuthenticatedCreatorApiToken>> {
147        db::sessions::authenticate_creator_api_token(self.pool, token_hash).await
148    }
149
150    pub async fn delete_expired_web_auth_rows(&self) -> Result<()> {
151        db::sessions::delete_expired_web_auth_rows(self.pool).await
152    }
153}