Skip to content

Commit

Permalink
Update resolvers, add examples for oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Sep 6, 2024
1 parent dcd91c4 commit 3c53e71
Show file tree
Hide file tree
Showing 29 changed files with 840 additions and 256 deletions.
172 changes: 170 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ sha2 = "0.10.8"

# Networking
futures = { version = "0.3.30", default-features = false, features = ["alloc"] }
hickory-resolver = "0.24.1"
http = "1.1.0"
tokio = { version = "1.39", default-features = false }

Expand Down
2 changes: 2 additions & 0 deletions atrium-oauth/oauth-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ base64.workspace = true
chrono.workspace = true
ecdsa = { workspace = true, features = ["signing"] }
elliptic-curve = { workspace = true }
futures.workspace = true
jose-jwa.workspace = true
jose-jwk = { workspace = true, features = ["p256"] }
p256 = { workspace = true, features = ["ecdsa"] }
Expand All @@ -33,6 +34,7 @@ sha2.workspace = true
thiserror.workspace = true

[dev-dependencies]
hickory-resolver.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

[features]
Expand Down
88 changes: 88 additions & 0 deletions atrium-oauth/oauth-client/examples/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use atrium_oauth_client::identity::handle::{DnsTxtResolver, HandleResolverImpl};
use atrium_oauth_client::identity::DidResolverConfig;
use atrium_oauth_client::identity::HandleResolverConfig;
use atrium_oauth_client::store::state::MemoryStateStore;
use atrium_oauth_client::{
AtprotoLocalhostClientMetadata, AuthorizeOptions, OAuthClient, OAuthClientConfig,
OAuthResolverConfig,
};
use atrium_xrpc::http::Uri;
use hickory_resolver::TokioAsyncResolver;
use std::io::{stdin, stdout, BufRead, Write};
use std::sync::Arc;

struct HickoryDnsTxtResolver {
resolver: TokioAsyncResolver,
}

impl HickoryDnsTxtResolver {
pub fn new() -> Self {
Self {
resolver: TokioAsyncResolver::tokio_from_system_conf()
.expect("failed to create resolver"),
}
}
}

#[async_trait::async_trait]
impl DnsTxtResolver for HickoryDnsTxtResolver {
async fn resolve(
&self,
query: &str,
) -> core::result::Result<Vec<String>, Box<dyn std::error::Error + Send + Sync + 'static>> {
Ok(self
.resolver
.txt_lookup(query)
.await?
.iter()
.map(|txt| txt.to_string())
.collect())
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = OAuthClientConfig {
client_metadata: AtprotoLocalhostClientMetadata {
redirect_uris: vec!["http://127.0.0.1".to_string()],
},
keys: None,
resolver: OAuthResolverConfig {
did: DidResolverConfig::default(),
handle: HandleResolverConfig {
r#impl: HandleResolverImpl::Atproto(Arc::new(HickoryDnsTxtResolver::new())),
},
},
state_store: MemoryStateStore::default(),
};
let client = OAuthClient::new(config)?;
println!(
"Authorization url: {}",
client
.authorize(
std::env::var("HANDLE").unwrap_or(String::from("https://bsky.social")),
AuthorizeOptions {
scopes: Some(vec![String::from("atproto")]),
..Default::default()
}
)
.await?
);

// Click the URL and sign in,
// then copy and paste the URL like “http://127.0.0.1/?iss=...&code=...” after it is redirected.

print!("Redirected url: ");
stdout().lock().flush()?;
let mut url = String::new();
stdin().lock().read_line(&mut url)?;

let uri = url.trim().parse::<Uri>()?;
let params = serde_html_form::from_str(uri.query().unwrap())?;
println!(
"{}",
serde_json::to_string_pretty(&client.callback(params).await?)?
);

Ok(())
}
2 changes: 1 addition & 1 deletion atrium-oauth/oauth-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum Error {
#[error(transparent)]
Keyset(#[from] crate::keyset::Error),
#[error(transparent)]
Resolver(#[from] crate::resolver::Error),
Identity(#[from] crate::identity::Error),
#[error(transparent)]
ServerAgent(#[from] crate::server_agent::Error),
#[error("authorize error: {0}")]
Expand Down
Loading

0 comments on commit 3c53e71

Please sign in to comment.