summaryrefslogtreecommitdiff
path: root/src/database.rs
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-06-11 15:15:41 -0600
committermo khan <mo@mokhan.ca>2025-06-11 15:15:41 -0600
commitaea6bd6ec7d7e70a67723edf6327df4a9cc65d89 (patch)
tree80fcb6cbda7baa5ed15cf044d7583acb2438c4d2 /src/database.rs
parent4435ee26b79648e92d0f172e42f9e6629e955505 (diff)
chore: run rustfmt again
Diffstat (limited to 'src/database.rs')
-rw-r--r--src/database.rs146
1 files changed, 104 insertions, 42 deletions
diff --git a/src/database.rs b/src/database.rs
index dc33cf8..2472d1a 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -1,6 +1,6 @@
use anyhow::Result;
use chrono::{DateTime, Utc};
-use rusqlite::{params, Connection};
+use rusqlite::{Connection, params};
use serde::{Deserialize, Serialize};
use std::path::Path;
@@ -10,9 +10,9 @@ pub struct DbOAuthClient {
pub client_id: String,
pub client_secret_hash: String,
pub client_name: String,
- pub redirect_uris: String, // JSON array
- pub scopes: String, // Space-separated
- pub grant_types: String, // Space-separated
+ pub redirect_uris: String, // JSON array
+ pub scopes: String, // Space-separated
+ pub grant_types: String, // Space-separated
pub response_types: String, // Space-separated
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
@@ -270,7 +270,7 @@ impl Database {
"INSERT INTO oauth_clients
(client_id, client_secret_hash, client_name, redirect_uris, scopes,
grant_types, response_types, created_at, updated_at, is_active)
- VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)"
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
)?;
let id = stmt.insert(params![
@@ -293,7 +293,7 @@ impl Database {
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 WHERE client_id = ?1 AND is_active = 1"
+ FROM oauth_clients WHERE client_id = ?1 AND is_active = 1",
)?;
let client = stmt.query_row([client_id], |row| {
@@ -307,10 +307,22 @@ impl Database {
grant_types: row.get(6)?,
response_types: row.get(7)?,
created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(8)?)
- .map_err(|_| rusqlite::Error::InvalidColumnType(8, "created_at".to_string(), rusqlite::types::Type::Text))?
+ .map_err(|_| {
+ rusqlite::Error::InvalidColumnType(
+ 8,
+ "created_at".to_string(),
+ rusqlite::types::Type::Text,
+ )
+ })?
.with_timezone(&Utc),
updated_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(9)?)
- .map_err(|_| rusqlite::Error::InvalidColumnType(9, "updated_at".to_string(), rusqlite::types::Type::Text))?
+ .map_err(|_| {
+ rusqlite::Error::InvalidColumnType(
+ 9,
+ "updated_at".to_string(),
+ rusqlite::types::Type::Text,
+ )
+ })?
.with_timezone(&Utc),
is_active: row.get(10)?,
})
@@ -329,7 +341,7 @@ impl Database {
"INSERT INTO auth_codes
(code, client_id, user_id, redirect_uri, scope, expires_at, created_at,
is_used, code_challenge, code_challenge_method)
- VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)"
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
)?;
let id = stmt.insert(params![
@@ -352,7 +364,7 @@ impl Database {
let mut stmt = self.conn.prepare(
"SELECT id, code, client_id, user_id, redirect_uri, scope, expires_at,
created_at, is_used, code_challenge, code_challenge_method
- FROM auth_codes WHERE code = ?1"
+ FROM auth_codes WHERE code = ?1",
)?;
let auth_code = stmt.query_row([code], |row| {
@@ -364,10 +376,22 @@ impl Database {
redirect_uri: row.get(4)?,
scope: row.get(5)?,
expires_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(6)?)
- .map_err(|_| rusqlite::Error::InvalidColumnType(6, "expires_at".to_string(), rusqlite::types::Type::Text))?
+ .map_err(|_| {
+ rusqlite::Error::InvalidColumnType(
+ 6,
+ "expires_at".to_string(),
+ rusqlite::types::Type::Text,
+ )
+ })?
.with_timezone(&Utc),
created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(7)?)
- .map_err(|_| rusqlite::Error::InvalidColumnType(7, "created_at".to_string(), rusqlite::types::Type::Text))?
+ .map_err(|_| {
+ rusqlite::Error::InvalidColumnType(
+ 7,
+ "created_at".to_string(),
+ rusqlite::types::Type::Text,
+ )
+ })?
.with_timezone(&Utc),
is_used: row.get(8)?,
code_challenge: row.get(9)?,
@@ -383,10 +407,8 @@ impl Database {
}
pub fn mark_auth_code_used(&self, code: &str) -> Result<()> {
- self.conn.execute(
- "UPDATE auth_codes SET is_used = 1 WHERE code = ?1",
- [code],
- )?;
+ self.conn
+ .execute("UPDATE auth_codes SET is_used = 1 WHERE code = ?1", [code])?;
Ok(())
}
@@ -395,7 +417,7 @@ impl Database {
let mut stmt = self.conn.prepare(
"INSERT INTO access_tokens
(token_id, client_id, user_id, scope, expires_at, created_at, is_revoked, token_hash)
- VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)"
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
)?;
let id = stmt.insert(params![
@@ -426,10 +448,22 @@ impl Database {
user_id: row.get(3)?,
scope: row.get(4)?,
expires_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(5)?)
- .map_err(|_| rusqlite::Error::InvalidColumnType(5, "expires_at".to_string(), rusqlite::types::Type::Text))?
+ .map_err(|_| {
+ rusqlite::Error::InvalidColumnType(
+ 5,
+ "expires_at".to_string(),
+ rusqlite::types::Type::Text,
+ )
+ })?
.with_timezone(&Utc),
created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(6)?)
- .map_err(|_| rusqlite::Error::InvalidColumnType(6, "created_at".to_string(), rusqlite::types::Type::Text))?
+ .map_err(|_| {
+ rusqlite::Error::InvalidColumnType(
+ 6,
+ "created_at".to_string(),
+ rusqlite::types::Type::Text,
+ )
+ })?
.with_timezone(&Utc),
is_revoked: row.get(7)?,
token_hash: row.get(8)?,
@@ -455,7 +489,7 @@ impl Database {
pub fn create_rsa_key(&self, key: &DbRsaKey) -> Result<i64> {
let mut stmt = self.conn.prepare(
"INSERT INTO rsa_keys (kid, private_key_pem, public_key_pem, created_at, is_current)
- VALUES (?1, ?2, ?3, ?4, ?5)"
+ VALUES (?1, ?2, ?3, ?4, ?5)",
)?;
let id = stmt.insert(params![
@@ -472,7 +506,7 @@ impl Database {
pub fn get_current_rsa_key(&self) -> Result<Option<DbRsaKey>> {
let mut stmt = self.conn.prepare(
"SELECT id, kid, private_key_pem, public_key_pem, created_at, is_current
- FROM rsa_keys WHERE is_current = 1 ORDER BY created_at DESC LIMIT 1"
+ FROM rsa_keys WHERE is_current = 1 ORDER BY created_at DESC LIMIT 1",
)?;
let key = stmt.query_row([], |row| {
@@ -482,7 +516,13 @@ impl Database {
private_key_pem: row.get(2)?,
public_key_pem: row.get(3)?,
created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(4)?)
- .map_err(|_| rusqlite::Error::InvalidColumnType(4, "created_at".to_string(), rusqlite::types::Type::Text))?
+ .map_err(|_| {
+ rusqlite::Error::InvalidColumnType(
+ 4,
+ "created_at".to_string(),
+ rusqlite::types::Type::Text,
+ )
+ })?
.with_timezone(&Utc),
is_current: row.get(5)?,
})
@@ -498,7 +538,7 @@ impl Database {
pub fn get_all_rsa_keys(&self) -> Result<Vec<DbRsaKey>> {
let mut stmt = self.conn.prepare(
"SELECT id, kid, private_key_pem, public_key_pem, created_at, is_current
- FROM rsa_keys ORDER BY created_at DESC"
+ FROM rsa_keys ORDER BY created_at DESC",
)?;
let keys = stmt.query_map([], |row| {
@@ -508,7 +548,13 @@ impl Database {
private_key_pem: row.get(2)?,
public_key_pem: row.get(3)?,
created_at: DateTime::parse_from_rfc3339(&row.get::<_, String>(4)?)
- .map_err(|_| rusqlite::Error::InvalidColumnType(4, "created_at".to_string(), rusqlite::types::Type::Text))?
+ .map_err(|_| {
+ rusqlite::Error::InvalidColumnType(
+ 4,
+ "created_at".to_string(),
+ rusqlite::types::Type::Text,
+ )
+ })?
.with_timezone(&Utc),
is_current: row.get(5)?,
})
@@ -523,14 +569,13 @@ impl Database {
pub fn set_current_rsa_key(&self, kid: &str) -> Result<()> {
// First, unset all current keys
- self.conn.execute("UPDATE rsa_keys SET is_current = 0", [])?;
-
+ self.conn
+ .execute("UPDATE rsa_keys SET is_current = 0", [])?;
+
// Then set the specified key as current
- self.conn.execute(
- "UPDATE rsa_keys SET is_current = 1 WHERE kid = ?1",
- [kid],
- )?;
-
+ self.conn
+ .execute("UPDATE rsa_keys SET is_current = 1 WHERE kid = ?1", [kid])?;
+
Ok(())
}
@@ -539,7 +584,7 @@ impl Database {
let mut stmt = self.conn.prepare(
"INSERT INTO audit_logs
(event_type, client_id, user_id, ip_address, user_agent, details, created_at, success)
- VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)"
+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
)?;
let id = stmt.insert(params![
@@ -557,7 +602,12 @@ impl Database {
}
// Rate Limiting operations
- pub fn increment_rate_limit(&self, identifier: &str, endpoint: &str, window_minutes: i32) -> Result<i32> {
+ pub fn increment_rate_limit(
+ &self,
+ identifier: &str,
+ endpoint: &str,
+ window_minutes: i32,
+ ) -> Result<i32> {
let now = Utc::now();
let window_start = now - chrono::Duration::minutes(window_minutes as i64);
@@ -630,7 +680,7 @@ mod tests {
#[test]
fn test_oauth_client_operations() {
let db = Database::new_in_memory().expect("Failed to create database");
-
+
let client = DbOAuthClient {
id: 0,
client_id: "test_client".to_string(),
@@ -645,10 +695,14 @@ mod tests {
is_active: true,
};
- let id = db.create_oauth_client(&client).expect("Failed to create client");
+ let id = db
+ .create_oauth_client(&client)
+ .expect("Failed to create client");
assert!(id > 0);
- let retrieved = db.get_oauth_client("test_client").expect("Failed to get client");
+ let retrieved = db
+ .get_oauth_client("test_client")
+ .expect("Failed to get client");
assert!(retrieved.is_some());
assert_eq!(retrieved.unwrap().client_name, "Test Client");
}
@@ -671,7 +725,8 @@ mod tests {
updated_at: Utc::now(),
is_active: true,
};
- db.create_oauth_client(&client).expect("Failed to create client");
+ db.create_oauth_client(&client)
+ .expect("Failed to create client");
let auth_code = DbAuthCode {
id: 0,
@@ -687,17 +742,24 @@ mod tests {
code_challenge_method: Some("S256".to_string()),
};
- let id = db.create_auth_code(&auth_code).expect("Failed to create auth code");
+ let id = db
+ .create_auth_code(&auth_code)
+ .expect("Failed to create auth code");
assert!(id > 0);
- let retrieved = db.get_auth_code("test_code_123").expect("Failed to get auth code");
+ let retrieved = db
+ .get_auth_code("test_code_123")
+ .expect("Failed to get auth code");
assert!(retrieved.is_some());
let code = retrieved.unwrap();
assert_eq!(code.client_id, "test_client");
assert_eq!(code.is_used, false);
- db.mark_auth_code_used("test_code_123").expect("Failed to mark code as used");
- let updated = db.get_auth_code("test_code_123").expect("Failed to get auth code");
+ db.mark_auth_code_used("test_code_123")
+ .expect("Failed to mark code as used");
+ let updated = db
+ .get_auth_code("test_code_123")
+ .expect("Failed to get auth code");
assert_eq!(updated.unwrap().is_used, true);
}
-} \ No newline at end of file
+}