forked from coracle/caravel
Refactor some stuff
This commit is contained in:
@@ -5,7 +5,6 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
use nostr_sdk::nostr::key::PublicKey;
|
use nostr_sdk::nostr::key::PublicKey;
|
||||||
use nostr_sdk::nostr::nips::nip98::HttpMethod;
|
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::types::url::Url;
|
||||||
use nostr_sdk::nostr::{Alphabet, Event, Kind, SingleLetterTag, TagKind, TagStandard};
|
use nostr_sdk::nostr::{Alphabet, Event, Kind, SingleLetterTag, TagKind, TagStandard};
|
||||||
use nostr_sdk::JsonUtil;
|
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> {
|
pub fn verify_nip98(auth_header: &str, url: &str, method: &str) -> Result<PublicKey> {
|
||||||
let url = Url::parse(url)?;
|
let url = Url::parse(url)?;
|
||||||
let method = HttpMethod::from_str(&method.to_uppercase())?;
|
let method = HttpMethod::from_str(&method.to_uppercase())?;
|
||||||
let now = Timestamp::now();
|
|
||||||
|
|
||||||
let event = decode_auth_event(auth_header)?;
|
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"));
|
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()?;
|
event.verify()?;
|
||||||
Ok(event.pubkey)
|
Ok(event.pubkey)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,8 +85,9 @@ fn resolve_database_url(database_url: String) -> String {
|
|||||||
return database_url;
|
return database_url;
|
||||||
}
|
}
|
||||||
|
|
||||||
let base = Path::new(env!("CARGO_MANIFEST_DIR"));
|
let absolute = Path::new(env!("CARGO_MANIFEST_DIR")).join(path);
|
||||||
let absolute = base.join(path);
|
format!(
|
||||||
let normalized = absolute.to_string_lossy();
|
"sqlite:///{}",
|
||||||
format!("sqlite:///{}", normalized.trim_start_matches('/'))
|
absolute.to_string_lossy().trim_start_matches('/')
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-20
@@ -1,8 +1,8 @@
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::Result;
|
||||||
use sqlx::{migrate::Migrator, sqlite::SqliteConnectOptions, sqlite::SqlitePoolOptions, SqlitePool};
|
use sqlx::{migrate::Migrator, sqlite::SqliteConnectOptions, sqlite::SqlitePoolOptions, SqlitePool};
|
||||||
use std::str::FromStr;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
static MIGRATOR: Migrator = sqlx::migrate!("./migrations");
|
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<()> {
|
fn ensure_sqlite_directory(database_url: &str) -> Result<()> {
|
||||||
const PREFIX: &str = "sqlite://";
|
let Some(path) = sqlite_path(database_url) else {
|
||||||
if !database_url.starts_with(PREFIX) {
|
|
||||||
return Ok(());
|
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()
|
|
||||||
};
|
};
|
||||||
|
if let Some(parent) = path.parent() {
|
||||||
let parent = Path::new(&path)
|
fs::create_dir_all(parent)?;
|
||||||
.parent()
|
}
|
||||||
.ok_or_else(|| anyhow!("invalid sqlite path"))?;
|
|
||||||
fs::create_dir_all(parent)?;
|
|
||||||
Ok(())
|
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))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user