From f9ca910036cf9020c9fc36b57194d285a0396f9d Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Thu, 10 Nov 2022 19:05:16 -0800 Subject: pds: implement homepage and did.json lookups --- adenosine-pds/src/lib.rs | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) (limited to 'adenosine-pds') diff --git a/adenosine-pds/src/lib.rs b/adenosine-pds/src/lib.rs index 9a4aa8a..2f2c114 100644 --- a/adenosine-pds/src/lib.rs +++ b/adenosine-pds/src/lib.rs @@ -183,12 +183,16 @@ impl AtpService { router!(request, // ============= Web Interface (GET) ["/"] => { - let host = request.header("Host").unwrap_or("localhost"); - if Some(host.to_string()) == config.registration_domain { - web_wrap(account_view_handler(&srv, &host, request)) + if let Some(ref handle) = config.homepage_handle { + web_wrap(account_view_handler(&srv, handle, request)) } else { - let view = GenericHomeView { domain: host.to_string() }; - Response::html(view.render().unwrap()) + web_wrap(home_view_handler(&srv, request)) + } + }, + (GET) ["/.well-known/did.json"] => { + match did_doc_view_handler(&srv, request) { + Ok(resp) => resp, + Err(e) => web_wrap(Err(e)), } }, (GET) ["/about"] => { @@ -675,6 +679,39 @@ fn xrpc_post_handler( } } +fn home_view_handler(srv: &Mutex, request: &Request) -> Result { + let host = request.header("Host").unwrap_or("localhost"); + + // check if the hostname resolves to a DID (account) + let did: Option = { + // this mutex lock should drop at the end of this block + let mut srv = srv.lock().or(Err(XrpcError::MutexPoisoned))?; + srv.atp_db.resolve_handle(host)? + }; + if did.is_some() { + account_view_handler(&srv, &host, request) + } else { + let view = GenericHomeView { + domain: host.to_string(), + }; + Ok(view.render()?) + } +} + +fn did_doc_view_handler(srv: &Mutex, request: &Request) -> Result { + let host = request.header("Host").unwrap_or("localhost"); + let mut srv = srv.lock().or(Err(XrpcError::MutexPoisoned))?; + if let Some(did) = srv.atp_db.resolve_handle(host)? { + if did.to_string().starts_with("did:web:") { + let did_doc = srv.atp_db.get_did_doc(&did)?; + return Ok(Response::json(&did_doc)); + } + }; + Err(XrpcError::NotFound( + "no did:web: account registered at this domain".to_string(), + ))? +} + // TODO: did, collection, tid have already been parsed by this point fn account_view_handler( srv: &Mutex, -- cgit v1.2.3