summaryrefslogtreecommitdiff
path: root/src/database.rs
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-06-11 17:11:39 -0600
committermo khan <mo@mokhan.ca>2025-06-11 17:11:39 -0600
commit5ffc9b007ccbd8a4510b58de72aaee53291d7973 (patch)
treef696a2a7599926d402c5456c434bd87e5e325c3a /src/database.rs
parentdbd3c780f27bd5bee23adf6e280b84d669230e0d (diff)
refactor: apply SOLID principles
Diffstat (limited to 'src/database.rs')
-rw-r--r--src/database.rs117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/database.rs b/src/database.rs
index 2472d1a..5251dac 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -665,6 +665,123 @@ impl Database {
)?;
Ok(affected)
}
+
+ // Additional methods needed for repository patterns
+ pub fn update_oauth_client(&self, client: &DbOAuthClient) -> Result<()> {
+ self.conn.execute(
+ "UPDATE oauth_clients SET
+ client_secret_hash = ?2, client_name = ?3, redirect_uris = ?4,
+ scopes = ?5, grant_types = ?6, response_types = ?7,
+ updated_at = ?8, is_active = ?9
+ WHERE client_id = ?1",
+ params![
+ client.client_id,
+ client.client_secret_hash,
+ client.client_name,
+ client.redirect_uris,
+ client.scopes,
+ client.grant_types,
+ client.response_types,
+ client.updated_at.to_rfc3339(),
+ client.is_active
+ ],
+ )?;
+ Ok(())
+ }
+
+ pub fn delete_oauth_client(&self, client_id: &str) -> Result<()> {
+ self.conn.execute(
+ "DELETE FROM oauth_clients WHERE client_id = ?1",
+ [client_id],
+ )?;
+ Ok(())
+ }
+
+ pub fn list_oauth_clients(&self) -> Result<Vec<DbOAuthClient>> {
+ let mut stmt = self.conn.prepare(
+ "SELECT id, client_id, client_secret_hash, client_name, redirect_uris,
+ scopes, grant_types, response_types, created_at, updated_at, is_active
+ FROM oauth_clients ORDER BY created_at DESC",
+ )?;
+
+ let clients = stmt
+ .query_map([], |row| {
+ Ok(DbOAuthClient {
+ id: row.get(0)?,
+ client_id: row.get(1)?,
+ client_secret_hash: row.get(2)?,
+ client_name: row.get(3)?,
+ redirect_uris: row.get(4)?,
+ scopes: row.get(5)?,
+ grant_types: row.get(6)?,
+ response_types: row.get(7)?,
+ created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(8)?)
+ .map_err(|e| {
+ rusqlite::Error::FromSqlConversionFailure(
+ 8,
+ rusqlite::types::Type::Text,
+ Box::new(e),
+ )
+ })?
+ .with_timezone(&Utc),
+ updated_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(9)?)
+ .map_err(|e| {
+ rusqlite::Error::FromSqlConversionFailure(
+ 9,
+ rusqlite::types::Type::Text,
+ Box::new(e),
+ )
+ })?
+ .with_timezone(&Utc),
+ is_active: row.get(10)?,
+ })
+ })?
+ .collect::<Result<Vec<_>, _>>()?;
+
+ Ok(clients)
+ }
+
+ pub fn get_audit_logs(&self, limit: i32) -> Result<Vec<DbAuditLog>> {
+ let mut stmt = self.conn.prepare(
+ "SELECT id, event_type, client_id, user_id, ip_address, user_agent, details, created_at, success
+ FROM audit_logs ORDER BY created_at DESC LIMIT ?1"
+ )?;
+
+ let logs = stmt
+ .query_map([limit], |row| {
+ Ok(DbAuditLog {
+ id: row.get(0)?,
+ event_type: row.get(1)?,
+ client_id: row.get(2)?,
+ user_id: row.get(3)?,
+ ip_address: row.get(4)?,
+ user_agent: row.get(5)?,
+ details: row.get(6)?,
+ created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(7)?)
+ .map_err(|e| {
+ rusqlite::Error::FromSqlConversionFailure(
+ 7,
+ rusqlite::types::Type::Text,
+ Box::new(e),
+ )
+ })?
+ .with_timezone(&Utc),
+ success: row.get(8)?,
+ })
+ })?
+ .collect::<Result<Vec<_>, _>>()?;
+
+ Ok(logs)
+ }
+
+ pub fn cleanup_old_rate_limits(&self) -> Result<()> {
+ let cutoff = Utc::now() - chrono::Duration::hours(24); // Clean up rate limits older than 24 hours
+ self.conn.execute(
+ "DELETE FROM rate_limits WHERE created_at < ?1",
+ [cutoff.to_rfc3339()],
+ )?;
+ Ok(())
+ }
}
#[cfg(test)]