Add request_ts + request_sig to every API call matching the mobile app interceptor pattern. Introduces signed_get/signed_get_auth helpers and a generic sign_request function. Endpoints now signed: - album/get, album/getFeatured - track/get, track/getList - artist/get, artist/getSimilarArtists, artist/page, artist/getReleasesGrid - discover/index, discover/playlists, discover/* browse endpoints - genre/list - label/page, label/explore, label/get - playlist/get, playlist/getUserPlaylists, playlist/create, playlist/delete, playlist/addTracks, playlist/deleteTracks, playlist/update, playlist/subscribe, playlist/unsubscribe - playlist/getTags - favorite/create, favorite/delete Excluded (pre-auth, cannot sign): user/login, oauth/callback. Already signed (unchanged): track/getFileUrl, favorite/getUserFavorites, session/start, file/url, all search endpoints. cc
205 lines
6.7 KiB
Rust
205 lines
6.7 KiB
Rust
//! Authentication and request signing
|
|
|
|
use chrono::{TimeZone, Utc};
|
|
use md5::{Digest, Md5};
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
use super::error::{ApiError, Result};
|
|
use qbz_models::UserSession;
|
|
|
|
/// Generate MD5 signature for protected API endpoints
|
|
///
|
|
/// Signature format: MD5(method + params + timestamp + secret)
|
|
pub fn generate_signature(method: &str, params: &str, timestamp: u64, secret: &str) -> String {
|
|
let sig_string = format!("{}{}{}{}", method, params, timestamp, secret);
|
|
let mut hasher = Md5::new();
|
|
hasher.update(sig_string.as_bytes());
|
|
format!("{:x}", hasher.finalize())
|
|
}
|
|
|
|
/// Generate signature for track/getFileUrl endpoint
|
|
pub fn sign_get_file_url(track_id: u64, format_id: u32, timestamp: u64, secret: &str) -> String {
|
|
let params = format!("format_id{}intentstreamtrack_id{}", format_id, track_id);
|
|
generate_signature("trackgetFileUrl", ¶ms, timestamp, secret)
|
|
}
|
|
|
|
/// Generate signature for favorite/getUserFavorites endpoint
|
|
pub fn sign_get_favorites(timestamp: u64, secret: &str) -> String {
|
|
generate_signature("favoritegetUserFavorites", "", timestamp, secret)
|
|
}
|
|
|
|
/// Generate signature for search endpoints.
|
|
/// `method` is the concatenated endpoint name (e.g. "catalogsearch", "albumsearch").
|
|
/// Query params are sorted alphabetically: limit, offset, query, [type].
|
|
pub fn sign_search(
|
|
method: &str,
|
|
query: &str,
|
|
limit: u32,
|
|
offset: u32,
|
|
search_type: Option<&str>,
|
|
timestamp: u64,
|
|
secret: &str,
|
|
) -> String {
|
|
let mut params = format!("limit{}offset{}query{}", limit, offset, query);
|
|
if let Some(st) = search_type {
|
|
params.push_str(&format!("type{}", st));
|
|
}
|
|
generate_signature(method, ¶ms, timestamp, secret)
|
|
}
|
|
|
|
/// Generic request signature for any endpoint.
|
|
///
|
|
/// `method` is the endpoint path with slashes removed, e.g. "/album/get" → "albumget".
|
|
/// `kv_pairs` are (key, value) pairs sorted alphabetically by key.
|
|
/// The params string is built as key1value1key2value2... (same as mobile app interceptor).
|
|
pub fn sign_request(method: &str, kv_pairs: &[(&str, &str)], timestamp: u64, secret: &str) -> String {
|
|
let mut sorted = kv_pairs.to_vec();
|
|
sorted.sort_by_key(|(k, _)| *k);
|
|
let params: String = sorted.iter().map(|(k, v)| format!("{}{}", k, v)).collect();
|
|
generate_signature(method, ¶ms, timestamp, secret)
|
|
}
|
|
|
|
/// Get current Unix timestamp
|
|
pub fn get_timestamp() -> u64 {
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.expect("Time went backwards")
|
|
.as_secs()
|
|
}
|
|
|
|
/// Parse user login response
|
|
pub fn parse_login_response(response: &serde_json::Value) -> Result<UserSession> {
|
|
let user = response
|
|
.get("user")
|
|
.ok_or_else(|| ApiError::AuthenticationError("No user in response".to_string()))?;
|
|
|
|
let user_auth_token = response
|
|
.get("user_auth_token")
|
|
.and_then(|v| v.as_str())
|
|
.ok_or_else(|| ApiError::AuthenticationError("No auth token in response".to_string()))?
|
|
.to_string();
|
|
|
|
let user_id = user
|
|
.get("id")
|
|
.and_then(|v| v.as_u64())
|
|
.ok_or_else(|| ApiError::AuthenticationError("No user id".to_string()))?;
|
|
|
|
let email = user
|
|
.get("email")
|
|
.and_then(|v| v.as_str())
|
|
.unwrap_or("")
|
|
.to_string();
|
|
|
|
let display_name = user
|
|
.get("display_name")
|
|
.and_then(|v| v.as_str())
|
|
.or_else(|| user.get("login").and_then(|v| v.as_str()))
|
|
.unwrap_or("")
|
|
.to_string();
|
|
|
|
// Check subscription
|
|
let credential = user.get("credential");
|
|
let subscription_label = credential
|
|
.and_then(|c| c.get("parameters"))
|
|
.and_then(|p| p.get("short_label"))
|
|
.and_then(|v| v.as_str())
|
|
.unwrap_or("Unknown")
|
|
.to_string();
|
|
|
|
fn parse_subscription_valid_until(parameters: &serde_json::Value) -> Option<String> {
|
|
// Try common string fields first.
|
|
let string_keys = [
|
|
"end_date",
|
|
"expiration_date",
|
|
"valid_until",
|
|
"expires_at",
|
|
"expiry_date",
|
|
];
|
|
for key in string_keys {
|
|
if let Some(s) = parameters.get(key).and_then(|v| v.as_str()) {
|
|
let trimmed = s.trim();
|
|
if !trimmed.is_empty() {
|
|
return Some(trimmed.to_string());
|
|
}
|
|
}
|
|
}
|
|
|
|
// Try common timestamp fields (seconds).
|
|
let ts_keys = [
|
|
"end_date_ts",
|
|
"expires_at_ts",
|
|
"expiration_ts",
|
|
"valid_until_ts",
|
|
];
|
|
for key in ts_keys {
|
|
if let Some(ts) = parameters.get(key).and_then(|v| v.as_i64()) {
|
|
if ts > 0 {
|
|
return Some(Utc.timestamp_opt(ts, 0).single()?.date_naive().to_string());
|
|
}
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
let subscription_valid_until = credential
|
|
.and_then(|c| c.get("parameters"))
|
|
.and_then(parse_subscription_valid_until);
|
|
|
|
// Check if user has valid subscription
|
|
let has_subscription = credential
|
|
.and_then(|c| c.get("parameters"))
|
|
.map(|p| !p.is_null() && p.as_object().map(|o| !o.is_empty()).unwrap_or(false))
|
|
.unwrap_or(false);
|
|
|
|
if !has_subscription {
|
|
return Err(ApiError::IneligibleUser);
|
|
}
|
|
|
|
Ok(UserSession {
|
|
user_auth_token,
|
|
user_id,
|
|
email,
|
|
display_name,
|
|
subscription_label,
|
|
subscription_valid_until,
|
|
})
|
|
}
|
|
|
|
/// App seed for CMAF request signing and key derivation.
|
|
/// Public value extracted from the webplayer JS bundle.
|
|
pub const CMAF_SEED: &str = "abb21364945c0583309667d13ca3d93a";
|
|
|
|
/// Generate CMAF signature for session/start endpoint
|
|
pub fn sign_session_start(timestamp: u64) -> String {
|
|
let mut args = std::collections::BTreeMap::new();
|
|
args.insert("profile", "qbz-1".to_string());
|
|
qbz_cmaf::compute_request_sig("sessionstart", &args, ×tamp.to_string(), CMAF_SEED)
|
|
}
|
|
|
|
/// Generate CMAF signature for file/url endpoint
|
|
pub fn sign_file_url(track_id: u64, format_id: u32, timestamp: u64) -> String {
|
|
let mut args = std::collections::BTreeMap::new();
|
|
args.insert("format_id", format_id.to_string());
|
|
args.insert("intent", "stream".to_string());
|
|
args.insert("track_id", track_id.to_string());
|
|
qbz_cmaf::compute_request_sig("fileurl", &args, ×tamp.to_string(), CMAF_SEED)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_generate_signature() {
|
|
let sig = generate_signature("test", "params", 1234567890, "secret");
|
|
assert_eq!(sig.len(), 32); // MD5 hex is 32 chars
|
|
}
|
|
|
|
#[test]
|
|
fn test_sign_get_file_url() {
|
|
let sig = sign_get_file_url(123456, 27, 1234567890, "testsecret");
|
|
assert_eq!(sig.len(), 32);
|
|
}
|
|
}
|