Skip to content

Commit

Permalink
adding callback for reusable credentials (librespot-org#983)
Browse files Browse the repository at this point in the history
This allows more control over how the credentials are saved to the cache
  • Loading branch information
Louis9902 authored May 20, 2022
1 parent 1efda79 commit 6c2491b
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [playback] `Sink`: `write()` now receives ownership of the packet (breaking).
- [playback] `pipe`: create file if it doesn't already exist
- [playback] More robust dynamic limiter for very wide dynamic range (breaking)
- [core] `Session`: `connect()` now returns the long-term credentials.
- [core] `Session`: `connect()` now accespt a flag if the credentails should be stored via the cache.
- [build] The MSRV is now 1.53.

### Added
Expand Down
11 changes: 7 additions & 4 deletions core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ impl Session {
config: SessionConfig,
credentials: Credentials,
cache: Option<Cache>,
) -> Result<Session, SessionError> {
store_credentials: bool,
) -> Result<(Session, Credentials), SessionError> {
let ap = apresolve(config.proxy.as_ref(), config.ap_port).await;

info!("Connecting to AP \"{}\"", ap);
Expand All @@ -76,18 +77,20 @@ impl Session {
connection::authenticate(&mut conn, credentials, &config.device_id).await?;
info!("Authenticated as \"{}\" !", reusable_credentials.username);
if let Some(cache) = &cache {
cache.save_credentials(&reusable_credentials);
if store_credentials {
cache.save_credentials(&reusable_credentials);
}
}

let session = Session::create(
conn,
config,
cache,
reusable_credentials.username,
reusable_credentials.username.clone(),
tokio::runtime::Handle::current(),
);

Ok(session)
Ok((session, reusable_credentials))
}

fn create(
Expand Down
1 change: 1 addition & 0 deletions core/tests/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async fn test_connection() {
SessionConfig::default(),
Credentials::with_password("test", "test"),
None,
false,
)
.await;

Expand Down
2 changes: 1 addition & 1 deletion examples/get_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() {

println!("Connecting..");
let credentials = Credentials::with_password(&args[1], &args[2]);
let session = Session::connect(session_config, credentials, None)
let (session, _) = Session::connect(session_config, credentials, None, false)
.await
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion examples/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn main() {
let backend = audio_backend::find(None).unwrap();

println!("Connecting ..");
let session = Session::connect(session_config, credentials, None)
let (session, _) = Session::connect(session_config, credentials, None, false)
.await
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion examples/playlist_tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn main() {
process::exit(1);
});

let session = Session::connect(session_config, credentials, None)
let (session, _) = Session::connect(session_config, credentials, None, false)
.await
.unwrap();

Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,7 @@ async fn main() {
setup.session_config.clone(),
credentials,
setup.cache.clone(),
true,
)
.fuse(),
);
Expand Down Expand Up @@ -1634,6 +1635,7 @@ async fn main() {
setup.session_config.clone(),
credentials,
setup.cache.clone(),
true,
).fuse());
},
None => {
Expand All @@ -1643,7 +1645,7 @@ async fn main() {
}
},
session = &mut connecting, if !connecting.is_terminated() => match session {
Ok(session) => {
Ok((session,_)) => {
let mixer_config = setup.mixer_config.clone();
let mixer = (setup.mixer)(mixer_config);
let player_config = setup.player_config.clone();
Expand Down Expand Up @@ -1711,6 +1713,7 @@ async fn main() {
setup.session_config.clone(),
credentials,
setup.cache.clone(),
true
).fuse());
},
_ => {
Expand Down

0 comments on commit 6c2491b

Please sign in to comment.