format: paket.login

This commit is contained in:
jane400 2024-09-17 19:24:57 +02:00 committed by jane400
parent 6fbdc6193c
commit 0dc60250e6

View file

@ -6,13 +6,11 @@ use std::{
}; };
use adw::prelude::*; use adw::prelude::*;
use gtk::prelude::*;
use libpaket::{ use libpaket::{
login::{create_nonce, CodeVerfier, DHLIdToken, RefreshToken, TokenResponse}, login::{create_nonce, CodeVerfier, DHLIdToken, RefreshToken, TokenResponse},
LibraryError, LibraryResult, OpenIdClient, LibraryError, LibraryResult, OpenIdClient,
}; };
use relm4::{ use relm4::{
adw, gtk,
prelude::*, prelude::*,
tokio::{sync::Mutex, time::sleep}, tokio::{sync::Mutex, time::sleep},
AsyncComponentSender, SharedState, AsyncComponentSender, SharedState,
@ -119,52 +117,47 @@ impl AsyncComponent for Login {
tracker: 0, tracker: 0,
}; };
{ let result = oo7::Keyring::new().await;
let result = oo7::Keyring::new().await; match result {
match result { Ok(keyring) => {
Ok(keyring) => { KEYRING.set(keyring).unwrap();
KEYRING.set(keyring).unwrap(); if let Err(err) = KEYRING.get().unwrap().unlock().await {
{
let keyring = KEYRING.get().unwrap();
if let Err(err) = keyring.unlock().await {
sender
.output(LoginOutput::KeyringError(err))
.expect("sender not worky");
} else {
match keyring
.search_items(&HashMap::from(KEYRING_ATTRIBUTES))
.await
{
Ok(res) => {
if res.len() > 0 {
let item = &res[0];
let refresh_token = item.secret().await.unwrap();
let refresh_token =
std::str::from_utf8(refresh_token.as_slice()).unwrap();
model.refresh_token = Some(
RefreshToken::new(refresh_token.to_string()).unwrap(),
);
sender.input(LoginInput::NeedsRefresh);
} else {
sender.input(LoginInput::NeedsLogin);
}
}
Err(err) => {
sender
.output(LoginOutput::KeyringError(err))
.expect("sender not worky");
}
};
}
}
}
Err(err) => {
sender sender
.output(LoginOutput::KeyringError(err)) .output(LoginOutput::KeyringError(err))
.expect("sender not worky"); .expect("sender not worky");
} else {
let keyring = KEYRING.get().unwrap();
match keyring
.search_items(&HashMap::from(KEYRING_ATTRIBUTES))
.await
{
Ok(res) => {
if res.len() > 0 {
let item = &res[0];
let refresh_token = item.secret().await.unwrap();
let refresh_token =
std::str::from_utf8(refresh_token.as_slice()).unwrap();
model.refresh_token =
Some(RefreshToken::new(refresh_token.to_string()).unwrap());
sender.input(LoginInput::NeedsRefresh);
} else {
sender.input(LoginInput::NeedsLogin);
}
}
Err(err) => {
sender
.output(LoginOutput::KeyringError(err))
.expect("sender not worky");
}
};
} }
}; }
} Err(err) => {
sender
.output(LoginOutput::KeyringError(err))
.expect("sender not worky");
}
};
let webcontext = WebContext::builder().build(); let webcontext = WebContext::builder().build();
{ {
@ -204,7 +197,10 @@ impl AsyncComponent for Login {
match message { match message {
LoginInput::NeedsRefresh => { LoginInput::NeedsRefresh => {
let refresh_token = self.refresh_token.as_ref().unwrap().clone(); let refresh_token = self.refresh_token.as_ref().unwrap().clone();
sender.oneshot_command(async { use_refresh_token(refresh_token).await }) sender.oneshot_command(async {
let res = use_refresh_token(refresh_token).await;
res
});
} }
LoginInput::ReceivedAuthCode(auth_code) => { LoginInput::ReceivedAuthCode(auth_code) => {
self.set_state(LoginState::Offline); self.set_state(LoginState::Offline);
@ -254,12 +250,6 @@ impl AsyncComponent for Login {
} }
} }
#[derive(PartialEq)]
enum ResponseType {
Retry,
Okay,
}
impl Login { impl Login {
fn construct_request_uri(&self) -> Option<URIRequest> { fn construct_request_uri(&self) -> Option<URIRequest> {
if self.state != LoginState::InFlow { if self.state != LoginState::InFlow {