bah
This commit is contained in:
parent
055df38c32
commit
34bec8fdae
1 changed files with 57 additions and 62 deletions
119
src/ui/setup.rs
119
src/ui/setup.rs
|
@ -67,8 +67,12 @@ mod imp {
|
||||||
|
|
||||||
#[template_callback]
|
#[template_callback]
|
||||||
pub(super) async fn on_authn_validate_activated(&self) {
|
pub(super) async fn on_authn_validate_activated(&self) {
|
||||||
|
assert!(self.obj().authn_can_validate());
|
||||||
self.obj().set_authn_can_validate(false);
|
self.obj().set_authn_can_validate(false);
|
||||||
|
|
||||||
|
assert!(self.obj().authn_can_edit());
|
||||||
self.obj().set_authn_can_edit(false);
|
self.obj().set_authn_can_edit(false);
|
||||||
|
|
||||||
self.obj().set_status("Connecting...");
|
self.obj().set_status("Connecting...");
|
||||||
|
|
||||||
let api = match crate::subsonic::Client::with_password(
|
let api = match crate::subsonic::Client::with_password(
|
||||||
|
@ -85,72 +89,62 @@ mod imp {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match api.ping().await {
|
if let Err(err) = api.ping().await {
|
||||||
Ok(()) => {
|
self.obj().set_status(format!("Error: {err}"));
|
||||||
self.obj().set_status("Connected");
|
self.obj().set_authn_can_validate(true);
|
||||||
self.save().await;
|
self.obj().set_authn_can_edit(true);
|
||||||
|
return;
|
||||||
// please REMOVEME once we've killed vala
|
|
||||||
fn get_random_salt(length: usize) -> String {
|
|
||||||
use rand::Rng;
|
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
std::iter::repeat(())
|
|
||||||
// 0.9: s/distributions/distr
|
|
||||||
.map(|()| rng.sample(rand::distributions::Alphanumeric))
|
|
||||||
.map(char::from)
|
|
||||||
.take(length)
|
|
||||||
.collect::<String>()
|
|
||||||
}
|
|
||||||
let new_salt = get_random_salt(8);
|
|
||||||
use md5::Digest;
|
|
||||||
let mut hasher = md5::Md5::new();
|
|
||||||
hasher.update(self.obj().password().as_bytes());
|
|
||||||
hasher.update(new_salt.as_bytes());
|
|
||||||
let new_token_bytes = hasher.finalize();
|
|
||||||
let new_token = base16ct::lower::encode_string(&new_token_bytes);
|
|
||||||
|
|
||||||
let vala_api = crate::subsonic_vala::Client::with_token(
|
|
||||||
&self.obj().server_url(),
|
|
||||||
&self.obj().username(),
|
|
||||||
&new_token,
|
|
||||||
&new_salt,
|
|
||||||
);
|
|
||||||
self.obj().emit_by_name::<()>("connected", &[&vala_api]);
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
self.obj().set_status(format!("Error: {err}"));
|
|
||||||
self.obj().set_authn_can_validate(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.obj().set_authn_can_edit(true);
|
self.obj().set_status("Connected");
|
||||||
}
|
|
||||||
|
|
||||||
async fn save(&self) {
|
|
||||||
assert!(self.obj().authn_can_edit());
|
|
||||||
self.obj().set_authn_can_edit(false);
|
self.obj().set_authn_can_edit(false);
|
||||||
|
|
||||||
async fn wrapper(self_: &Setup) -> Result<(), impl std::error::Error> {
|
// FIXME: remove unwraps
|
||||||
let keyring = oo7::Keyring::new().await?;
|
let keyring = oo7::Keyring::new().await.unwrap();
|
||||||
let mut attributes = vec![("xdg:schema", crate::APP_ID.to_string())];
|
let mut attributes = vec![("xdg:schema", crate::APP_ID.to_string())];
|
||||||
// clear previous passwords
|
// clear previous passwords
|
||||||
keyring.delete(&attributes).await?;
|
keyring.delete(&attributes).await.unwrap();
|
||||||
|
|
||||||
attributes.push(("server-url", self_.obj().server_url()));
|
attributes.push(("server-url", self.obj().server_url()));
|
||||||
attributes.push(("username", self_.obj().username()));
|
attributes.push(("username", self.obj().username()));
|
||||||
keyring
|
keyring
|
||||||
.create_item(
|
.create_item(
|
||||||
"Audrey Subsonic password",
|
"Audrey Subsonic password",
|
||||||
&attributes,
|
&attributes,
|
||||||
self_.obj().password(),
|
self.obj().password(),
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// please REMOVEME once we've killed vala
|
||||||
|
fn get_random_salt(length: usize) -> String {
|
||||||
|
use rand::Rng;
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
std::iter::repeat(())
|
||||||
|
// 0.9: s/distributions/distr
|
||||||
|
.map(|()| rng.sample(rand::distributions::Alphanumeric))
|
||||||
|
.map(char::from)
|
||||||
|
.take(length)
|
||||||
|
.collect::<String>()
|
||||||
}
|
}
|
||||||
|
let new_salt = get_random_salt(8);
|
||||||
|
use md5::Digest;
|
||||||
|
let mut hasher = md5::Md5::new();
|
||||||
|
hasher.update(self.obj().password().as_bytes());
|
||||||
|
hasher.update(new_salt.as_bytes());
|
||||||
|
let new_token_bytes = hasher.finalize();
|
||||||
|
let new_token = base16ct::lower::encode_string(&new_token_bytes);
|
||||||
|
|
||||||
let result = wrapper(self).await;
|
let vala_api = crate::subsonic_vala::Client::with_token(
|
||||||
|
&self.obj().server_url(),
|
||||||
|
&self.obj().username(),
|
||||||
|
&new_token,
|
||||||
|
&new_salt,
|
||||||
|
);
|
||||||
self.obj().set_authn_can_edit(true);
|
self.obj().set_authn_can_edit(true);
|
||||||
result.unwrap(); // FIXME
|
|
||||||
|
self.obj().emit_by_name::<()>("connected", &[&vala_api]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -170,8 +164,11 @@ impl Setup {
|
||||||
#[weak(rename_to = self_)]
|
#[weak(rename_to = self_)]
|
||||||
self,
|
self,
|
||||||
async move {
|
async move {
|
||||||
assert!(self_.authn_can_edit())
|
self_.set_server_url("");
|
||||||
|
self_.set_username("");
|
||||||
|
self_.set_password("");
|
||||||
self_.set_authn_can_edit(false);
|
self_.set_authn_can_edit(false);
|
||||||
|
self_.set_authn_can_validate(false);
|
||||||
|
|
||||||
// TODO remove unwraps, make sure authn_can_edit is set back to true
|
// TODO remove unwraps, make sure authn_can_edit is set back to true
|
||||||
let keyring = oo7::Keyring::new().await.unwrap();
|
let keyring = oo7::Keyring::new().await.unwrap();
|
||||||
|
@ -180,9 +177,6 @@ impl Setup {
|
||||||
|
|
||||||
if items.is_empty() {
|
if items.is_empty() {
|
||||||
// didn't find shit, leave all empty
|
// didn't find shit, leave all empty
|
||||||
self_.set_server_url("");
|
|
||||||
self_.set_username("");
|
|
||||||
self_.set_password("");
|
|
||||||
// TODO: onboarding
|
// TODO: onboarding
|
||||||
self_.set_authn_can_edit(true);
|
self_.set_authn_can_edit(true);
|
||||||
self_.set_authn_can_validate(true);
|
self_.set_authn_can_validate(true);
|
||||||
|
@ -196,6 +190,7 @@ impl Setup {
|
||||||
self_.set_password(String::from_utf8_lossy(&item.secret().await.unwrap()));
|
self_.set_password(String::from_utf8_lossy(&item.secret().await.unwrap()));
|
||||||
|
|
||||||
// first connection
|
// first connection
|
||||||
|
self_.set_authn_can_edit(true); // act as if the user had inputted this
|
||||||
self_.set_authn_can_validate(true);
|
self_.set_authn_can_validate(true);
|
||||||
self_.imp().on_authn_validate_activated().await;
|
self_.imp().on_authn_validate_activated().await;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue