Skip to content

Commit a07431d

Browse files
committed
Read client_id from a file in the config folder
1 parent c526ae5 commit a07431d

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ docker run --rm \
164164
165165
To enable a full [Spotify connect](https://www.spotify.com/us/connect/) support, users will need to enable a _"user-provided client integration"_.
166166
167-
This integration can be done by following [this documentation](https://developer.spotify.com/documentation/general/guides/authorization/app-settings/) to register a Spotify app and then specifying the app's `client_id` in the [general configuration file](docs/config.md#general). **NOTE**: please make sure that you specify `http://127.0.0.1:8989/login` (default value of `login_redirect_uri` config option) in the **Redirect URI** section when creating the app.
167+
This integration can be done by following [this documentation](https://developer.spotify.com/documentation/general/guides/authorization/app-settings/) to register a Spotify app and then specifying the app's `client_id` in the [general configuration file](docs/config.md#general). You can also create a file called `client_id` in the [config folder](#configurations) and put the ID there. **NOTE**: please make sure that you specify `http://127.0.0.1:8989/login` (default value of `login_redirect_uri` config option) in the **Redirect URI** section when creating the app.
168168
169169
Upon running `spotify_player` with a user-provided `client_id`, user will be prompted to authenticate the app described earlier. **NOTE** that this prompt is different from the prompt to authenticate `spotify_player`. Upon accepting the authentication request, `spotify_player` will retrieve an access token of the app to finish setting up the integration.
170170

docs/config.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ esac
129129

130130
### Client id command
131131

132-
If you prefer not to include your own `client_id` directly in your configuration, you can retrieve it at runtime using the `client_id_command` option.
132+
If you prefer not to include your own `client_id` directly in your configuration, you have two options:
133+
134+
1. you can create a file called `client_id` with the value of the client_id in the [config folder](../README.md#configurations), which will be picked up if it is not empty.
135+
2. you can retrieve it at runtime using the `client_id_command` option.
133136

134137
If specified, `client_id_command` should be an object with two fields `command` and `args`, just like `player_event_hook_command`.
135138
For example to read your client_id from a file your could use `client_id_command = { command = "cat", args = ["/path/to/file"] }`

spotify_player/src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl AppClient {
7070
// Construct user-provided client.
7171
// This custom client is needed for Spotify Connect integration because the Spotify client (`AppConfig::spotify`),
7272
// which `spotify-player` uses to retrieve Spotify data, doesn't have access to user available devices
73-
let mut user_client = configs.app_config.get_user_client_id()?.clone().map(|id| {
73+
let mut user_client = configs.app_config.get_user_client_id(&configs.config_folder)?.clone().map(|id| {
7474
let creds = rspotify::Credentials { id, secret: None };
7575
let oauth = rspotify::OAuth {
7676
scopes: rspotify::scopes!("user-read-playback-state"),

spotify_player/src/config/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct Configs {
3131
pub app_config: AppConfig,
3232
pub keymap_config: KeymapConfig,
3333
pub theme_config: ThemeConfig,
34+
pub config_folder: std::path::PathBuf,
3435
pub cache_folder: std::path::PathBuf,
3536
}
3637

@@ -40,6 +41,7 @@ impl Configs {
4041
app_config: AppConfig::new(config_folder)?,
4142
keymap_config: KeymapConfig::new(config_folder)?,
4243
theme_config: ThemeConfig::new(config_folder)?,
44+
config_folder: config_folder.to_path_buf(),
4345
cache_folder: cache_folder.to_path_buf(),
4446
})
4547
}
@@ -450,8 +452,19 @@ impl AppConfig {
450452
}
451453
}
452454

453-
/// Returns stdout of `client_id_command` if set, otherwise it returns the the value of `client_id`
454-
pub fn get_user_client_id(&self) -> Result<Option<String>> {
455+
/// Returns the user's client ID by checking the following sources in order:
456+
/// 1. `client_id` file in the config folder (if it exists and is not empty)
457+
/// 2. stdout of `client_id_command` if set
458+
/// 3. the value of `client_id` field
459+
pub fn get_user_client_id(&self, config_folder: &Path) -> Result<Option<String>> {
460+
let client_id_file = config_folder.join("client_id");
461+
if let Ok(file_content) = std::fs::read_to_string(&client_id_file) {
462+
let trimmed = file_content.trim();
463+
if !trimmed.is_empty() {
464+
return Ok(Some(trimmed.to_string()));
465+
}
466+
}
467+
455468
match self.client_id_command {
456469
Some(ref cmd) => cmd.execute(None).map(|out| Some(out.trim().to_string())),
457470
None => Ok(self.client_id.clone()),

0 commit comments

Comments
 (0)