Something went wrong. Try again.
A fork of @slices.network/slices forked from slices.network/slices
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116//! OAuth client management operations.//!//! This module handles database operations for OAuth client registrations//! associated with slices, including creation, retrieval, and deletion.
use super::client::Database;use crate::errors::DatabaseError;use crate::models::OAuthClient;
impl Database { /// Creates a new OAuth client registration for a slice. /// /// # Arguments /// * `slice_uri` - The slice this client is registered for /// * `client_id` - The OAuth client ID from the authorization server /// * `registration_access_token` - Optional token for client management /// * `created_by_did` - The DID of the user who created this client /// /// # Returns /// The created OAuthClient with generated ID and timestamp pub async fn create_oauth_client( &self, slice_uri: &str, client_id: &str, registration_access_token: Option<&str>, created_by_did: &str, ) -> Result<OAuthClient, DatabaseError> { let client = sqlx::query_as!( OAuthClient, r#" INSERT INTO oauth_clients (slice_uri, client_id, registration_access_token, created_by_did) VALUES ($1, $2, $3, $4) RETURNING id, slice_uri, client_id, registration_access_token, created_at as "created_at!", created_by_did "#, slice_uri, client_id, registration_access_token, created_by_did ) .fetch_one(&self.pool) .await?;
Ok(client) }
/// Gets all OAuth clients registered for a specific slice. /// /// Results are ordered by creation time, most recent first. pub async fn get_oauth_clients_for_slice( &self, slice_uri: &str, ) -> Result<Vec<OAuthClient>, DatabaseError> { let clients = sqlx::query_as!( OAuthClient, r#" SELECT id, slice_uri, client_id, registration_access_token, created_at as "created_at!", created_by_did FROM oauth_clients WHERE slice_uri = $1 ORDER BY created_at DESC "#, slice_uri ) .fetch_all(&self.pool) .await?;
Ok(clients) }
/// Gets a single OAuth client by its client_id. /// /// # Returns /// Some(OAuthClient) if found, None otherwise pub async fn get_oauth_client_by_id( &self, client_id: &str, ) -> Result<Option<OAuthClient>, DatabaseError> { let client = sqlx::query_as!( OAuthClient, r#" SELECT id, slice_uri, client_id, registration_access_token, created_at as "created_at!", created_by_did FROM oauth_clients WHERE client_id = $1 "#, client_id ) .fetch_optional(&self.pool) .await?;
Ok(client) }
/// Deletes an OAuth client by its client_id. /// /// # Returns /// Error if no client with the given client_id exists pub async fn delete_oauth_client(&self, client_id: &str) -> Result<(), DatabaseError> { let result = sqlx::query!( r#" DELETE FROM oauth_clients WHERE client_id = $1 "#, client_id ) .execute(&self.pool) .await?;
if result.rows_affected() == 0 { return Err(DatabaseError::RecordNotFound { uri: client_id.to_string(), }); }
Ok(()) }}