Skip to content

feat(auth): run tvOS QR sign-in on v2 - #360

Merged
Quick104 merged 2 commits into
mainfrom
apiv2-stack/12-tvos-qr-sign-in-v2
Sep 23, 2026
Merged

Quick104 merged 2 commits into
mainfrom
apiv2-stack/12-tvos-qr-sign-in-v2

Conversation

@Quick104

Copy link
Copy Markdown
Contributor

The tvOS QR sign-in now starts and polls through /api/v2/auth/device/*, binds the verified account id from the approved poll, and shows the existing update messages when the server or app needs an update.

Problem

The tvOS QR sign-in still called POST /api/v1/auth/device/start and POST /api/v1/auth/device/poll (inventory rows 11 and 12). A 1.0 server answers those with 410. The v1 poll answer also had no verified account id, so a QR sign-in installed an unverified account binding. On a v1-only server, the screen showed "Couldn't start sign-in." followed by the raw error instead of the existing server-update message.

Solution

  • QRLoginViewModel goes through AuthService to the shared SiloAPI.shared.apiV2Client:
    • startDeviceLogin calls POST /api/v2/auth/device/start and expects 201.
    • pollDeviceLogin calls POST /api/v2/auth/device/poll, expects 200, and returns the validated APIv2DevicePoll.
    • The contract marks both operations public and non_retryable. Each call is dispatched once, with no bearer and no refresh replay. APIv2Client already enforces this, and AuthDeviceV2Tests covers it.
  • An approved poll installs the nested TokenPair and binds tokens.user.id as the verified account id. AuthService.installSession(accountID:) is now non-optional, because both callers (password and QR) have a verified id. The QR flow refuses an approved poll marked temporary, since that flag belongs to a SiloRemote handoff.
  • The poll loop starts at the start answer's interval, then waits for each poll's poll_after, as Android's DeviceLoginRepository does.
  • QRLoginViewModel.pollFailure(for:) classifies every failed poll (F5 / D7):
    • A v1-only server (the legacy Go 404 on a /api/v2 route, or the recorded serverUpdateRequired verdict) shows "This server needs to be updated before this version of Silo can use it." The flow stops.
    • A 410 with problem type client_upgrade_required shows "Update Silo to keep using this server." The flow stops, and no tokens are touched.
    • A 404 problem, or any other 404 that is not the legacy Go text, shows "This sign-in request has expired."
    • An incomplete approval, an account change during sign-in, or a failed session install ends the attempt, because the server issues the tokens only once.
    • Network errors and other statuses (5xx, 429 and so on) keep polling until the code expires, as before.
  • A start failure caused by an update requirement shows only the update message.
  • QRLoginViewModel now takes its AuthService and TokenStore through init, defaulting to the shared instances, so tests can drive it. TVLoginView is unchanged.
  • The v1 AuthService start and poll methods are removed. DeviceLoginPollResponse, DeviceLoginPollRequest and the v1 paths in PairingDeviceAPI stay because pairing still uses them; W5-C removes them.

Failure outcomes

Both operations are non_retryable.

Outcome Start Poll
Definite success Show the QR code and start polling pending: wait for poll_after. approved: install the tokens and bind the account. denied/expired/consumed: show the terminal message
Definite failure Show the error (the update message when an update is required) Legacy 404 or 410 upgrade: update message, stop. Other 404: expired, stop. Other status: poll again later, which is a new poll, not a replay
Uncertain (sent, no answer) Show the error. Retrying opens a new request Poll again on the next tick. If the lost answer was approved, the next poll returns consumed and the screen says the code was already used

Validation

  • iOS, tvOS and macOS builds passed.
  • iOS tests (SiloTests/AuthDeviceV2Tests SiloTests/AccountSessionPersistenceTests) passed (48 tests, 0 failures). The same classes under SiloTVTests passed (48 tests, 0 failures).
  • scripts/ci/check-no-api-v1.sh passed.
  • New tests in AuthDeviceV2Tests run on StubURLProtocol:
    • Poll: a legacy 404 shows the server-update message, a 404 problem means expired, and a 410 client_upgrade_required shows the app-update message. A 503 problem and a timeout keep polling.
    • Start: a legacy 404 shows only the server-update message.
    • QRLoginViewModel runs against a stubbed server: start, a pending poll with poll_after: 1 and an approved poll end in .approved, and TokenPair.user.id becomes the durable account id. The start interval is 30 s and the test settles within 10 s, so it passes only when the loop follows poll_after. Each poll sends {"device_code": …}.
    • An approved poll marked temporary passes wire validation, but the view model still ends in .error and installs no tokens.
    • Removing the temporary refusal, or ignoring poll_after, makes these two tests fail.
  • The completed stack passed the full iOS suite (1981 tests) and tvOS suite (1792 tests) with 0 failures, built for macOS and passed the v1 path guard. Every branch in the stack builds on its own.
  • Not run: a manual QR sign-in against a server. No test covers a failed session install after an approved poll.

Risks and follow-ups

  • Rerun the QR device-login cases of the open validation issues TV client sign-in & onboarding — iOS #326 (iOS) and TV client sign-in & onboarding — tvOS #327 (tvOS) after this lands. This PR changes the wire and error shapes those cases exercise.
  • If an approved poll answer is lost in transit, the tokens cannot be collected again. The user sees "This code has already been used." and must start again. The server contract (non_retryable) causes this, so this PR does not change it.
  • PairingDeviceAPI, SiloControlClient and the pairing-only models in DeviceLoginModels.swift still use v1 device paths. W5-C and W5-D move them.

AI disclosure

Code and description written by Claude Opus 5.5 (claude-opus-5-5) agents in Claude Code (T3 Code), orchestrated by a Claude Code workflow.


Part 12 of 50 in the Apple API v2 stack. Base: apiv2-stack/11-sign-in-through-v2. Merge in order from the bottom of the stack. The plan and full index are in PR 01.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Sep 23, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 28f126d0-c368-47fa-8b1d-74a7548c439a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick104 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-23T13:37:41.760296Z 4e75806 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Quick104

Copy link
Copy Markdown
Contributor Author

Code review summary

Reviewed head: 17e6bdf5 (apiv2-stack/12-tvos-qr-sign-in-v2).

Process: an adversarial review of this PR's diff with each candidate finding independently verified before any fix, a simplify pass for medium and larger PRs, and a whole-stack review across contract conformance, authority and failure handling, data integrity, user-visible behavior and cross-PR integration.

Verified findings, all fixed on this branch:

  • minor: The approved-poll test repeats the view model's install step instead of running QRLoginViewModel

Validation: this branch builds on its own for iOS, tvOS and macOS, and its focused tests passed. The completed stack passed the full iOS suite (1981 tests) and tvOS suite (1792 tests) with 0 failures, the macOS build and the v1 path guard. The final stack was also exercised on iOS and tvOS simulators against a v1/v2 bridge server: sign-in (password and QR), restore, browse, search, detail, favorites and watchlist, playback (direct play, transcode, subtitles, stop), audiobooks, downloads with offline playback, settings, requests, profiles, push registration and diagnostics. Server logs showed no /api/v1 request from the new builds except the retained health probe.

Remaining: no open verified findings. Merge in stack order.

Mergeability: 8/10 — conditional. The approach holds up and every verified finding is fixed. Conditions: this PR's CI must pass (it was pending at review time), and the owner still plans a physical-device check of the completed stack.

Review by Claude Opus 5.5 (claude-opus-5-5) agents in Claude Code (T3 Code).

Quick104 and others added 2 commits September 23, 2026 09:32
The tvOS QR sign-in still opened and polled its pairing request through
/api/v1/auth/device/start and /api/v1/auth/device/poll. A 1.0 server
answers those with 410. The v1 poll answer also carried no verified
account id, so a QR sign-in installed an unverified account binding. On
a v1-only server the screen showed "Couldn't start sign-in" followed by
the raw error instead of the server-update message.

QRLoginViewModel now goes through AuthService to the shared
APIv2Client: POST /api/v2/auth/device/start (expects 201) and POST
/api/v2/auth/device/poll. Both are public and non_retryable, so each
call is dispatched once with no bearer. An approved poll installs the
nested TokenPair and binds TokenPair.user.id as the verified account.
installSession now requires an account id because every caller has
one. The poll loop follows the server's poll_after.

A failed poll is classified in one place. A v1-only server (legacy 404)
or a 410 client_upgrade_required answer shows the existing update
message. A 404 problem means the request expired. An incomplete
approval, a failed session install or an account change ends the
attempt, because the tokens are issued only once. Network errors and
other server errors keep polling until the code expires. A start
failure caused by an update requirement shows only the update message.

Stack-PR: W5-B
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The approved-poll test called AuthService.installSession itself, copying
the view model's step instead of running it. A change that bound the
wrong account, accepted a temporary session or ignored poll_after would
not have failed any test. QRLoginViewModel used the shared AuthService
and TokenStore, so tests could not run it.

QRLoginViewModel now takes its AuthService and TokenStore through init,
defaulting to the shared instances, so TVLoginView is unchanged. The new
tests drive begin() against the stubbed server: start, a pending poll
and an approved poll bind TokenPair.user.id, and settling in time shows
the loop followed poll_after. An approved temporary answer ends in an
error and installs no tokens.

Stack-PR: W5-B
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Base automatically changed from apiv2-stack/11-sign-in-through-v2 to main September 23, 2026 13:32
@Quick104
Quick104 force-pushed the apiv2-stack/12-tvos-qr-sign-in-v2 branch from c1182a6 to 4e75806 Compare September 23, 2026 13:32
@Quick104
Quick104 merged commit f69e854 into main Sep 23, 2026
3 of 6 checks passed
@Quick104
Quick104 deleted the apiv2-stack/12-tvos-qr-sign-in-v2 branch September 23, 2026 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant