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 Jan 16, 2023
1 parent 0900aa1 commit d9d69ce
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
14 changes: 7 additions & 7 deletions core/src/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl From<AuthenticationError> 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<String>,

#[serde(serialize_with = "serialize_protobuf_enum")]
#[serde(deserialize_with = "deserialize_protobuf_enum")]
Expand All @@ -51,15 +51,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 @@ -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,
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(),
};
Expand Down
5 changes: 3 additions & 2 deletions core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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

0 comments on commit d9d69ce

Please sign in to comment.