Skip to content

Commit 99fcb20

Browse files
authored
ignore cache when authenticating the application using authenticate CLI command (aome510#592)
resolves aome510#590 This PR also allows `login_redirect_uri` to be configurable
1 parent 1c3f09a commit 99fcb20

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

Diff for: docs/config.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ All configuration files should be placed inside the application's configuration
2626
| --------------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------- |
2727
| `client_id` | the Spotify client's ID | `65b708073fc0480ea92a077233ca87bd` |
2828
| `client_id_command` | a shell command that prints the Spotify client ID to stdout (overrides `client_id`) | `None` |
29+
| `login_redirect_uri` | the redirect URI for authenticating the application | `http://127.0.0.1:8989/login` |
2930
| `client_port` | the port that the application's client is running on to handle CLI commands | `8080` |
3031
| `tracks_playback_limit` | the limit for the number of tracks played in a **tracks** playback | `50` |
3132
| `playback_format` | the format of the text in the playback's window | `{status} {track} • {artists}\n{album}\n{metadata}` |

Diff for: examples/app.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
theme = "default"
22
client_id = "65b708073fc0480ea92a077233ca87bd"
3+
login_redirect_uri = "http://127.0.0.1:8989/login"
34
client_port = 8080
45
tracks_playback_limit = 50
56
playback_format = "{status} {track} • {artists}\n{album}\n{metadata}"

Diff for: spotify_player/src/auth.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use librespot_oauth::get_access_token;
55
use crate::config;
66

77
pub const SPOTIFY_CLIENT_ID: &str = "65b708073fc0480ea92a077233ca87bd";
8-
const CLIENT_REDIRECT_URI: &str = "http://127.0.0.1:8989/login";
98
// based on https://github.com/librespot-org/librespot/blob/f96f36c064795011f9fee912291eecb1aa46fff6/src/main.rs#L173
109
const OAUTH_SCOPES: &[&str] = &[
1110
"app-remote-control",
@@ -40,13 +39,15 @@ const OAUTH_SCOPES: &[&str] = &[
4039
pub struct AuthConfig {
4140
pub cache: Cache,
4241
pub session_config: SessionConfig,
42+
pub login_redirect_uri: String,
4343
}
4444

4545
impl Default for AuthConfig {
4646
fn default() -> Self {
4747
AuthConfig {
4848
cache: Cache::new(None::<String>, None, None, None).unwrap(),
4949
session_config: SessionConfig::default(),
50+
login_redirect_uri: "http://127.0.0.1:8989/login".to_string(),
5051
}
5152
}
5253
}
@@ -74,20 +75,36 @@ impl AuthConfig {
7475
Ok(AuthConfig {
7576
cache,
7677
session_config: configs.app_config.session_config()?,
78+
login_redirect_uri: configs.app_config.login_redirect_uri.clone(),
7779
})
7880
}
7981
}
8082

8183
/// Get Spotify credentials to authenticate the application
82-
pub async fn get_creds(auth_config: &AuthConfig, reauth: bool) -> Result<Credentials> {
83-
Ok(match auth_config.cache.credentials() {
84+
///
85+
/// # Args
86+
/// - `auth_config`: authentication configuration
87+
/// - `reauth`: whether to re-authenticate the application if no cached credentials are found
88+
// - `use_cached`: whether to use cached credentials if available
89+
pub async fn get_creds(
90+
auth_config: &AuthConfig,
91+
reauth: bool,
92+
use_cached: bool,
93+
) -> Result<Credentials> {
94+
let creds = if use_cached {
95+
auth_config.cache.credentials()
96+
} else {
97+
None
98+
};
99+
100+
Ok(match creds {
84101
None => {
85102
let msg = "No cached credentials found, please authenticate the application first.";
86103
if reauth {
87104
eprintln!("{msg}");
88105
get_access_token(
89106
SPOTIFY_CLIENT_ID,
90-
CLIENT_REDIRECT_URI,
107+
&auth_config.login_redirect_uri,
91108
OAUTH_SCOPES.to_vec(),
92109
)
93110
.map(|t| Credentials::with_access_token(t.access_token))?

Diff for: spotify_player/src/cli/handlers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,14 @@ fn try_connect_to_client(socket: &UdpSocket, configs: &config::Configs) -> Resul
171171
}
172172

173173
pub fn handle_cli_subcommand(cmd: &str, args: &ArgMatches) -> Result<()> {
174-
let socket = UdpSocket::bind("127.0.0.1:0")?;
175174
let configs = config::get_config();
176175

177176
// handle commands that don't require a client separately
178177
match cmd {
179178
"authenticate" => {
180179
let auth_config = AuthConfig::new(configs)?;
181180
let rt = tokio::runtime::Runtime::new()?;
182-
rt.block_on(crate::auth::get_creds(&auth_config, true))?;
181+
rt.block_on(crate::auth::get_creds(&auth_config, true, false))?;
183182
std::process::exit(0);
184183
}
185184
"generate" => {
@@ -194,6 +193,7 @@ pub fn handle_cli_subcommand(cmd: &str, args: &ArgMatches) -> Result<()> {
194193
_ => {}
195194
}
196195

196+
let socket = UdpSocket::bind("127.0.0.1:0")?;
197197
try_connect_to_client(&socket, configs).context("try to connect to a client")?;
198198

199199
// construct a socket request based on the CLI command and its arguments

Diff for: spotify_player/src/client/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Client {
114114
/// Create a new client session
115115
pub async fn new_session(&self, state: Option<&SharedState>, reauth: bool) -> Result<()> {
116116
let session = self.auth_config.session();
117-
let creds = auth::get_creds(&self.auth_config, reauth)
117+
let creds = auth::get_creds(&self.auth_config, reauth, true)
118118
.await
119119
.context("get credentials")?;
120120
*self.session.lock().await = Some(session.clone());

Diff for: spotify_player/src/config/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub struct AppConfig {
5454

5555
pub client_port: u16,
5656

57+
pub login_redirect_uri: String,
58+
5759
pub player_event_hook_command: Option<Command>,
5860

5961
pub playback_format: String,
@@ -256,6 +258,8 @@ impl Default for AppConfig {
256258

257259
client_port: 8080,
258260

261+
login_redirect_uri: "http://127.0.0.1:8989/login".to_string(),
262+
259263
tracks_playback_limit: 50,
260264

261265
playback_format: String::from("{status} {track} • {artists}\n{album}\n{metadata}"),

0 commit comments

Comments
 (0)