From a8b86c35abac893e4febc75d447130870eeff568 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 | 12 +++++++----- core/src/session.rs | 5 +++-- examples/get_token.rs | 2 +- src/main.rs | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/core/src/authentication.rs b/core/src/authentication.rs index b2448ab2a..dae217119 100644 --- a/core/src/authentication.rs +++ b/core/src/authentication.rs @@ -29,7 +29,7 @@ impl From for Error { /// The credentials are used to log into the Spotify API. #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] pub struct Credentials { - pub username: String, + pub username: Option, #[serde(serialize_with = "serialize_protobuf_enum")] #[serde(deserialize_with = "deserialize_protobuf_enum")] @@ -52,15 +52,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(), } @@ -145,9 +145,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 4bac6e3e3..7a8eed9c8 100644 --- a/core/src/connection/mod.rs +++ b/core/src/connection/mod.rs @@ -99,10 +99,12 @@ pub async fn authenticate( }; let mut packet = ClientResponseEncrypted::new(); - packet - .login_credentials - .mut_or_insert_default() - .set_username(credentials.username); + if let Some(username) = credentials.username { + packet + .login_credentials + .mut_or_insert_default() + .set_username(username); + } packet .login_credentials .mut_or_insert_default() @@ -144,7 +146,7 @@ pub async fn authenticate( let welcome_data = APWelcome::parse_from_bytes(data.as_ref())?; let reusable_credentials = Credentials { - username: welcome_data.canonical_username().to_owned(), + username: Some(welcome_data.canonical_username().to_owned()), auth_type: welcome_data.reusable_auth_credentials_type(), auth_data: welcome_data.reusable_auth_credentials().to_owned(), }; diff --git a/core/src/session.rs b/core/src/session.rs index b8c55d205..07b193bf3 100755 --- a/core/src/session.rs +++ b/core/src/session.rs @@ -172,8 +172,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 { let cred_changed = cache 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()), diff --git a/src/main.rs b/src/main.rs index 3e656be2c..e9767046d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1095,7 +1095,7 @@ fn get_setup() -> Setup { Some(Credentials::with_password(username, password)) } else { match cached_creds { - Some(creds) if username == creds.username => Some(creds), + Some(creds) if Some(&username) == creds.username.as_ref() => Some(creds), _ => { let prompt = &format!("Password for {username}: "); match rpassword::prompt_password(prompt) {