code cleanup per rust-analyzer suggestions

This commit is contained in:
Grant Limberg 2022-12-09 11:33:10 -08:00
commit 504b5d26cd
No known key found for this signature in database
GPG key ID: 8F2F97D3BE8D7735

View file

@ -150,7 +150,7 @@ impl ZeroIDC {
let redirect = RedirectUrl::new(redir_url.to_string())?; let redirect = RedirectUrl::new(redir_url.to_string())?;
(*idc.inner.lock().unwrap()).oidc_client = Some( idc.inner.lock().unwrap().oidc_client = Some(
CoreClient::from_provider_metadata( CoreClient::from_provider_metadata(
provider_meta, provider_meta,
ClientId::new(client_id.to_string()), ClientId::new(client_id.to_string()),
@ -165,25 +165,25 @@ impl ZeroIDC {
fn kick_refresh_thread(&mut self) { fn kick_refresh_thread(&mut self) {
let local = Arc::clone(&self.inner); let local = Arc::clone(&self.inner);
(*local.lock().unwrap()).kick = true; local.lock().unwrap().kick = true;
} }
fn start(&mut self) { fn start(&mut self) {
let local = Arc::clone(&self.inner); let local = Arc::clone(&self.inner);
if !(*local.lock().unwrap()).running { if !local.lock().unwrap().running {
let inner_local = Arc::clone(&self.inner); let inner_local = Arc::clone(&self.inner);
(*local.lock().unwrap()).oidc_thread = Some(spawn(move || { local.lock().unwrap().oidc_thread = Some(spawn(move || {
(*inner_local.lock().unwrap()).running = true; inner_local.lock().unwrap().running = true;
let mut running = true; let mut running = true;
// Keep a copy of the initial nonce used to get the tokens // Keep a copy of the initial nonce used to get the tokens
// Will be needed later when verifying the responses from refresh tokens // Will be needed later when verifying the responses from refresh tokens
let nonce = (*inner_local.lock().unwrap()).nonce.clone(); let nonce = inner_local.lock().unwrap().nonce.clone();
while running { while running {
let exp = let exp =
UNIX_EPOCH + Duration::from_secs((*inner_local.lock().unwrap()).exp_time); UNIX_EPOCH + Duration::from_secs(inner_local.lock().unwrap().exp_time);
let now = SystemTime::now(); let now = SystemTime::now();
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -200,17 +200,17 @@ impl ZeroIDC {
) )
); );
} }
let refresh_token = (*inner_local.lock().unwrap()).refresh_token.clone(); let refresh_token = inner_local.lock().unwrap().refresh_token.clone();
if let Some(refresh_token) = refresh_token { if let Some(refresh_token) = refresh_token {
let should_kick = (*inner_local.lock().unwrap()).kick; let should_kick = inner_local.lock().unwrap().kick;
if now >= (exp - Duration::from_secs(30)) || should_kick { if now >= (exp - Duration::from_secs(30)) || should_kick {
if should_kick { if should_kick {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {
println!("refresh thread kicked"); println!("refresh thread kicked");
} }
(*inner_local.lock().unwrap()).kick = false; inner_local.lock().unwrap().kick = false;
} }
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -218,10 +218,8 @@ impl ZeroIDC {
println!("Refresh Token: {}", refresh_token.secret()); println!("Refresh Token: {}", refresh_token.secret());
} }
let token_response = (*inner_local.lock().unwrap()) let token_response =
.oidc_client inner_local.lock().unwrap().oidc_client.as_ref().map(|c| {
.as_ref()
.map(|c| {
let res = c let res = c
.exchange_refresh_token(&refresh_token) .exchange_refresh_token(&refresh_token)
.request(http_client); .request(http_client);
@ -254,7 +252,9 @@ impl ZeroIDC {
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let r = client let r = client
.post( .post(
(*inner_local.lock().unwrap()) inner_local
.lock()
.unwrap()
.auth_endpoint .auth_endpoint
.clone(), .clone(),
) )
@ -291,10 +291,10 @@ impl ZeroIDC {
match claims.expiration { match claims.expiration {
Some(exp) => { Some(exp) => {
println!("exp: {}", exp); println!("exp: {}", exp);
(*inner_local inner_local
.lock() .lock()
.unwrap()) .unwrap()
.exp_time = exp; .exp_time = exp;
} }
None => { None => {
panic!("expiration is None. This shouldn't happen") panic!("expiration is None. This shouldn't happen")
@ -304,12 +304,16 @@ impl ZeroIDC {
panic!("error parsing claims"); panic!("error parsing claims");
} }
(*inner_local.lock().unwrap()) inner_local
.lock()
.unwrap()
.access_token = .access_token =
Some(access_token.clone()); Some(access_token.clone());
if let Some(t) = res.refresh_token() { if let Some(t) = res.refresh_token() {
// println!("New Refresh Token: {}", t.secret()); // println!("New Refresh Token: {}", t.secret());
(*inner_local.lock().unwrap()) inner_local
.lock()
.unwrap()
.refresh_token = .refresh_token =
Some(t.clone()); Some(t.clone());
} }
@ -335,10 +339,10 @@ impl ZeroIDC {
} }
} }
(*inner_local.lock().unwrap()) inner_local.lock().unwrap().exp_time =
.exp_time = 0; 0;
(*inner_local.lock().unwrap()) inner_local.lock().unwrap().running =
.running = false; false;
} }
} }
Err(e) => { Err(e) => {
@ -348,29 +352,28 @@ impl ZeroIDC {
e.url().unwrap().as_str() e.url().unwrap().as_str()
); );
println!("Status: {}", e.status().unwrap()); println!("Status: {}", e.status().unwrap());
(*inner_local.lock().unwrap()).exp_time = 0; inner_local.lock().unwrap().exp_time = 0;
(*inner_local.lock().unwrap()).running = inner_local.lock().unwrap().running = false;
false;
} }
} }
} }
None => { None => {
println!("no id token?!?"); println!("no id token?!?");
(*inner_local.lock().unwrap()).exp_time = 0; inner_local.lock().unwrap().exp_time = 0;
(*inner_local.lock().unwrap()).running = false; inner_local.lock().unwrap().running = false;
} }
} }
} }
Err(e) => { Err(e) => {
println!("token error: {}", e); println!("token error: {}", e);
(*inner_local.lock().unwrap()).exp_time = 0; inner_local.lock().unwrap().exp_time = 0;
(*inner_local.lock().unwrap()).running = false; inner_local.lock().unwrap().running = false;
} }
} }
} else { } else {
println!("token response??"); println!("token response??");
(*inner_local.lock().unwrap()).exp_time = 0; inner_local.lock().unwrap().exp_time = 0;
(*inner_local.lock().unwrap()).running = false; inner_local.lock().unwrap().running = false;
} }
} else { } else {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -378,19 +381,19 @@ impl ZeroIDC {
} }
} else { } else {
println!("no refresh token?"); println!("no refresh token?");
(*inner_local.lock().unwrap()).exp_time = 0; inner_local.lock().unwrap().exp_time = 0;
(*inner_local.lock().unwrap()).running = false; inner_local.lock().unwrap().running = false;
} }
sleep(Duration::from_secs(1)); sleep(Duration::from_secs(1));
{ {
running = (*inner_local.lock().unwrap()).running; running = inner_local.lock().unwrap().running;
} }
} }
// end run loop // end run loop
println!("thread done!"); println!("thread done!");
(*inner_local.lock().unwrap()).running = false; inner_local.lock().unwrap().running = false;
println!("set idc thread running flag to false"); println!("set idc thread running flag to false");
})); }));
} }
@ -399,19 +402,19 @@ impl ZeroIDC {
pub fn stop(&mut self) { pub fn stop(&mut self) {
let local = self.inner.clone(); let local = self.inner.clone();
if self.is_running() { if self.is_running() {
(*local.lock().unwrap()).running = false; local.lock().unwrap().running = false;
} }
} }
pub fn is_running(&mut self) -> bool { pub fn is_running(&mut self) -> bool {
let local = Arc::clone(&self.inner); let local = Arc::clone(&self.inner);
let running = (*local.lock().unwrap()).running; let running = local.lock().unwrap().running;
running running
} }
pub fn get_exp_time(&mut self) -> u64 { pub fn get_exp_time(&mut self) -> u64 {
return (*self.inner.lock().unwrap()).exp_time; return self.inner.lock().unwrap().exp_time;
} }
pub fn set_nonce_and_csrf(&mut self, csrf_token: String, nonce: String) { pub fn set_nonce_and_csrf(&mut self, csrf_token: String, nonce: String) {
@ -451,7 +454,6 @@ impl ZeroIDC {
.add_scope(Scope::new("profile".to_string())) .add_scope(Scope::new("profile".to_string()))
.add_scope(Scope::new("email".to_string())) .add_scope(Scope::new("email".to_string()))
.add_scope(Scope::new("offline_access".to_string())) .add_scope(Scope::new("offline_access".to_string()))
.add_scope(Scope::new("openid".to_string()))
.add_scope(Scope::new("groups".to_string())) .add_scope(Scope::new("groups".to_string()))
.set_pkce_challenge(pkce_challenge) .set_pkce_challenge(pkce_challenge)
.url(); .url();
@ -467,7 +469,6 @@ impl ZeroIDC {
.add_scope(Scope::new("profile".to_string())) .add_scope(Scope::new("profile".to_string()))
.add_scope(Scope::new("email".to_string())) .add_scope(Scope::new("email".to_string()))
.add_scope(Scope::new("offline_access".to_string())) .add_scope(Scope::new("offline_access".to_string()))
.add_scope(Scope::new("openid".to_string()))
.set_pkce_challenge(pkce_challenge) .set_pkce_challenge(pkce_challenge)
.url(); .url();