this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

further ui and auth additions (misc)

+88 -14
+49
src/auth.rs
··· 20 20 session::{ClientData, ClientSessionData}, 21 21 }; 22 22 use keyring::Entry; 23 + use owo_colors::OwoColorize; 23 24 use serde::{Deserialize, Serialize, de::DeserializeOwned}; 24 25 use std::{ 25 26 fmt::Display, ··· 221 222 #[derive(Debug, Serialize, Deserialize)] 222 223 pub struct AuthSession { 223 224 pub did: String, 225 + pub handles: Vec<String>, 224 226 pub session_id: String, 225 227 pub store: StoreMethod, 226 228 pub auth: AuthMethod, ··· 256 258 let session_str = serde_json::to_string(session)?; 257 259 let session_path = self.config_dir.join("session.json"); 258 260 std::fs::write(&session_path, &session_str)?; 261 + 262 + // set file perms on unix for security (just in case) 263 + #[cfg(unix)] 264 + { 265 + use std::fs; 266 + use std::os::unix::fs::PermissionsExt; 267 + 268 + let perms = fs::Permissions::from_mode(0o0600); // -rw------- 269 + fs::set_permissions(&session_path, perms)?; 270 + } 271 + 259 272 Ok(()) 260 273 } 261 274 ··· 528 541 Ok(did) 529 542 } 530 543 544 + async fn resolve_handles(&self, ident: &str) -> Result<Vec<Handle<'_>>, OnyxError> { 545 + if let Ok(handle) = ident.parse() { 546 + return Ok(vec![handle]); 547 + } 548 + 549 + let did = Did::new(ident)?; 550 + let did_doc = self.resolver.resolve_did_doc(&did).await?; 551 + let doc = did_doc.parse()?; 552 + Ok(doc.handles()) 553 + } 554 + 531 555 pub async fn login( 532 556 &self, 533 557 ident: &str, 534 558 store: StoreMethod, 535 559 password: Option<String>, 536 560 ) -> Result<(), OnyxError> { 561 + // ensure previous creds are cleared 562 + let _ = self.logout().await; 563 + 537 564 match password { 538 565 Some(pass) => self.login_app_password(ident, store, pass).await, 539 566 None => self.login_oauth(ident, store).await, ··· 549 576 let session_id = "session"; 550 577 let resolver = PublicResolver::default(); 551 578 579 + let handles = self 580 + .resolve_handles(ident) 581 + .await 582 + .unwrap_or(vec![]) 583 + .iter() 584 + .map(|h| h.to_string()) 585 + .collect(); 586 + 552 587 // TODO: See if there's a clean way to fix this duplication 553 588 554 589 if store_method == StoreMethod::Keyring { ··· 566 601 .await?; 567 602 let auth_session = AuthSession { 568 603 did: auth.did.to_string(), 604 + handles, 569 605 session_id: session_id.to_string(), 570 606 store: store_method, 571 607 auth: AuthMethod::AppPassword, ··· 586 622 .await?; 587 623 let auth_session = AuthSession { 588 624 did: auth.did.to_string(), 625 + handles, 589 626 session_id: session_id.to_string(), 590 627 store: store_method, 591 628 auth: AuthMethod::AppPassword, ··· 604 641 config: AtprotoClientMetadata::default_localhost(), 605 642 }; 606 643 644 + let handles = self 645 + .resolve_handles(ident) 646 + .await 647 + .unwrap_or(vec![]) 648 + .iter() 649 + .map(|h| h.to_string()) 650 + .collect(); 651 + 607 652 // There's probably a better way of doing this to avoid duplication, 608 653 // but stores aren't dyn-compatible, and I couldn't be bothered 609 654 if store_method == StoreMethod::Keyring { ··· 616 661 let session_id = session.data.try_read()?.session_id.clone(); 617 662 let auth_session = AuthSession { 618 663 did: did.to_string(), 664 + handles, 619 665 session_id: session_id.to_string(), 620 666 store: store_method, 621 667 auth: AuthMethod::OAuth, ··· 631 677 let session_id = session.data.try_read()?.session_id.clone(); 632 678 let auth_session = AuthSession { 633 679 did: did.to_string(), 680 + handles, 634 681 session_id: session_id.to_string(), 635 682 store: store_method, 636 683 auth: AuthMethod::OAuth, ··· 713 760 return Err(OnyxError::Auth("not logged in".to_string())); 714 761 } 715 762 }; 763 + 764 + println!("{}", format!("logging out {}", &session.did).dimmed()); 716 765 717 766 let did = Did::new(&session.did)?; 718 767
+39 -14
src/main.rs
··· 1 - use jacquard::{prelude::IdentityResolver, types::did::Did}; 2 1 use owo_colors::OwoColorize; 3 2 use serde::{Deserialize, Serialize}; 4 3 use std::path::PathBuf; ··· 194 193 } => { 195 194 let auth = get_auth()?; 196 195 auth.login(&handle, store, password).await?; 196 + 197 + let session_info = auth.get_session_info()?; 198 + 199 + println!( 200 + "{}: logged in {}{}", 201 + "success".green().bold(), 202 + (session_info 203 + .handles 204 + .first() 205 + .unwrap_or(&"(no handle)".to_string())) 206 + .magenta(), 207 + format!(", {}", session_info.did).dimmed() 208 + ); 197 209 } 198 210 AuthCommands::Logout => { 199 211 let auth = get_auth()?; 212 + let session_info = auth.get_session_info()?; 213 + 200 214 auth.logout().await?; 215 + 216 + println!( 217 + "{}: logged out {}{}", 218 + "success".green().bold(), 219 + (session_info 220 + .handles 221 + .first() 222 + .unwrap_or(&"(no handle)".to_string())) 223 + .magenta(), 224 + format!(", {}", session_info.did).dimmed(), 225 + ); 201 226 } 202 227 AuthCommands::Whoami => { 203 228 let auth = get_auth()?; 204 - let session_info = auth.get_session_info()?; 205 229 let session = auth.restore().await; 230 + let session_info = auth.get_session_info()?; 206 231 207 232 let method_str = if session_info.auth == AuthMethod::OAuth { 208 233 "oauth" ··· 210 235 "app password" 211 236 }; 212 237 213 - if let Ok(session) = session { 238 + if session.is_ok() { 214 239 println!( 215 240 "{} {} {} {}", 216 241 "status:".dimmed(), ··· 218 243 "via".dimmed(), 219 244 method_str.blue() 220 245 ); 221 - 222 - let did_doc = session.resolve_did_doc(&Did::new(&session_info.did)?).await; 223 - if let Ok(did_doc) = did_doc 224 - && let Ok(doc) = did_doc.parse() 225 - { 226 - print!("{}", "handles: ".dimmed()); 227 - for handle in doc.handles() { 228 - print!("{} ", handle.magenta()); 229 - } 230 - println!(); 231 - } 232 246 } else { 233 247 println!( 234 248 "{} {} {} {}", ··· 237 251 "via".dimmed(), 238 252 method_str.blue() 239 253 ); 254 + } 255 + 256 + print!("{} ", "handles:".dimmed()); 257 + 258 + if session_info.handles.is_empty() { 259 + println!("{}", "(no handle)".magenta()); 260 + } else { 261 + for handle in &session_info.handles { 262 + print!("{} ", handle.magenta()); 263 + } 264 + println!(); 240 265 } 241 266 242 267 println!("{}", format!("did: {}", session_info.did).dimmed());