in the beginning there was darkness

This commit is contained in:
Dominic Pearson
2024-08-05 18:02:37 +02:00
commit 8e615f9006
6 changed files with 2381 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
credentials.json

2285
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

16
Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[package]
name = "librespot-auth"
version = "0.1.0"
edition = "2021"
[dependencies]
futures = "0.3"
librespot-core = { git = "https://github.com/librespot-org/librespot", branch = "dev" }
librespot-discovery = { git = "https://github.com/librespot-org/librespot", branch = "dev" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
sha1 = "0.10.5"
hex = "0.4.3"
log = "0.4"
clap = { version = "4.0", features = ["derive"] }

13
LICENCE Normal file
View File

@@ -0,0 +1,13 @@
Copyright 2024 Dominic Pearson <dsp@technoanimal.net>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

10
README.md Normal file
View File

@@ -0,0 +1,10 @@
librespot-auth
==============
A simple program for populating a `credentials.json` via Spotify's zeroconf authentication.
Optional arguments:
--name Name of the virtual speaker, default: "Speaker"
--path Target path for credentials, default: "credentials.json" relative to execution
While running on the same network as a Spotify client, it should appear as a Spotify Connect device with the given name. Select it once as an output device, and the Spotify client will transmit the required authentication blob, which is received by this application, written to the credentials path, and immediately exits. This is useful in cases where you run librespot on remote hosts on a different network (thus complicating zeroconf), as in version 3.203.235 of the Spotify eSDK they removed the SpConnectionLoginPassword API.

55
src/main.rs Normal file
View File

@@ -0,0 +1,55 @@
use clap::Parser;
use futures::StreamExt;
use librespot_core::authentication::Credentials;
use librespot_core::SessionConfig;
use librespot_discovery::{DeviceType, Discovery};
use sha1::{Digest, Sha1};
use serde_json;
use std::fs::File;
use std::io::Write;
use std::process::exit;
use log::warn;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(short, long, default_value = "Speaker")]
name: String,
#[arg(short, long, default_value = "credentials.json")]
path: String,
}
pub fn save_credentials_and_exit(location: &str, cred: &Credentials) {
let result = File::create(location).and_then(|mut file| {
let data = serde_json::to_string(cred)?;
write!(file, "{data}")
});
if let Err(e) = result {
warn!("Cannot save credentials to cache: {}", e);
exit(1);
} else {
println!("Credentials saved: {}", location);
exit(0);
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let args = Args::parse();
let name = args.name;
let credentials_location = args.path;
let device_id = hex::encode(Sha1::digest(name.clone().as_bytes()));
let mut server = Discovery::builder(device_id.clone(), SessionConfig::default().client_id)
.name(name.clone())
.device_type(DeviceType::Speaker)
.launch()
.unwrap();
println!("Open Spotify and select output device: {}", name);
while let Some(credentials) = server.next().await {
save_credentials_and_exit(&credentials_location, &credentials);
}
}