feat: Yandex sign-in with device code, keychain storage and account status

- ym-api wraps yamuse: device-code flow, token refresh, account status,
  and its own Error type that the UI acts on (expired, denied, network)
- Credentials live in the system keychain, never print their tokens, and
  refresh when within a week of expiry; a rejected token is forgotten
- Sign-in screen with code, QR, copy/open-in-browser, countdown and a
  pasted-token fallback; restoring/unreachable screens on startup
- Sidebar account chip and Settings account card with Plus status and
  sign-out; Settings re-reads the account when opened
- Live device-code test behind --ignored
This commit is contained in:
Yuriy Panov committed 2026-10-15 14:25:45 +06:00
1 parent c4928ae7fe
commit 43e8f86a5d
17 files changed
+2811 -127

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
use crate::Result;
use crate::auth::Credentials;
/// The signed-in user as the UI needs them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Account {
pub uid: i64,
pub login: String,
/// Best human-readable name: display name, then full name, then login.
pub name: String,
/// Yandex Plus unlocks full-length tracks; without it the API serves 30-second previews.
pub has_plus: bool,
/// When the current subscription ends, as Yandex formats it (ISO 8601).
pub subscription_end: Option<String>,
}
impl Account {
/// One letter for the avatar.
pub fn initial(&self) -> char {
self.name.chars().find(|c| c.is_alphanumeric()).map_or('?', |c| {
c.to_uppercase().next().unwrap_or(c)
})
}
fn from_status(status: yamuse::models::Status) -> Option<Self> {
let account = status.account?;
let uid = account.uid?;
let login = account.login.unwrap_or_default();
let non_empty = |s: Option<String>| s.filter(|s| !s.trim().is_empty());
let name = non_empty(account.display_name)
.or_else(|| non_empty(account.full_name))
.unwrap_or_else(|| login.clone());
Some(Self {
uid,
login,
name,
has_plus: status.plus.and_then(|p| p.has_plus).unwrap_or(false),
subscription_end: status.subscription.and_then(|s| {
s.end.or_else(|| s.auto_renewable.into_iter().find_map(|a| a.expires))
}),
})
}
}
/// An authenticated API client. Cheap to clone.
#[derive(Debug, Clone)]
pub struct YmClient {
inner: yamuse::Client,
}
impl YmClient {
/// Builds a client for `credentials` and loads the account. Fails with
/// [`crate::Error::Unauthorized`] when the token is no longer valid.
pub async fn connect(credentials: &Credentials) -> Result<(Self, Account)> {
let inner = yamuse::Client::builder().token(&credentials.access_token).build()?;
let status = inner.refresh_account().await?;
let account = Account::from_status(status)
.ok_or_else(|| crate::Error::Unauthorized("account status has no user".into()))?;
Ok((Self { inner }, account))
}
/// A client with no token, for previews and tests. Every account call fails.
pub fn unauthenticated() -> Result<Self> {
Ok(Self { inner: yamuse::Client::anonymous()? })
}
/// Re-reads the account, e.g. after a subscription may have changed.
pub async fn account(&self) -> Result<Account> {
let status = self.inner.refresh_account().await?;
Account::from_status(status)
.ok_or_else(|| crate::Error::Unauthorized("account status has no user".into()))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn status(json: &str) -> yamuse::models::Status {
serde_json::from_str(json).unwrap()
}
#[test]
fn account_prefers_display_name_and_reads_plus() {
let a = Account::from_status(status(
r#"{"account":{"uid":42,"login":"yuriy.panov","displayName":"Юрий","fullName":"Юрий Панов"},
"plus":{"hasPlus":true},"subscription":{"end":"2027-01-01T00:00:00+03:00"}}"#,
))
.unwrap();
assert_eq!(a.uid, 42);
assert_eq!(a.name, "Юрий");
assert_eq!(a.initial(), 'Ю');
assert!(a.has_plus);
assert_eq!(a.subscription_end.as_deref(), Some("2027-01-01T00:00:00+03:00"));
}
#[test]
fn account_falls_back_to_login_and_no_plus() {
let a = Account::from_status(status(
r#"{"account":{"uid":7,"login":"listener","displayName":" "}}"#,
))
.unwrap();
assert_eq!(a.name, "listener");
assert!(!a.has_plus);
assert_eq!(a.subscription_end, None);
}
#[test]
fn anonymous_status_is_not_an_account() {
assert!(Account::from_status(status(r#"{"account":{"now":"2026-10-15"}}"#)).is_none());
assert!(Account::from_status(status("{}")).is_none());
}
}