|
| 1 | +use atrium_api::{agent::Agent, chat::bsky::convo::get_messages, types::string::Datetime}; |
| 2 | +use atrium_common::store::memory::MemoryMapStore; |
| 3 | +use atrium_identity::{ |
| 4 | + did::{CommonDidResolver, CommonDidResolverConfig, DEFAULT_PLC_DIRECTORY_URL}, |
| 5 | + handle::{AtprotoHandleResolver, AtprotoHandleResolverConfig}, |
| 6 | +}; |
| 7 | +use atrium_oauth_client::store::session::Session; |
| 8 | +use atrium_oauth_client::store::state::InternalStateData; |
| 9 | +use atrium_oauth_client::store::state::MemoryStateStore; |
| 10 | +use atrium_oauth_client::AtprotoLocalhostClientMetadata; |
| 11 | +use atrium_oauth_client::AuthorizeOptions; |
| 12 | +use atrium_oauth_client::DefaultHttpClient; |
| 13 | +use atrium_oauth_client::OAuthClient; |
| 14 | +use atrium_oauth_client::OAuthClientConfig; |
| 15 | +use atrium_oauth_client::OAuthResolverConfig; |
| 16 | +use jose_jwk::JwkSet; |
| 17 | +use std::sync::Arc; |
| 18 | + |
| 19 | +struct HickoryDnsTxtResolver { |
| 20 | + resolver: hickory_resolver::TokioAsyncResolver, |
| 21 | +} |
| 22 | + |
| 23 | +impl Default for HickoryDnsTxtResolver { |
| 24 | + fn default() -> Self { |
| 25 | + Self { |
| 26 | + resolver: hickory_resolver::TokioAsyncResolver::tokio_from_system_conf() |
| 27 | + .expect("failed to create resolver"), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl atrium_identity::handle::DnsTxtResolver for HickoryDnsTxtResolver { |
| 33 | + async fn resolve( |
| 34 | + &self, |
| 35 | + query: &str, |
| 36 | + ) -> core::result::Result<Vec<String>, Box<dyn std::error::Error + Send + Sync + 'static>> { |
| 37 | + Ok(self.resolver.txt_lookup(query).await?.iter().map(|txt| txt.to_string()).collect()) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +fn keyset() -> JwkSet { |
| 42 | + serde_json::from_value(serde_json::json!({ |
| 43 | + "keys": [ |
| 44 | + { |
| 45 | + "kty": "EC", |
| 46 | + "crv": "P-256", |
| 47 | + "x": "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4", |
| 48 | + "y": "4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM", |
| 49 | + "use": "enc", |
| 50 | + "kid": "some-ec-kid" |
| 51 | + }, |
| 52 | + { |
| 53 | + "kty": "RSA", |
| 54 | + "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtV\ |
| 55 | + T86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5\ |
| 56 | + JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMic\ |
| 57 | + AtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bF\ |
| 58 | + TWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-\ |
| 59 | + kEgU8awapJzKnqDKgw", |
| 60 | + "e": "AQAB", |
| 61 | + "alg": "RS256", |
| 62 | + "kid": "some-rsa-kid" |
| 63 | + } |
| 64 | + ] |
| 65 | + })) |
| 66 | + .unwrap() |
| 67 | +} |
| 68 | + |
| 69 | +fn config() -> OAuthClientConfig< |
| 70 | + MemoryMapStore<String, InternalStateData>, |
| 71 | + MemoryMapStore<String, Session>, |
| 72 | + AtprotoLocalhostClientMetadata, |
| 73 | + CommonDidResolver<DefaultHttpClient>, |
| 74 | + AtprotoHandleResolver<HickoryDnsTxtResolver, DefaultHttpClient>, |
| 75 | +> { |
| 76 | + let http_client = Arc::new(DefaultHttpClient::default()); |
| 77 | + |
| 78 | + OAuthClientConfig { |
| 79 | + client_metadata: AtprotoLocalhostClientMetadata { |
| 80 | + redirect_uris: vec!["http://127.0.0.1".to_string()], |
| 81 | + }, |
| 82 | + // keys: Some(keyset().keys).filter(|_| true), |
| 83 | + keys: None, |
| 84 | + resolver: OAuthResolverConfig { |
| 85 | + did_resolver: CommonDidResolver::new(CommonDidResolverConfig { |
| 86 | + plc_directory_url: DEFAULT_PLC_DIRECTORY_URL.to_string(), |
| 87 | + http_client: http_client.clone(), |
| 88 | + }), |
| 89 | + handle_resolver: AtprotoHandleResolver::new(AtprotoHandleResolverConfig { |
| 90 | + dns_txt_resolver: HickoryDnsTxtResolver::default(), |
| 91 | + http_client: http_client.clone(), |
| 92 | + }), |
| 93 | + authorization_server_metadata: Default::default(), |
| 94 | + protected_resource_metadata: Default::default(), |
| 95 | + }, |
| 96 | + state_store: MemoryStateStore::default(), |
| 97 | + session_store: MemoryMapStore::default(), |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[tokio::main] |
| 102 | +async fn main() { |
| 103 | + use std::io::{stdin, stdout, BufRead, Write}; |
| 104 | + |
| 105 | + use atrium_xrpc::http::Uri; |
| 106 | + |
| 107 | + let client = OAuthClient::new(config()).expect("OAuthClient"); |
| 108 | + |
| 109 | + let handle = std::env::var("HANDLE").unwrap_or(String::from("https://bsky.social")); |
| 110 | + let options = |
| 111 | + AuthorizeOptions { scopes: Some(vec!["atproto".to_string()]), ..Default::default() }; |
| 112 | + |
| 113 | + println!( |
| 114 | + "Authorization URL: {}", |
| 115 | + client.authorize(handle, options).await.expect("Authorization") |
| 116 | + ); |
| 117 | + |
| 118 | + print!("Redirect url: "); |
| 119 | + stdout().lock().flush().unwrap(); |
| 120 | + let mut url = String::new(); |
| 121 | + stdin().lock().read_line(&mut url).unwrap(); |
| 122 | + |
| 123 | + let uri = url.trim().parse::<Uri>().expect("Uri"); |
| 124 | + let params = serde_html_form::from_str(uri.query().unwrap()).expect("serde_html_form"); |
| 125 | + let session = client.callback(params).await.expect("callback"); |
| 126 | + |
| 127 | + let inner = session.get_session(true).await.expect("get_session"); |
| 128 | + let expires_at = inner.token_set.expires_at.expect("expires_at"); |
| 129 | + println!( |
| 130 | + "expires_at: {:?}", |
| 131 | + expires_at.as_ref().signed_duration_since(Datetime::now().as_ref()).num_seconds() |
| 132 | + ); |
| 133 | + |
| 134 | + let agent = Agent::new(session); |
| 135 | + let get_session = agent.api.chat.bsky.convo.get_messages( |
| 136 | + get_messages::ParametersData { convo_id: "convo".to_owned(), cursor: None, limit: None } |
| 137 | + .into(), |
| 138 | + ).await; |
| 139 | + |
| 140 | + println!("get_session: {:?}", get_session); |
| 141 | +} |
0 commit comments