Skip to content

Commit

Permalink
ignore cache when authenticating the application using CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
aome510 committed Oct 31, 2024
1 parent 8cd21cf commit e0203d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
20 changes: 18 additions & 2 deletions spotify_player/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const OAUTH_SCOPES: &[&str] = &[
pub struct AuthConfig {
pub cache: Cache,
pub session_config: SessionConfig,
pub login_redirect_uri: String,
}

impl Default for AuthConfig {
Expand Down Expand Up @@ -80,8 +81,23 @@ impl AuthConfig {
}

/// Get Spotify credentials to authenticate the application
pub async fn get_creds(auth_config: &AuthConfig, reauth: bool) -> Result<Credentials> {
Ok(match auth_config.cache.credentials() {
///
/// # Args
/// - `auth_config`: authentication configuration
/// - `reauth`: whether to re-authenticate the application if no cached credentials are found
// - `use_cached`: whether to use cached credentials if available
pub async fn get_creds(
auth_config: &AuthConfig,
reauth: bool,
use_cached: bool,
) -> Result<Credentials> {
let creds = if use_cached {
auth_config.cache.credentials()
} else {
None
};

Ok(match creds {
None => {
let msg = "No cached credentials found, please authenticate the application first.";
if reauth {
Expand Down
4 changes: 2 additions & 2 deletions spotify_player/src/cli/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,14 @@ fn try_connect_to_client(socket: &UdpSocket, configs: &config::Configs) -> Resul
}

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

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

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

// construct a socket request based on the CLI command and its arguments
Expand Down
2 changes: 1 addition & 1 deletion spotify_player/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Client {
/// Create a new client session
pub async fn new_session(&self, state: Option<&SharedState>, reauth: bool) -> Result<()> {
let session = self.auth_config.session();
let creds = auth::get_creds(&self.auth_config, reauth)
let creds = auth::get_creds(&self.auth_config, reauth, true)
.await
.context("get credentials")?;
*self.session.lock().await = Some(session.clone());
Expand Down

0 comments on commit e0203d0

Please sign in to comment.