Skip to main content

ya-passport-auth

Async Yandex Passport (mobile) authentication library for Music Assistant providers.

CI PyPI Python 3.12+ License: MIT

Features

  • Three login methods — QR code scan, OAuth Device Flow (short code on ya.ru/device, plus a refresh_token for silent re-auth), and cookie-based login.
  • Full token-derivation graph — x_token exchange, music_token refresh, Passport cookie refresh (redirect-following), Quasar CSRF, Glagol device token, account info.
  • Security-first — SecretStr redacts tokens in repr/str/format/tracebacks and blocks pickling; host allow-list with HTTPS-only enforcement; CSRF extraction; per-request rate limiting; response size caps; log redaction via RedactingFilter.
  • Async-native — built on aiohttp with asyncio.Lock-protected rate limiter and connection management.
  • Strictly typed — mypy --strict clean, PEP 561 py.typed marker.
  • Well tested — 386 tests, 97 % branch coverage.

Installation

pip install ya-passport-auth

With the Music Assistant credential helpers (token refresh/rotation, shared accounts, and cookie parsing used by the MA Yandex providers):

pip install ya-passport-auth[ma]

The ma extra requires Music Assistant 2.10.0 or newer (music-assistant-models>=1.1.204).

Quick start

QR login

from ya_passport_auth import PassportClient

async def qr_login():
    async with PassportClient.create() as client:
        qr = await client.start_qr_login()
        print(f"Scan QR: {qr.qr_url}")
        creds = await client.poll_qr_until_confirmed(qr)

        info = await client.fetch_account_info(creds.x_token)
        print(f"Logged in as {info.display_login} (uid={info.uid})")

Device flow

OAuth 2.0 Device Authorization Grant. The user opens ya.ru/device on any device, enters the short user_code, and the library receives an x_token plus a long-lived refresh_token for silent re-auth later.

from ya_passport_auth import DeviceCodeSession, PassportClient

async def device_login():
    def on_code(session: DeviceCodeSession) -> None:
        print(f"Open {session.verification_url} and enter: {session.user_code}")

    async with PassportClient.create() as client:
        creds = await client.login_device_code(on_code=on_code)
        # creds.refresh_token is populated (only for this flow).
        # Persist creds — the access token is valid for ~1 year.

After the x_token expires, mint a new one without user interaction:

new_creds = await client.refresh_credentials(creds)

Only device-flow credentials carry a refresh_token; QR/cookie-login credentials have refresh_token=None and cannot be silently refreshed.

Device flow for a caller-owned OAuth application

Service providers can use the same polling implementation with their own Yandex OAuth application. The returned tokens are not converted to Passport or Yandex Music credentials:

from ya_passport_auth import OAuthDeviceClient

async def service_login():
    async with OAuthDeviceClient.create(
        client_id="your-client-id",
        client_secret="your-client-secret",
        scope="cloud_api:disk.read",
    ) as client:
        tokens = await client.login_device_code(
            on_code=lambda code: print(code.verification_url, code.user_code)
        )
        # Persist tokens.refresh_token for silent rotation.

Music Assistant providers present the code in their own setup UI and can use the MA helper for silent rotation:

from ya_passport_auth.ma import refresh_oauth_tokens

tokens = await refresh_oauth_tokens(
    client_id=client_id,
    client_secret=client_secret,
    refresh_token=tokens.refresh_token,
    scope="service.scope",
    session=mass.http_session,
)
from ya_passport_auth import PassportClient

async def cookie_login():
    cookies = "Session_id=...; sessionid2=..."  # from browser
    async with PassportClient.create() as client:
        creds = await client.login_cookies(cookies)
        print(f"x_token acquired, music_token ready")

Sharing one account between consumers

One consumer (the owner) persists and rotates the tokens; refresh tokens are single-use, so nobody else may rotate them. Other consumers borrow read-only: they read the owner's current tokens and, when no usable music token is stored, mint one in memory from the x_token. Minted tokens are cached, and a burst of concurrent 401s causes a single Passport call. Implement CredentialReader over wherever the owner keeps its tokens:

from ya_passport_auth import SecretStr, SharedTokenResolver, TokenSnapshot

class KeyringReader:
    async def read_tokens(self) -> TokenSnapshot:
        x_token = load_x_token_from_keyring()  # your storage
        return TokenSnapshot(music_token=None, x_token=SecretStr(x_token))

resolver = SharedTokenResolver(KeyringReader())
creds = await resolver.resolve()        # one read: music_token + matching x_token
# after a 401 with creds.music_token:
resolver.invalidate(creds.music_token)  # the next resolve() mints a fresh token

Music Assistant providers use ya_passport_auth.ma.BorrowedCredentialSource, which reads the linked Yandex Music instance's setup data.

Architecture

