Refactor some stuff

This commit is contained in:
Jon Staab
2026-02-25 19:23:15 -08:00
parent 58e8c821d4
commit 4e4d5907bf
3 changed files with 20 additions and 36 deletions
-12
View File
@@ -5,7 +5,6 @@ use std::str::FromStr;
use nostr_sdk::nostr::key::PublicKey;
use nostr_sdk::nostr::nips::nip98::HttpMethod;
use nostr_sdk::nostr::types::time::Timestamp;
use nostr_sdk::nostr::types::url::Url;
use nostr_sdk::nostr::{Alphabet, Event, Kind, SingleLetterTag, TagKind, TagStandard};
use nostr_sdk::JsonUtil;
@@ -13,7 +12,6 @@ use nostr_sdk::JsonUtil;
pub fn verify_nip98(auth_header: &str, url: &str, method: &str) -> Result<PublicKey> {
let url = Url::parse(url)?;
let method = HttpMethod::from_str(&method.to_uppercase())?;
let now = Timestamp::now();
let event = decode_auth_event(auth_header)?;
@@ -40,16 +38,6 @@ pub fn verify_nip98(auth_header: &str, url: &str, method: &str) -> Result<Public
return Err(anyhow!("authorization does not match request"));
}
let created_at = event.created_at.as_u64();
let now_secs = now.as_u64();
if created_at > now_secs {
if created_at - now_secs > 30 {
return Err(anyhow!("authorization timestamp too far in future"));
}
} else if now_secs - created_at > 60 {
return Err(anyhow!("authorization timestamp too old"));
}
event.verify()?;
Ok(event.pubkey)
}
+5 -4
View File
@@ -85,8 +85,9 @@ fn resolve_database_url(database_url: String) -> String {
return database_url;
}
let base = Path::new(env!("CARGO_MANIFEST_DIR"));
let absolute = base.join(path);
let normalized = absolute.to_string_lossy();
format!("sqlite:///{}", normalized.trim_start_matches('/'))
let absolute = Path::new(env!("CARGO_MANIFEST_DIR")).join(path);
format!(
"sqlite:///{}",
absolute.to_string_lossy().trim_start_matches('/')
)
}
+15 -20
View File
@@ -1,8 +1,8 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use sqlx::{migrate::Migrator, sqlite::SqliteConnectOptions, sqlite::SqlitePoolOptions, SqlitePool};
use std::str::FromStr;
use std::fs;
use std::path::Path;
use std::str::FromStr;
static MIGRATOR: Migrator = sqlx::migrate!("./migrations");
@@ -24,25 +24,20 @@ pub async fn init_pool(database_url: &str) -> Result<SqlitePool> {
}
fn ensure_sqlite_directory(database_url: &str) -> Result<()> {
const PREFIX: &str = "sqlite://";
if !database_url.starts_with(PREFIX) {
let Some(path) = sqlite_path(database_url) else {
return Ok(());
}
let path = &database_url[PREFIX.len()..];
if path.is_empty() || path == ":memory:" {
return Ok(());
}
let path = if let Some(stripped) = path.strip_prefix('/') {
format!("/{}", stripped)
} else {
path.to_string()
};
let parent = Path::new(&path)
.parent()
.ok_or_else(|| anyhow!("invalid sqlite path"))?;
fs::create_dir_all(parent)?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
Ok(())
}
fn sqlite_path(database_url: &str) -> Option<&Path> {
const PREFIX: &str = "sqlite://";
let path = database_url.strip_prefix(PREFIX)?;
if path.is_empty() || path == ":memory:" {
return None;
}
Some(Path::new(path))
}