Skip to content

Commit

Permalink
Make Credentials.username optional.
Browse files Browse the repository at this point in the history
Isn't required for token auth.
  • Loading branch information
kingosticks committed Jul 31, 2024
1 parent 2053d6e commit a8b86c3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
14 changes: 7 additions & 7 deletions core/src/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl From<AuthenticationError> 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<String>,

#[serde(serialize_with = "serialize_protobuf_enum")]
#[serde(deserialize_with = "deserialize_protobuf_enum")]
Expand All @@ -52,15 +52,15 @@ impl Credentials {
/// ```
pub fn with_password(username: impl Into<String>, password: impl Into<String>) -> 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<String>, token: impl Into<String>) -> Self {
pub fn with_access_token(token: impl Into<String>) -> Self {
Self {
username: username.into(),
username: None,
auth_type: AuthenticationType::AUTHENTICATION_SPOTIFY_TOKEN,
auth_data: token.into().into_bytes(),
}
Expand Down Expand Up @@ -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,
})
}
}
Expand Down
12 changes: 7 additions & 5 deletions core/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(),
};
Expand Down
5 changes: 3 additions & 2 deletions core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/get_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a8b86c3

Please sign in to comment.