From 9ca31fd3713ed9bcf1e1eb601c022afcb4fc4d74 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 9 Nov 2022 15:47:22 -0800 Subject: pds: session.get and handle.resolve XRPC endpoints --- adenosine-pds/src/db.rs | 26 ++++++++++++++++++++++++++ adenosine-pds/src/lib.rs | 32 ++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/adenosine-pds/src/db.rs b/adenosine-pds/src/db.rs index 051588d..b94689a 100644 --- a/adenosine-pds/src/db.rs +++ b/adenosine-pds/src/db.rs @@ -138,6 +138,32 @@ impl AtpDatabase { Ok(did_maybe.map(|v| Did::from_str(&v).expect("valid DID in database"))) } + /// Looks up local account handle associated with a DID + /// + /// TODO: remote lookups, and/or local cache of external DIDs + pub fn resolve_did(&mut self, did: &Did) -> Result> { + let mut stmt = self + .conn + .prepare_cached("SELECT handle FROM account WHERE did = $1")?; + let handle_maybe: Option = stmt + .query_row(params!(did.to_string()), |row| row.get(0)) + .optional()?; + Ok(handle_maybe) + } + + /// Looks up local DID associated with handle + /// + /// TODO: remote lookups, and/or local cache of external DIDs + pub fn resolve_handle(&mut self, handle: &str) -> Result> { + let mut stmt = self + .conn + .prepare_cached("SELECT did FROM account WHERE handle = $1")?; + let did_maybe: Option = stmt + .query_row(params!(handle), |row| row.get(0)) + .optional()?; + Ok(did_maybe.map(|v| Did::from_str(&v).expect("valid DID in database"))) + } + pub fn delete_session(&mut self, jwt: &str) -> Result { let mut stmt = self .conn diff --git a/adenosine-pds/src/lib.rs b/adenosine-pds/src/lib.rs index 738a061..7b517f2 100644 --- a/adenosine-pds/src/lib.rs +++ b/adenosine-pds/src/lib.rs @@ -383,21 +383,25 @@ fn xrpc_get_handler( } Ok(json!({ "records": record_list })) } - "com.atproto.repo.describe" => { - let did = Did::from_str(&xrpc_required_param(request, "user")?)?; - // TODO: resolve handle? - let handle = did.to_string(); + "com.atproto.session.get" => { let mut srv = srv.lock().expect("service mutex"); - let did_doc = srv.atp_db.get_did_doc(&did)?; - let collections: Vec = srv.repo.collections(&did)?; - let desc = RepoDescribe { - name: handle, - did: did.to_string(), - didDoc: did_doc, - collections: collections, - nameIsCorrect: true, - }; - Ok(json!(desc)) + let auth_did = &xrpc_check_auth_header(&mut srv, request, None)?; + let handle = srv + .atp_db + .resolve_did(auth_did)? + .expect("registered account has handle"); + Ok(json!({"did": auth_did.to_string(), "handle": handle})) + } + "com.atproto.handle.resolve" => { + let handle = xrpc_required_param(request, "handle")?; + let mut srv = srv.lock().expect("service mutex"); + match srv.atp_db.resolve_handle(&handle)? { + Some(did) => Ok(json!({"did": did.to_string()})), + None => Err(XrpcError::NotFound(format!( + "could not resolve handle internally: {}", + handle + )))?, + } } // =========== app.bsky methods "app.bsky.actor.getProfile" => { -- cgit v1.2.3