use serde::{Deserialize, Serialize};
use crate::error::PodError;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SolidWellKnown {
#[serde(rename = "@context")]
pub context: serde_json::Value,
pub solid_oidc_issuer: String,
pub notification_gateway: String,
pub storage: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub webfinger: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub api: Option<SolidWellKnownApi>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SolidWellKnownApi {
pub accounts: SolidWellKnownAccounts,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SolidWellKnownAccounts {
pub new: String,
pub recover: String,
pub signin: String,
pub signout: String,
}
pub fn well_known_solid(
pod_base: &str,
oidc_issuer: &str,
) -> SolidWellKnown {
let base = pod_base.trim_end_matches('/');
SolidWellKnown {
context: serde_json::json!("https://www.w3.org/ns/solid/terms"),
solid_oidc_issuer: oidc_issuer.trim_end_matches('/').to_string(),
notification_gateway: format!("{base}/.notifications"),
storage: format!("{base}/"),
webfinger: Some(format!("{base}/.well-known/webfinger")),
api: Some(SolidWellKnownApi {
accounts: SolidWellKnownAccounts {
new: format!("{base}/api/accounts/new"),
recover: format!("{base}/api/accounts/recover"),
signin: format!("{base}/login"),
signout: format!("{base}/logout"),
},
}),
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebFingerJrd {
pub subject: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub aliases: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub links: Vec<WebFingerLink>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebFingerLink {
pub rel: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub href: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "type")]
pub content_type: Option<String>,
}
pub fn webfinger_response(
resource: &str,
pod_base: &str,
webid: &str,
) -> Option<WebFingerJrd> {
if !resource.starts_with("acct:") && !resource.starts_with("https://") {
return None;
}
let base = pod_base.trim_end_matches('/');
Some(WebFingerJrd {
subject: resource.to_string(),
aliases: vec![webid.to_string()],
links: vec![
WebFingerLink {
rel: "http://openid.net/specs/connect/1.0/issuer".to_string(),
href: Some(format!("{base}/")),
content_type: None,
},
WebFingerLink {
rel: "http://www.w3.org/ns/solid#webid".to_string(),
href: Some(webid.to_string()),
content_type: None,
},
WebFingerLink {
rel: "http://www.w3.org/ns/pim/space#storage".to_string(),
href: Some(format!("{base}/")),
content_type: None,
},
],
})
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Nip05Document {
pub names: std::collections::HashMap<String, String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub relays: Option<std::collections::HashMap<String, Vec<String>>>,
}
pub fn verify_nip05(
identifier: &str,
document: &Nip05Document,
) -> Result<String, PodError> {
let (local, _domain) = identifier
.split_once('@')
.ok_or_else(|| PodError::Nip98(format!("invalid NIP-05 identifier: {identifier}")))?;
let lookup = if local.is_empty() { "_" } else { local };
let pubkey = document
.names
.get(lookup)
.ok_or_else(|| PodError::NotFound(format!("NIP-05 name not found: {lookup}")))?;
if pubkey.len() != 64 || hex::decode(pubkey).is_err() {
return Err(PodError::Nip98(format!(
"NIP-05 pubkey malformed for {identifier}"
)));
}
Ok(pubkey.clone())
}
pub fn nip05_document(
names: impl IntoIterator<Item = (String, String)>,
) -> Nip05Document {
Nip05Document {
names: names.into_iter().collect(),
relays: None,
}
}
#[derive(Debug, Clone)]
pub struct DevSession {
pub webid: String,
pub pubkey: Option<String>,
pub is_admin: bool,
}
pub fn dev_session(webid: impl Into<String>, is_admin: bool) -> DevSession {
DevSession {
webid: webid.into(),
pubkey: None,
is_admin,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn well_known_solid_advertises_oidc_and_storage() {
let d = well_known_solid("https://pod.example/", "https://op.example/");
assert_eq!(d.solid_oidc_issuer, "https://op.example");
assert!(d.notification_gateway.ends_with(".notifications"));
assert!(d.storage.ends_with('/'));
}
#[test]
fn webfinger_returns_links_for_acct() {
let j = webfinger_response(
"acct:alice@pod.example",
"https://pod.example",
"https://pod.example/profile/card#me",
)
.unwrap();
assert_eq!(j.subject, "acct:alice@pod.example");
assert!(j.links.iter().any(|l| l.rel == "http://www.w3.org/ns/solid#webid"));
}
#[test]
fn webfinger_rejects_unknown_scheme() {
assert!(webfinger_response("mailto:a@b", "https://p", "https://w").is_none());
}
#[test]
fn nip05_verify_returns_pubkey() {
let mut names = std::collections::HashMap::new();
names.insert("alice".to_string(), "a".repeat(64));
let doc = nip05_document(names);
let pk = verify_nip05("alice@pod.example", &doc).unwrap();
assert_eq!(pk, "a".repeat(64));
}
#[test]
fn nip05_verify_rejects_malformed_pubkey() {
let mut names = std::collections::HashMap::new();
names.insert("alice".to_string(), "shortkey".to_string());
let doc = nip05_document(names);
assert!(verify_nip05("alice@p", &doc).is_err());
}
#[test]
fn nip05_root_name_resolves_via_underscore() {
let mut names = std::collections::HashMap::new();
names.insert("_".to_string(), "b".repeat(64));
let doc = nip05_document(names);
assert!(verify_nip05("@pod.example", &doc).is_ok());
}
#[test]
fn dev_session_stores_admin_flag() {
let s = dev_session("https://me/profile#me", true);
assert!(s.is_admin);
assert_eq!(s.webid, "https://me/profile#me");
}
}
#[cfg(feature = "did-nostr")]
pub mod did_nostr {
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use url::Url;
use crate::security::ssrf::SsrfPolicy;
pub fn did_nostr_well_known_url(origin: &str, pubkey: &str) -> String {
format!(
"{}/.well-known/did/nostr/{}.json",
origin.trim_end_matches('/'),
pubkey
)
}
pub fn did_nostr_document(pubkey: &str, also_known_as: &[String]) -> serde_json::Value {
serde_json::json!({
"@context": [
"https://www.w3.org/ns/did/v1",
"https://w3id.org/security/suites/secp256k1-2019/v1"
],
"id": format!("did:nostr:{}", pubkey),
"alsoKnownAs": also_known_as,
"verificationMethod": [{
"id": format!("did:nostr:{}#nostr-schnorr", pubkey),
"type": "SchnorrSecp256k1VerificationKey2019",
"controller": format!("did:nostr:{}", pubkey),
"publicKeyHex": pubkey,
}]
})
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct DidNostrDoc {
pub id: String,
#[serde(default, rename = "alsoKnownAs")]
pub also_known_as: Vec<String>,
}
pub struct DidNostrResolver {
ssrf: Arc<SsrfPolicy>,
client: Client,
cache: Arc<RwLock<HashMap<String, CachedEntry>>>,
success_ttl: Duration,
failure_ttl: Duration,
}
struct CachedEntry {
fetched: Instant,
web_id: Option<String>,
}
impl DidNostrResolver {
pub fn new(ssrf: Arc<SsrfPolicy>) -> Self {
let client = Client::builder()
.timeout(Duration::from_secs(10))
.build()
.unwrap_or_else(|_| Client::new());
Self {
ssrf,
client,
cache: Arc::new(RwLock::new(HashMap::new())),
success_ttl: Duration::from_secs(300),
failure_ttl: Duration::from_secs(60),
}
}
pub fn with_ttls(mut self, success: Duration, failure: Duration) -> Self {
self.success_ttl = success;
self.failure_ttl = failure;
self
}
pub async fn resolve(&self, origin: &str, pubkey: &str) -> Option<String> {
let cache_key = format!("{origin}|{pubkey}");
if let Ok(guard) = self.cache.read() {
if let Some(entry) = guard.get(&cache_key) {
let ttl = if entry.web_id.is_some() {
self.success_ttl
} else {
self.failure_ttl
};
if entry.fetched.elapsed() < ttl {
return entry.web_id.clone();
}
}
}
let result = self.resolve_uncached(origin, pubkey).await;
if let Ok(mut guard) = self.cache.write() {
guard.insert(
cache_key,
CachedEntry {
fetched: Instant::now(),
web_id: result.clone(),
},
);
}
result
}
async fn resolve_uncached(&self, origin: &str, pubkey: &str) -> Option<String> {
let origin_url = Url::parse(origin).ok()?;
self.ssrf.resolve_and_check(&origin_url).await.ok()?;
let url = did_nostr_well_known_url(origin, pubkey);
let resp = self
.client
.get(&url)
.header("accept", "application/did+json, application/json")
.send()
.await
.ok()?
.error_for_status()
.ok()?;
let doc: DidNostrDoc = resp.json().await.ok()?;
if doc.id != format!("did:nostr:{pubkey}") {
return None;
}
let did_iri = format!("did:nostr:{pubkey}");
for candidate in &doc.also_known_as {
if let Some(web_id) = self.try_candidate(candidate, &did_iri).await {
return Some(web_id);
}
}
None
}
async fn try_candidate(&self, candidate: &str, did_iri: &str) -> Option<String> {
let url = Url::parse(candidate).ok()?;
self.ssrf.resolve_and_check(&url).await.ok()?;
let resp = self
.client
.get(url.as_str())
.header("accept", "text/turtle, application/ld+json")
.send()
.await
.ok()?
.error_for_status()
.ok()?;
let body = resp.text().await.ok()?;
let has_predicate = body.contains("owl:sameAs")
|| body.contains("schema:sameAs")
|| body.contains("http://www.w3.org/2002/07/owl#sameAs")
|| body.contains("https://schema.org/sameAs");
if has_predicate && body.contains(did_iri) {
Some(candidate.to_string())
} else {
None
}
}
}
}
pub fn nodeinfo_discovery(base_url: &str) -> serde_json::Value {
serde_json::json!({
"links": [
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
"href": format!(
"{}/.well-known/nodeinfo/2.1",
base_url.trim_end_matches('/')
)
}
]
})
}
pub fn nodeinfo_2_1(
software_name: &str,
software_version: &str,
open_registrations: bool,
total_users: u64,
) -> serde_json::Value {
serde_json::json!({
"version": "2.1",
"software": {
"name": software_name,
"version": software_version,
"repository": "https://github.com/dreamlab-ai/solid-pod-rs",
"homepage": "https://github.com/dreamlab-ai/solid-pod-rs"
},
"protocols": ["solid", "activitypub"],
"services": {
"inbound": [],
"outbound": []
},
"openRegistrations": open_registrations,
"usage": {
"users": {
"total": total_users
}
},
"metadata": {}
})
}