Skip to content

Commit 530bafe

Browse files
kingosticksNick Steel
authored and
Nick Steel
committed
oauth: username not always available in token. Use BasicClient
1 parent 27b2b59 commit 530bafe

File tree

1 file changed

+12
-48
lines changed

1 file changed

+12
-48
lines changed

oauth/src/lib.rs

+12-48
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
use log::{debug, error, info, trace};
2-
use oauth2::basic::{
3-
BasicErrorResponse, BasicRevocationErrorResponse, BasicTokenIntrospectionResponse,
4-
BasicTokenType,
5-
};
62
use oauth2::reqwest::http_client;
73
use oauth2::{
8-
AuthUrl, AuthorizationCode, Client, ClientId, CsrfToken, ExtraTokenFields, PkceCodeChallenge,
9-
RedirectUrl, Scope, StandardRevocableToken, StandardTokenResponse, TokenResponse, TokenUrl,
4+
basic::BasicClient, AuthUrl, AuthorizationCode, ClientId, CsrfToken, PkceCodeChallenge,
5+
RedirectUrl, Scope, TokenResponse, TokenUrl,
106
};
11-
use serde::{Deserialize, Serialize};
127
use std::io;
138
use std::{
149
io::{BufRead, BufReader, Write},
@@ -18,33 +13,6 @@ use std::{
1813
};
1914
use url::Url;
2015

21-
// Define extra fields to get the username too.
22-
// TODO: Maybe don't bother and use simpler BasicClient instead?
23-
24-
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
25-
pub struct SpotifyFields {
26-
#[serde(rename = "username")]
27-
#[serde(skip_serializing_if = "Option::is_none")]
28-
pub username: Option<String>,
29-
}
30-
impl SpotifyFields {
31-
pub fn username(&self) -> Option<&String> {
32-
self.username.as_ref()
33-
}
34-
}
35-
impl ExtraTokenFields for SpotifyFields {}
36-
37-
type SpotifyTokenResponse = StandardTokenResponse<SpotifyFields, BasicTokenType>;
38-
39-
type SpotifyClient = Client<
40-
BasicErrorResponse,
41-
SpotifyTokenResponse,
42-
BasicTokenType,
43-
BasicTokenIntrospectionResponse,
44-
StandardRevocableToken,
45-
BasicRevocationErrorResponse,
46-
>;
47-
4816
fn get_authcode_stdin() -> AuthorizationCode {
4917
println!("Provide code");
5018
let mut buffer = String::new();
@@ -90,29 +58,25 @@ fn get_authcode_listener(socket_address: SocketAddr) -> AuthorizationCode {
9058
code
9159
}
9260

93-
// TODO: Return a Result
61+
// TODO: Return a Result?
9462
// TODO: Pass in redirect_address instead since the redirect host depends on client ID?
95-
// TODO: Should also return username, for fun?
9663
pub fn get_access_token(client_id: &str, redirect_port: u16) -> String {
9764
// Must use host 127.0.0.1 with Spotify Desktop client ID.
9865
let redirect_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), redirect_port);
9966
let redirect_uri = format!("http://{redirect_address}/login");
10067

101-
let client = SpotifyClient::new(
68+
let client = BasicClient::new(
10269
ClientId::new(client_id.to_string()),
10370
None,
104-
AuthUrl::new("https://accounts.spotify.com/authorize".to_string())
105-
.expect("Invalid authorization endpoint URL"),
106-
Some(
107-
TokenUrl::new("https://accounts.spotify.com/api/token".to_string())
108-
.expect("Invalid token endpoint URL"),
109-
),
71+
AuthUrl::new("https://accounts.spotify.com/authorize".to_string()).unwrap(),
72+
Some(TokenUrl::new("https://accounts.spotify.com/api/token".to_string()).unwrap()),
11073
)
11174
.set_redirect_uri(RedirectUrl::new(redirect_uri).expect("Invalid redirect URL"));
11275

11376
let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
11477

11578
// Generate the full authorization URL.
79+
// Some of these scopes are unavailable for custom client IDs. Which?
11680
let scopes = vec![
11781
"app-remote-control",
11882
"playlist-modify",
@@ -169,15 +133,15 @@ pub fn get_access_token(client_id: &str, redirect_port: u16) -> String {
169133
});
170134
let token_response = rx.recv().unwrap();
171135
let token = match token_response {
172-
Ok(tok) => tok,
136+
Ok(tok) => {
137+
trace!("Obtained new access token: {tok:?}");
138+
tok
139+
}
173140
Err(e) => {
174141
error!("Failed to exchange code for access token: {e:?}");
175142
exit(1);
176143
}
177144
};
178-
let username = token.extra_fields().username().unwrap().to_string();
179-
let access_token = token.access_token().secret().to_string();
180-
trace!("Obtained new access token for {username}: {token:?}");
181145

182-
access_token
146+
token.access_token().secret().to_string()
183147
}

0 commit comments

Comments
 (0)