Skip to content

Commit

Permalink
Update atrium-identity
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Sep 25, 2024
1 parent 2f0613a commit dc223cb
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 444 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion atrium-oauth/identity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ keywords = ["atproto", "bluesky", "identity"]
atrium-api = { workspace = true, default-features = false }
atrium-xrpc.workspace = true
dashmap.workspace = true
futures.workspace = true
hickory-proto = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
serde_html_form.workspace = true
serde_json.workspace = true
thiserror.workspace = true
tokio = { workspace = true, default-features = false, features = ["sync"] }
trait-variant.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
moka = { workspace = true, features = ["future"] }
Expand All @@ -32,6 +32,9 @@ moka = { workspace = true, features = ["future"] }
lru.workspace = true
web-time.workspace = true

[dev-dependencies]
futures.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }

Expand Down
2 changes: 0 additions & 2 deletions atrium-oauth/identity/src/did.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod base_resolver;
mod common_resolver;
mod plc_resolver;
mod web_resolver;

pub use self::common_resolver::{CommonDidResolver, CommonDidResolverConfig};
pub(crate) use self::plc_resolver::DEFAULT_PLC_DIRECTORY_URL;
use crate::Resolver;
use atrium_api::did_doc::DidDocument;
use atrium_api::types::string::Did;
Expand Down
41 changes: 0 additions & 41 deletions atrium-oauth/identity/src/did/base_resolver.rs

This file was deleted.

49 changes: 33 additions & 16 deletions atrium-oauth/identity/src/did/common_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use super::base_resolver::{BaseResolver, Method};
use atrium_api::did_doc::DidDocument;
use atrium_api::types::string::Did;

use super::plc_resolver::{PlcDidResolver, PlcDidResolverConfig};
use super::web_resolver::{WebDidResolver, WebDidResolverConfig};
use super::DidResolver;
use crate::error::Result;
use crate::error::{Error, Result};
use crate::Resolver;
use std::sync::Arc;

#[derive(Clone, Debug)]
Expand All @@ -12,33 +15,47 @@ pub struct CommonDidResolverConfig<T> {
}

pub struct CommonDidResolver<T> {
plc_resolver: Arc<PlcDidResolver<T>>,
web_resolver: Arc<WebDidResolver<T>>,
plc_resolver: PlcDidResolver<T>,
web_resolver: WebDidResolver<T>,
}

