mirror of
https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git
synced 2024-09-28 14:52:15 +00:00
spotify: improve credentials cache
Compare user name case insensitive. Invalidate cache only if login has failed. Keep cache on other error. Write cache file only if it has changed.
This commit is contained in:
parent
05ee55d617
commit
ea26d0d6d5
1 changed files with 26 additions and 7 deletions
|
@ -12,7 +12,8 @@ use gst::glib;
|
||||||
use gst::prelude::*;
|
use gst::prelude::*;
|
||||||
|
|
||||||
use librespot::core::{
|
use librespot::core::{
|
||||||
cache::Cache, config::SessionConfig, session::Session, spotify_id::SpotifyId,
|
cache::Cache, config::SessionConfig, session::Session, session::SessionError,
|
||||||
|
spotify_id::SpotifyId,
|
||||||
};
|
};
|
||||||
use librespot::discovery::Credentials;
|
use librespot::discovery::Credentials;
|
||||||
|
|
||||||
|
@ -132,7 +133,9 @@ impl Settings {
|
||||||
let cache = Cache::new(credentials_cache, None, files_cache, max_size)?;
|
let cache = Cache::new(credentials_cache, None, files_cache, max_size)?;
|
||||||
|
|
||||||
if let Some(cached_cred) = cache.credentials() {
|
if let Some(cached_cred) = cache.credentials() {
|
||||||
if !self.username.is_empty() && self.username != cached_cred.username {
|
if !self.username.is_empty()
|
||||||
|
&& self.username.to_lowercase() != cached_cred.username.to_lowercase()
|
||||||
|
{
|
||||||
gst::debug!(
|
gst::debug!(
|
||||||
cat,
|
cat,
|
||||||
obj: &src,
|
obj: &src,
|
||||||
|
@ -147,16 +150,32 @@ impl Settings {
|
||||||
"reuse cached credentials for user {}",
|
"reuse cached credentials for user {}",
|
||||||
cached_cred.username
|
cached_cred.username
|
||||||
);
|
);
|
||||||
if let Ok((session, _credentials)) = Session::connect(
|
let old_cached_cred = cached_cred.clone();
|
||||||
|
match Session::connect(
|
||||||
SessionConfig::default(),
|
SessionConfig::default(),
|
||||||
cached_cred,
|
cached_cred,
|
||||||
Some(cache.clone()),
|
Some(cache.clone()),
|
||||||
true,
|
false,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
|
Ok((session, credentials)) => {
|
||||||
|
if old_cached_cred.auth_type != credentials.auth_type
|
||||||
|
|| old_cached_cred.auth_data != credentials.auth_data
|
||||||
|
{
|
||||||
|
cache.save_credentials(&credentials);
|
||||||
|
}
|
||||||
return Ok(session);
|
return Ok(session);
|
||||||
}
|
}
|
||||||
|
Err(e) => match e {
|
||||||
|
SessionError::AuthenticationError(ae)
|
||||||
|
if format!("{:?}", ae).starts_with("LoginFailed(") =>
|
||||||
|
{
|
||||||
|
()
|
||||||
|
}
|
||||||
|
_ => return Err(e.into()),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,10 +186,10 @@ impl Settings {
|
||||||
);
|
);
|
||||||
|
|
||||||
if self.username.is_empty() {
|
if self.username.is_empty() {
|
||||||
bail!("username is not set and credentials are not in cache");
|
bail!("username is not set and valid credentials are not in cache");
|
||||||
}
|
}
|
||||||
if self.password.is_empty() {
|
if self.password.is_empty() {
|
||||||
bail!("password is not set and credentials are not in cache");
|
bail!("password is not set and valid credentials are not in cache");
|
||||||
}
|
}
|
||||||
|
|
||||||
let cred = Credentials::with_password(&self.username, &self.password);
|
let cred = Credentials::with_password(&self.username, &self.password);
|
||||||
|
|
Loading…
Reference in a new issue