From d9d69cebf9ab6040dd275c82ae44b1acc4b65c8b Mon Sep 17 00:00:00 2001 From: Nick Steel Date: Mon, 16 Jan 2023 01:34:41 +0000 Subject: [PATCH] Make Credentials.username optional. Isn't required for token auth. --- core/src/authentication.rs | 14 +++++++------- core/src/connection/mod.rs | 8 ++++---- core/src/session.rs | 5 +++-- examples/get_token.rs | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/core/src/authentication.rs b/core/src/authentication.rs index 4fa4da815..195d6dd20 100644 --- a/core/src/authentication.rs +++ b/core/src/authentication.rs @@ -28,7 +28,7 @@ impl From for Error { /// The credentials are used to log into the Spotify API. #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct Credentials { - pub username: String, + pub username: Option, #[serde(serialize_with = "serialize_protobuf_enum")] #[serde(deserialize_with = "deserialize_protobuf_enum")] @@ -51,15 +51,15 @@ impl Credentials { /// ``` pub fn with_password(username: impl Into, password: impl Into) -> Self { Self { - username: username.into(), + username: Some(username.into()), auth_type: AuthenticationType::AUTHENTICATION_USER_PASS, auth_data: password.into().into_bytes(), } } - pub fn with_access_token(username: impl Into, token: impl Into) -> Self { + pub fn with_access_token(token: impl Into) -> Self { Self { - username: username.into(), + username: None, auth_type: AuthenticationType::AUTHENTICATION_SPOTIFY_TOKEN, auth_data: token.into().into_bytes(), } @@ -144,9 +144,9 @@ impl Credentials { let auth_data = read_bytes(&mut cursor)?; Ok(Self { - username, - auth_type, - auth_data, + username: Some(username), + auth_type: auth_type, + auth_data: auth_data, }) } } diff --git a/core/src/connection/mod.rs b/core/src/connection/mod.rs index a0ea8b797..d3efd7cb6 100644 --- a/core/src/connection/mod.rs +++ b/core/src/connection/mod.rs @@ -99,9 +99,9 @@ pub async fn authenticate( }; let mut packet = ClientResponseEncrypted::new(); - packet - .mut_login_credentials() - .set_username(credentials.username); + if let Some(username) = credentials.username { + packet.mut_login_credentials().set_username(username); + } packet .mut_login_credentials() .set_typ(credentials.auth_type); @@ -136,7 +136,7 @@ pub async fn authenticate( let welcome_data = APWelcome::parse_from_bytes(data.as_ref())?; let reusable_credentials = Credentials { - username: welcome_data.get_canonical_username().to_owned(), + username: Some(welcome_data.get_canonical_username().to_owned()), auth_type: welcome_data.get_reusable_auth_credentials_type(), auth_data: welcome_data.get_reusable_auth_credentials().to_owned(), }; diff --git a/core/src/session.rs b/core/src/session.rs index 1936467ee..7c3c1740b 100644 --- a/core/src/session.rs +++ b/core/src/session.rs @@ -162,8 +162,9 @@ impl Session { } }; - info!("Authenticated as \"{}\" !", reusable_credentials.username); - self.set_username(&reusable_credentials.username); + let username = reusable_credentials.username.as_ref().expect("Username"); + info!("Authenticated as \"{}\" !", username); + self.set_username(username); if let Some(cache) = self.cache() { if store_credentials { cache.save_credentials(&reusable_credentials); diff --git a/examples/get_token.rs b/examples/get_token.rs index 8a9cd5730..edc0a6026 100644 --- a/examples/get_token.rs +++ b/examples/get_token.rs @@ -36,7 +36,7 @@ async fn main() { // Now create a new session with that token. let session = Session::new(session_config, None); - let credentials = Credentials::with_access_token(username, token.access_token); + let credentials = Credentials::with_access_token(token.access_token); println!("Connecting with token.."); match session.connect(credentials, false).await { Ok(()) => println!("Session username: {:#?}", session.username()),