impl<T> CommonDidResolver<T> {
pub fn new(config: CommonDidResolverConfig<T>) -> Result<Self> {
Ok(Self {
plc_resolver: Arc::new(PlcDidResolver::new(PlcDidResolverConfig {
pub fn new(config: CommonDidResolverConfig<T>) -> Self {
Self {
plc_resolver: PlcDidResolver::new(PlcDidResolverConfig {
plc_directory_url: config.plc_directory_url,
http_client: config.http_client.clone(),
})?),
web_resolver: Arc::new(WebDidResolver::new(WebDidResolverConfig {
}),
web_resolver: WebDidResolver::new(WebDidResolverConfig {
http_client: config.http_client,
})),
})
}),
}
}
}

impl<T> BaseResolver for CommonDidResolver<T>
impl<T> Resolver for CommonDidResolver<T>
where
PlcDidResolver<T>: DidResolver + Send + Sync + 'static,
WebDidResolver<T>: DidResolver + Send + Sync + 'static,
{
fn get_resolver(&self, method: Method) -> Arc<dyn DidResolver + Send + Sync + 'static> {
match method {
Method::Plc => self.plc_resolver.clone(),
Method::Web => self.web_resolver.clone(),
type Input = Did;
type Output = DidDocument;

async fn resolve(&self, did: &Self::Input) -> Result<Self::Output> {
match did
.strip_prefix("did:")
.and_then(|s| s.split_once(':').and_then(|(method, _)| Some(method)))
{
Some("plc") => self.plc_resolver.resolve(did).await,
Some("web") => self.web_resolver.resolve(did).await,
_ => Err(Error::UnsupportedDidMethod(did.clone())),
}
}
}

impl<T> DidResolver for CommonDidResolver<T>
where
PlcDidResolver<T>: DidResolver + Send + Sync + 'static,
WebDidResolver<T>: DidResolver + Send + Sync + 'static,
{
}
17 changes: 6 additions & 11 deletions atrium-oauth/identity/src/did/plc_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use super::DidResolver;
use crate::error::{Error, Result};
use crate::Resolver;
use async_trait::async_trait;
use atrium_api::did_doc::DidDocument;
use atrium_api::types::string::Did;
use atrium_xrpc::http::uri::Builder;
use atrium_xrpc::http::{Request, Uri};
use atrium_xrpc::HttpClient;
use std::sync::Arc;

pub(crate) const DEFAULT_PLC_DIRECTORY_URL: &str = "https://plc.directory/";
#[allow(dead_code)]
pub const DEFAULT_PLC_DIRECTORY_URL: &str = "https://plc.directory/";

#[derive(Clone, Debug)]
pub struct PlcDidResolverConfig<T> {
Expand All @@ -18,21 +18,16 @@ pub struct PlcDidResolverConfig<T> {
}

pub struct PlcDidResolver<T> {
plc_directory_url: Uri,
plc_directory_url: String,
http_client: Arc<T>,
}

impl<T> PlcDidResolver<T> {
pub fn new(config: PlcDidResolverConfig<T>) -> Result<Self> {
Ok(Self {
plc_directory_url: config.plc_directory_url.parse()?,
http_client: config.http_client,
})
pub fn new(config: PlcDidResolverConfig<T>) -> Self {
Self { plc_directory_url: config.plc_directory_url, http_client: config.http_client }
}
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<T> Resolver for PlcDidResolver<T>
where
T: HttpClient + Send + Sync + 'static,
Expand All @@ -41,7 +36,7 @@ where
type Output = DidDocument;

async fn resolve(&self, did: &Self::Input) -> Result<Self::Output> {
let uri = Builder::from(self.plc_directory_url.clone())
let uri = Builder::from(self.plc_directory_url.parse::<Uri>()?)
.path_and_query(format!("/{}", did.as_str()))
.build()?;
let res = self
Expand Down
7 changes: 1 addition & 6 deletions atrium-oauth/identity/src/did/web_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::DidResolver;
use crate::error::{Error, Result};
use crate::Resolver;
use async_trait::async_trait;
use atrium_api::did_doc::DidDocument;
use atrium_api::types::string::Did;
use atrium_xrpc::http::{header::ACCEPT, Request, Uri};
Expand All @@ -21,14 +20,10 @@ pub struct WebDidResolver<T> {

impl<T> WebDidResolver<T> {
pub fn new(config: WebDidResolverConfig<T>) -> Self {
Self {
http_client: config.http_client,
}
Self { http_client: config.http_client }
}
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<T> Resolver for WebDidResolver<T>
where
T: HttpClient + Send + Sync + 'static,
Expand Down
75 changes: 0 additions & 75 deletions atrium-oauth/identity/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,85 +8,10 @@ mod well_known_resolver;
pub use self::appview_resolver::{AppViewHandleResolver, AppViewHandleResolverConfig};
pub use self::atproto_resolver::{AtprotoHandleResolver, AtprotoHandleResolverConfig};
pub use self::dns_resolver::DnsTxtResolver;
use self::dns_resolver::DynamicDnsTxtResolver;
#[cfg(feature = "doh-handle-resolver")]
pub use self::doh_dns_txt_resolver::{DohDnsTxtResolver, DohDnsTxtResolverConfig};
pub use self::well_known_resolver::{WellKnownHandleResolver, WellKnownHandleResolverConfig};
use crate::error::{Error, Result};
use crate::Resolver;
use async_trait::async_trait;
use atrium_api::types::string::{Did, Handle};
use atrium_xrpc::HttpClient;
use std::sync::Arc;

pub trait HandleResolver: Resolver<Input = Handle, Output = Did> {}

pub struct DynamicHandleResolver {
resolver: Arc<dyn HandleResolver + Send + Sync + 'static>,
}

impl DynamicHandleResolver {
pub fn new(resolver: Arc<dyn HandleResolver + Send + Sync + 'static>) -> Self {
Self { resolver }
}
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl Resolver for DynamicHandleResolver {
type Input = Handle;
type Output = Did;

async fn resolve(&self, handle: &Self::Input) -> Result<Self::Output> {
self.resolver.resolve(handle).await
}
}

impl HandleResolver for DynamicHandleResolver {}

#[derive(Clone)]
pub enum HandleResolverImpl {
Atproto(Arc<dyn DnsTxtResolver + Send + Sync + 'static>),
AppView(String),
}

impl std::fmt::Debug for HandleResolverImpl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HandleResolverImpl::Atproto(_) => write!(f, "Atproto"),
HandleResolverImpl::AppView(url) => write!(f, "AppView({url})"),
}
}
}

#[derive(Clone, Debug)]
pub struct HandleResolverConfig<T> {
pub r#impl: HandleResolverImpl,
pub http_client: Arc<T>,
}

impl<T> TryFrom<HandleResolverConfig<T>> for DynamicHandleResolver
where
T: HttpClient + Send + Sync + 'static,
{
type Error = Error;

fn try_from(config: HandleResolverConfig<T>) -> Result<Self> {
Ok(Self {
resolver: match config.r#impl {
HandleResolverImpl::Atproto(dns_txt_resolver) => {
Arc::new(AtprotoHandleResolver::new(AtprotoHandleResolverConfig {
dns_txt_resolver: DynamicDnsTxtResolver::new(dns_txt_resolver),
http_client: config.http_client,
})?)
}
HandleResolverImpl::AppView(service) => {
Arc::new(AppViewHandleResolver::new(AppViewHandleResolverConfig {
service_url: service,
http_client: config.http_client,
})?)
}
},
})
}
}
14 changes: 4 additions & 10 deletions atrium-oauth/identity/src/handle/appview_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::HandleResolver;
use crate::error::{Error, Result};
use crate::Resolver;
use async_trait::async_trait;
use atrium_api::com::atproto::identity::resolve_handle;
use atrium_api::types::string::{Did, Handle};
use atrium_xrpc::http::uri::Builder;
Expand All @@ -16,21 +15,16 @@ pub struct AppViewHandleResolverConfig<T> {
}

pub struct AppViewHandleResolver<T> {
service_url: Uri,
service_url: String,
http_client: Arc<T>,
}

impl<T> AppViewHandleResolver<T> {
pub fn new(config: AppViewHandleResolverConfig<T>) -> Result<Self> {
Ok(Self {
service_url: config.service_url.parse()?,
http_client: config.http_client,
})
pub fn new(config: AppViewHandleResolverConfig<T>) -> Self {
Self { service_url: config.service_url, http_client: config.http_client }
}
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<T> Resolver for AppViewHandleResolver<T>
where
T: HttpClient + Send + Sync + 'static,
Expand All @@ -39,7 +33,7 @@ where
type Output = Did;

async fn resolve(&self, handle: &Self::Input) -> Result<Self::Output> {
let uri = Builder::from(self.service_url.clone())
let uri = Builder::from(self.service_url.parse::<Uri>()?)
.path_and_query(format!(
"/xrpc/com.atproto.identity.resolveHandle?{}",
serde_html_form::to_string(resolve_handle::ParametersData {
Expand Down
Loading

0 comments on commit dc223cb

Please sign in to comment.