Skip to content

Commit cc9611f

Browse files
committed
Make Credentials.username optional.
Isn't required for token auth.
1 parent b12b16f commit cc9611f

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

core/src/authentication.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl From<AuthenticationError> for Error {
2828
/// The credentials are used to log into the Spotify API.
2929
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
3030
pub struct Credentials {
31-
pub username: String,
31+
pub username: Option<String>,
3232

3333
#[serde(serialize_with = "serialize_protobuf_enum")]
3434
#[serde(deserialize_with = "deserialize_protobuf_enum")]
@@ -51,15 +51,15 @@ impl Credentials {
5151
/// ```
5252
pub fn with_password(username: impl Into<String>, password: impl Into<String>) -> Self {
5353
Self {
54-
username: username.into(),
54+
username: Some(username.into()),
5555
auth_type: AuthenticationType::AUTHENTICATION_USER_PASS,
5656
auth_data: password.into().into_bytes(),
5757
}
5858
}
5959

60-
pub fn with_access_token(username: impl Into<String>, token: impl Into<String>) -> Self {
60+
pub fn with_access_token(token: impl Into<String>) -> Self {
6161
Self {
62-
username: username.into(),
62+
username: None,
6363
auth_type: AuthenticationType::AUTHENTICATION_SPOTIFY_TOKEN,
6464
auth_data: token.into().into_bytes(),
6565
}
@@ -144,9 +144,9 @@ impl Credentials {
144144
let auth_data = read_bytes(&mut cursor)?;
145145

146146
Ok(Self {
147-
username,
148-
auth_type,
149-
auth_data,
147+
username: Some(username),
148+
auth_type: auth_type,
149+
auth_data: auth_data,
150150
})
151151
}
152152
}

core/src/connection/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ pub async fn authenticate(
9999
};
100100

101101
let mut packet = ClientResponseEncrypted::new();
102-
packet
103-
.login_credentials
104-
.mut_or_insert_default()
105-
.set_username(credentials.username);
102+
if let Some(username) = credentials.username {
103+
packet
104+
.login_credentials
105+
.mut_or_insert_default()
106+
.set_username(username);
107+
}
106108
packet
107109
.login_credentials
108110
.mut_or_insert_default()
@@ -144,7 +146,7 @@ pub async fn authenticate(
144146
let welcome_data = APWelcome::parse_from_bytes(data.as_ref())?;
145147

146148
let reusable_credentials = Credentials {
147-
username: welcome_data.canonical_username().to_owned(),
149+
username: Some(welcome_data.canonical_username().to_owned()),
148150
auth_type: welcome_data.reusable_auth_credentials_type(),
149151
auth_data: welcome_data.reusable_auth_credentials().to_owned(),
150152
};

core/src/session.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ impl Session {
162162
}
163163
};
164164

165-
info!("Authenticated as \"{}\" !", reusable_credentials.username);
166-
self.set_username(&reusable_credentials.username);
165+
let username = reusable_credentials.username.as_ref().expect("Username");
166+
info!("Authenticated as \"{}\" !", username);
167+
self.set_username(username);
167168
if let Some(cache) = self.cache() {
168169
if store_credentials {
169170
cache.save_credentials(&reusable_credentials);

examples/get_token.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async fn main() {
3636

3737
// Now create a new session with that token.
3838
let session = Session::new(session_config, None);
39-
let credentials = Credentials::with_access_token(username, token.access_token);
39+
let credentials = Credentials::with_access_token(token.access_token);
4040
println!("Connecting with token..");
4141
match session.connect(credentials, false).await {
4242
Ok(()) => println!("Session username: {:#?}", session.username()),

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ fn get_setup() -> Setup {
10931093
Some(Credentials::with_password(username, password))
10941094
} else {
10951095
match cached_creds {
1096-
Some(creds) if username == creds.username => Some(creds),
1096+
Some(creds) if Some(&username) == creds.username.as_ref() => Some(creds),
10971097
_ => {
10981098
let prompt = &format!("Password for {}: ", username);
10991099
match rpassword::prompt_password(prompt) {

0 commit comments

Comments
 (0)