PassportClient  (public facade)
├── SafeHttpClient  (host allow-list, HTTPS enforcement, size caps, rate limiting)
│   └── AsyncMinDelayLimiter
└── Flows
    ├── QrLoginFlow        → CSRF scrape → session create → poll → x_token
    ├── DeviceCodeFlow     → device_code → ya.ru/device → x_token + refresh_token
    ├── CookieLoginFlow    → raw cookies → x_token
    ├── _token_exchange    → cookies→x_token, x_token→music_token  (shared)
    ├── PassportSessionRefresher  → x_token → session cookies (follows redirects)
    ├── AccountInfoFetcher → x_token → uid/login/avatar
    ├── QuasarCsrfFetcher  → CSRF token for IoT API
    └── GlagolDeviceTokenFetcher → music_token → Glagol device token

OAuthDeviceClient  (caller-owned Yandex OAuth application)
└── DeviceCodeFlow → service-scoped access_token + refresh_token

API overview

PassportClient

Method Description
start_qr_login() Begin QR login, returns QrSession
poll_qr_until_confirmed(qr) Poll until scanned, returns Credentials
complete_qr_login(qr) Exchange confirmed QR for tokens
start_device_login(...) Begin Device Flow, returns DeviceCodeSession
poll_device_until_confirmed(session, ...) Poll until confirmed, returns Credentials
login_device_code(on_code=..., ...) Full Device Flow with callback
refresh_credentials(creds) Mint fresh Credentials via refresh_token
login_cookies(cookies) Exchange browser cookies for Credentials
refresh_music_token(x_token) x_token → music_token
refresh_passport_cookies(x_token) Refresh session cookies (follows redirect chain)
get_quasar_csrf_token() Quasar CSRF token
get_glagol_device_token(music_token, ...) Glagol device token
fetch_account_info(x_token) Account metadata
validate_x_token(x_token) Check if token is valid

OAuthDeviceClient

Method Description
start_device_login(...) Begin Device Flow for a caller-owned OAuth app
poll_device_until_confirmed(session, ...) Poll and return unchanged OAuthTokens
login_device_code(on_code=..., ...) Run both phases with a UI callback
refresh(refresh_token) Rotate the service OAuth token pair

SecretStr

Opaque wrapper — repr() and str() return ***, pickling raises TypeError. Access plaintext only via get_secret().

Credentials

Frozen, slotted dataclass returned by poll_qr_until_confirmed() and login_cookies():

Field Type
x_token SecretStr
music_token SecretStr | None
uid int | None
display_login str | None
refresh_token SecretStr | None (device flow only)

Exception hierarchy

YaPassportError
├── NetworkError
│   └── UnexpectedHostError
├── CredentialSourceUnavailableError
└── AuthFailedError
    ├── InvalidCredentialsError
    ├── NoUsableCredentialsError
    ├── CsrfExtractionError
    ├── RateLimitedError
    ├── QRPendingError
    ├── QRTimeoutError
    └── DeviceCodeTimeoutError

Security

  • HTTPS-only — _check_host() rejects any non-https URL, preventing protocol-downgrade attacks via redirect Location headers.
  • Host allow-list — every request is validated against a frozen set of allowed Yandex hosts. Redirect targets are checked at each hop.
  • Token redaction — SecretStr hides values in repr/str/format; RedactingFilter scrubs OAuth headers and hex tokens from log output.
  • No pickling — SecretStr and Credentials block pickle/copy.
  • Response size caps — 1 MiB for JSON, 2 MiB for HTML.
  • See SECURITY.md for the full threat model (T1–T14).

Used by

Security disclaimer

The Passport-specific client interacts with Yandex using public mobile OAuth client IDs and secrets extracted from official Yandex Android applications. These values are well-known and present in many open-source projects; they are treated here as constants, not secrets. OAuthDeviceClient, by contrast, accepts credentials for an application owned by its caller.

There is no official Yandex API for the mobile Passport flow. Endpoints, response shapes, and regex patterns may break without notice.

License

MIT. See LICENSE and NOTICE for third-party attribution.

Metadata

Release files for ya-passport-auth 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ya-passport-auth 2.1.0
File Size Uploaded Provenance Actions
ya_passport_auth-2.1.0.tar.gz 93.0 kB With provenance Details for ya_passport_auth-2.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ya-passport-auth 2.1.0
File Interpreter ABI Platform Actions
ya_passport_auth-2.1.0-py3-none-any.whl Python 3 none any Details for ya_passport_auth-2.1.0-py3-none-any.whl

Total release size: 154.5 kB

Release files / ya_passport_auth-2.1.0.tar.gz

Download URL ya_passport_auth-2.1.0.tar.gz
Size 93.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d43b77e6f80d882e97da3ee6786af4eb7725fd4cc870687717a7b977ecee7f7b
BLAKE2b-256 checksum
How to use checksums
0764cd9b225006c88f818c00b390f4d2169fcc41ef83d4aef59405d4a9247c90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

Release files / ya_passport_auth-2.1.0-py3-none-any.whl

Download URL ya_passport_auth-2.1.0-py3-none-any.whl
Size 61.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0dd37b5198c6abdbfab3d5d04e2c625e99b5ec275cb4e071b6e6c24f989df467
BLAKE2b-256 checksum
How to use checksums
43d7668be114b78938f047f40c69fd0f7595d272f3dee340fff234d90afe5cb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

Security for ya-passport-auth 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ya_passport_auth-2.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page