feat(qobuz): migrate login_with_token to V2 crates
Add login_with_token to QobuzClient and QbzCore for restoring sessions from saved OAuth tokens. Previously only existed in the legacy client. cc
This commit is contained in:
2 files changed
+81
No files matched your search
@@ -110,6 +110,31 @@ impl<A: FrontendAdapter + Send + Sync + 'static> QbzCore<A> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Restore a session from a saved OAuth user_auth_token.
|
||||
pub async fn login_with_token(&self, token: &str) -> Result<UserSession, CoreError> {
|
||||
let client = self.client.read().await;
|
||||
let client = client.as_ref().ok_or(CoreError::NotInitialized)?;
|
||||
|
||||
match client.login_with_token(token).await {
|
||||
Ok(session) => {
|
||||
self.emit(CoreEvent::LoggedIn {
|
||||
session: session.clone(),
|
||||
})
|
||||
.await;
|
||||
Ok(session)
|
||||
}
|
||||
Err(e) => {
|
||||
self.emit(CoreEvent::Error {
|
||||
code: "OAUTH_TOKEN_FAILED".to_string(),
|
||||
message: e.to_string(),
|
||||
recoverable: true,
|
||||
})
|
||||
.await;
|
||||
Err(CoreError::AuthFailed(e.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Inject an already-authenticated session (e.g. from OAuth flow).
|
||||
/// Emits a LoggedIn event so the rest of the system knows auth state changed.
|
||||
pub async fn set_session(&self, session: UserSession) -> Result<(), CoreError> {
|
||||
|
||||
@@ -276,6 +276,62 @@ impl QobuzClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Restore a session from a previously saved OAuth user_auth_token.
|
||||
///
|
||||
/// Used at startup when the user logged in via OAuth (web browser) and the
|
||||
/// token was persisted. Calls POST /user/login with X-User-Auth-Token header.
|
||||
/// Returns an error if the token has expired.
|
||||
pub async fn login_with_token(&self, token: &str) -> Result<UserSession> {
|
||||
use reqwest::header::{HeaderMap, HeaderValue};
|
||||
|
||||
let tokens = self.tokens.read().await;
|
||||
let app_id = tokens
|
||||
.as_ref()
|
||||
.ok_or_else(|| ApiError::BundleExtractionError("Client not initialized".to_string()))?
|
||||
.app_id
|
||||
.clone();
|
||||
drop(tokens);
|
||||
|
||||
let user_login_url = endpoints::build_url(endpoints::paths::USER_LOGIN);
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"X-App-Id",
|
||||
HeaderValue::from_str(&app_id).map_err(|_| ApiError::InvalidAppId)?,
|
||||
);
|
||||
headers.insert(
|
||||
"X-User-Auth-Token",
|
||||
HeaderValue::from_str(token)
|
||||
.map_err(|_| ApiError::AuthenticationError("Invalid token format".into()))?,
|
||||
);
|
||||
|
||||
log::info!("[OAuth] Restoring session from saved token");
|
||||
let resp = self
|
||||
.http
|
||||
.post(&user_login_url)
|
||||
.headers(headers)
|
||||
.header("Content-Type", "text/plain;charset=UTF-8")
|
||||
.body("extra=partner")
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
match resp.status() {
|
||||
StatusCode::OK => {
|
||||
let json: serde_json::Value = resp.json().await?;
|
||||
let session = super::auth::parse_login_response(&json)?;
|
||||
*self.session.write().await = Some(session.clone());
|
||||
log::info!("[OAuth] Session restored from token");
|
||||
Ok(session)
|
||||
}
|
||||
StatusCode::UNAUTHORIZED => Err(ApiError::AuthenticationError(
|
||||
"OAuth token expired or invalid".to_string(),
|
||||
)),
|
||||
status => Err(ApiError::ApiResponse(format!(
|
||||
"Token re-auth failed: {}",
|
||||
status
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get current user info (display name, subscription, and expiry if available)
|
||||
pub async fn get_user_info(&self) -> Option<(String, String, Option<String>)> {
|
||||
self.session.read().await.as_ref().map(|s| {
|
||||
|
||||
Reference in new issue
Block a user