Highly ambitious ATProtocol AppView service and sdks
1//! OAuth client management operations.
2//!
3//! This module handles database operations for OAuth client registrations
4//! associated with slices, including creation, retrieval, and deletion.
5
6use super::client::Database;
7use crate::errors::DatabaseError;
8use crate::models::OAuthClient;
9
10impl Database {
11 /// Creates a new OAuth client registration for a slice.
12 ///
13 /// # Arguments
14 /// * `slice_uri` - The slice this client is registered for
15 /// * `client_id` - The OAuth client ID from the authorization server
16 /// * `registration_access_token` - Optional token for client management
17 /// * `created_by_did` - The DID of the user who created this client
18 ///
19 /// # Returns
20 /// The created OAuthClient with generated ID and timestamp
21 pub async fn create_oauth_client(
22 &self,
23 slice_uri: &str,
24 client_id: &str,
25 registration_access_token: Option<&str>,
26 created_by_did: &str,
27 ) -> Result<OAuthClient, DatabaseError> {
28 let client = sqlx::query_as!(
29 OAuthClient,
30 r#"
31 INSERT INTO oauth_clients (slice_uri, client_id, registration_access_token, created_by_did)
32 VALUES ($1, $2, $3, $4)
33 RETURNING id, slice_uri, client_id, registration_access_token, created_at as "created_at!", created_by_did
34 "#,
35 slice_uri,
36 client_id,
37 registration_access_token,
38 created_by_did
39 )
40 .fetch_one(&self.pool)
41 .await?;
42
43 Ok(client)
44 }
45
46 /// Gets all OAuth clients registered for a specific slice.
47 ///
48 /// Results are ordered by creation time, most recent first.
49 pub async fn get_oauth_clients_for_slice(
50 &self,
51 slice_uri: &str,
52 ) -> Result<Vec<OAuthClient>, DatabaseError> {
53 let clients = sqlx::query_as!(
54 OAuthClient,
55 r#"
56 SELECT id, slice_uri, client_id, registration_access_token, created_at as "created_at!", created_by_did
57 FROM oauth_clients
58 WHERE slice_uri = $1
59 ORDER BY created_at DESC
60 "#,
61 slice_uri
62 )
63 .fetch_all(&self.pool)
64 .await?;
65
66 Ok(clients)
67 }
68
69 /// Gets a single OAuth client by its client_id.
70 ///
71 /// # Returns
72 /// Some(OAuthClient) if found, None otherwise
73 pub async fn get_oauth_client_by_id(
74 &self,
75 client_id: &str,
76 ) -> Result<Option<OAuthClient>, DatabaseError> {
77 let client = sqlx::query_as!(
78 OAuthClient,
79 r#"
80 SELECT id, slice_uri, client_id, registration_access_token, created_at as "created_at!", created_by_did
81 FROM oauth_clients
82 WHERE client_id = $1
83 "#,
84 client_id
85 )
86 .fetch_optional(&self.pool)
87 .await?;
88
89 Ok(client)
90 }
91
92 /// Deletes an OAuth client by its client_id.
93 ///
94 /// # Returns
95 /// Error if no client with the given client_id exists
96 pub async fn delete_oauth_client(&self, client_id: &str) -> Result<(), DatabaseError> {
97 let result = sqlx::query!(
98 r#"
99 DELETE FROM oauth_clients
100 WHERE client_id = $1
101 "#,
102 client_id
103 )
104 .execute(&self.pool)
105 .await?;
106
107 if result.rows_affected() == 0 {
108 return Err(DatabaseError::RecordNotFound {
109 uri: client_id.to_string(),
110 });
111 }
112
113 Ok(())
114 }
115}