Skip to content

Commit 3c53e71

Browse files
committed
Update resolvers, add examples for oauth
1 parent dcd91c4 commit 3c53e71

29 files changed

+840
-256
lines changed

Cargo.lock

Lines changed: 170 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ sha2 = "0.10.8"
6161

6262
# Networking
6363
futures = { version = "0.3.30", default-features = false, features = ["alloc"] }
64+
hickory-resolver = "0.24.1"
6465
http = "1.1.0"
6566
tokio = { version = "1.39", default-features = false }
6667

atrium-oauth/oauth-client/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ base64.workspace = true
2121
chrono.workspace = true
2222
ecdsa = { workspace = true, features = ["signing"] }
2323
elliptic-curve = { workspace = true }
24+
futures.workspace = true
2425
jose-jwa.workspace = true
2526
jose-jwk = { workspace = true, features = ["p256"] }
2627
p256 = { workspace = true, features = ["ecdsa"] }
@@ -33,6 +34,7 @@ sha2.workspace = true
3334
thiserror.workspace = true
3435

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

3840
[features]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use atrium_oauth_client::identity::handle::{DnsTxtResolver, HandleResolverImpl};
2+
use atrium_oauth_client::identity::DidResolverConfig;
3+
use atrium_oauth_client::identity::HandleResolverConfig;
4+
use atrium_oauth_client::store::state::MemoryStateStore;
5+
use atrium_oauth_client::{
6+
AtprotoLocalhostClientMetadata, AuthorizeOptions, OAuthClient, OAuthClientConfig,
7+
OAuthResolverConfig,
8+
};
9+
use atrium_xrpc::http::Uri;
10+
use hickory_resolver::TokioAsyncResolver;
11+
use std::io::{stdin, stdout, BufRead, Write};
12+
use std::sync::Arc;
13+
14+
struct HickoryDnsTxtResolver {
15+
resolver: TokioAsyncResolver,
16+
}
17+
18+
impl HickoryDnsTxtResolver {
19+
pub fn new() -> Self {
20+
Self {
21+
resolver: TokioAsyncResolver::tokio_from_system_conf()
22+
.expect("failed to create resolver"),
23+
}
24+
}
25+
}
26+
27+
#[async_trait::async_trait]
28+
impl DnsTxtResolver for HickoryDnsTxtResolver {
29+
async fn resolve(
30+
&self,
31+
query: &str,
32+
) -> core::result::Result<Vec<String>, Box<dyn std::error::Error + Send + Sync + 'static>> {
33+
Ok(self
34+
.resolver
35+
.txt_lookup(query)
36+
.await?
37+
.iter()
38+
.map(|txt| txt.to_string())
39+
.collect())
40+
}
41+
}
42+
43+
#[tokio::main]
44+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
45+
let config = OAuthClientConfig {
46+
client_metadata: AtprotoLocalhostClientMetadata {
47+
redirect_uris: vec!["http://127.0.0.1".to_string()],
48+
},
49+
keys: None,
50+
resolver: OAuthResolverConfig {
51+
did: DidResolverConfig::default(),
52+
handle: HandleResolverConfig {
53+
r#impl: HandleResolverImpl::Atproto(Arc::new(HickoryDnsTxtResolver::new())),
54+
},
55+
},
56+
state_store: MemoryStateStore::default(),
57+
};
58+
let client = OAuthClient::new(config)?;
59+
println!(
60+
"Authorization url: {}",
61+
client
62+
.authorize(
63+
std::env::var("HANDLE").unwrap_or(String::from("https://bsky.social")),
64+
AuthorizeOptions {
65+
scopes: Some(vec![String::from("atproto")]),
66+
..Default::default()
67+
}
68+
)
69+
.await?
70+
);
71+
72+
// Click the URL and sign in,
73+
// then copy and paste the URL like “http://127.0.0.1/?iss=...&code=...” after it is redirected.
74+
75+
print!("Redirected url: ");
76+
stdout().lock().flush()?;
77+
let mut url = String::new();
78+
stdin().lock().read_line(&mut url)?;
79+
80+
let uri = url.trim().parse::<Uri>()?;
81+
let params = serde_html_form::from_str(uri.query().unwrap())?;
82+
println!(
83+
"{}",
84+
serde_json::to_string_pretty(&client.callback(params).await?)?
85+
);
86+
87+
Ok(())
88+
}

atrium-oauth/oauth-client/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub enum Error {
77
#[error(transparent)]
88
Keyset(#[from] crate::keyset::Error),
99
#[error(transparent)]
10-
Resolver(#[from] crate::resolver::Error),
10+
Identity(#[from] crate::identity::Error),
1111
#[error(transparent)]
1212
ServerAgent(#[from] crate::server_agent::Error),
1313
#[error("authorize error: {0}")]

0 commit comments

Comments
 (0)