Skip to content

feat(auth): sign in and sign out through v2 - #359

Merged
Quick104 merged 2 commits into
mainfrom
apiv2-stack/11-sign-in-through-v2
Sep 23, 2026
Merged

Quick104 merged 2 commits into
mainfrom
apiv2-stack/11-sign-in-through-v2

Conversation

@Quick104

Copy link
Copy Markdown
Contributor

Password sign-in and both sign-out revocations now use /api/v2. A fresh sign-in also binds the session to the account id the server returns.

Problem

AuthService.login posted to /api/v1/auth/login (inventory row 3). HTTPClient.revokeSession (row 4) and the tvOS SiloRemote RemotePlaybackIdentityManager.end() (row 22) posted to /api/v1/auth/logout. A 1.0 server answers every v1 path except health with 410, so sign-in would fail and sign-out would never revoke the server session.

The v1 login answer carried an integer user.id that the app ignored. installSession wrote an unverified binding (accountID: nil), so TokenStore.captureDurableAccountAuth() returned nil after every fresh sign-in until something else bound the account.

Solution

  • Sign-in: AuthService.login calls APIv2Client.login (POST /api/v2/auth/login, non_retryable, public). The request goes out once with no bearer, and a 401 does not start a refresh. installSession gains an accountID parameter, and login passes TokenPair.user.id, so the session is bound to a verified account. The QR sign-in passes nil until W5-B moves it. APIv2Client.login now checks for 200 before it decodes the body.
  • Sign-in errors: LoginViewModel.message(for:) maps v2 problem statuses to form copy: 401 wrong credentials, 403 account can't sign in, 400/422 check the fields, 429 too many attempts. Silo's login rejects only with problem documents, so a bare 401 or 403 (for example from an authenticating proxy or WAF) shows the generic "The server returned HTTP N." text instead of blaming the credentials or the account. A bare 429 still reads as too many attempts, because rate limiters may answer without a problem body. An UpdateRequirement takes precedence: a v1-only server shows the existing "server needs to be updated" copy, and a 410 client_upgrade_required shows "Update Silo". Other errors keep their description.
  • Local-first sign-out (row 4): revokeSession changes only its path, to /api/v2/auth/logout. It still runs after local credentials are cleared, uses the captured bearer on its own URLSession with a 5 s timeout, never refreshes, and sends no profile headers.
  • SiloRemote end (row 22): RemotePlaybackIdentityManager.end() calls SiloAPI.shared.apiV2Client.logout(expectedAccount:) while the temporary scope is still active. v2 logout does not accept X-Profile-Id, so HTTPClient.requestData gains sendsProfile (default true). With false, it removes X-Profile-Id and X-Profile-Token after the auth headers are attached, and again on the resend after a 401 refresh. Logout is natural_idempotent, so that one resend is safe. APIv2Client.logout still requires 204, and the caller still ignores failures (try?).
  • Deleted: the v1 LoginResponse model. LoginRequest stays as the v2 LoginInputBody (a nil provider is omitted). AuthUser stays because the v1 QR poll still uses it until W5-B.

Failure outcomes

Operation Definite success Definite failure Uncertain (sent, no answer)
login (non_retryable) 200 with complete tokens: install the session bound to user.id and clear the old profile Any non-2xx, an incomplete body, or an identity change: the previous session and profile stay, and the mapped message is shown once Error shown, nothing installed. No automatic resend; signing in again starts a new login session
Persistent sign-out revoke 204: nothing more to do Ignored: local sign-out already finished Ignored: the server session expires on its own
SiloRemote end logout (natural_idempotent) 204, then the temporary scope ends Ignored, and the temporary scope still ends. A v1-only verdict skips the call Ignored, and the scope ends. The temporary session expires server-side

Validation

  • iOS, tvOS and macOS builds passed.
  • iOS tests for AuthDeviceV2Tests, AccountSessionPersistenceTests, SettingValuesAPITests, UpdateRequirementTests, RefreshSessionV2Tests, RemotePlaybackRoutingTests, APIv2ContractStateTests, RestoredSessionValidatorTests and CredentialIdentityTests passed: 181 tests, 0 failures. The same nine classes under SiloTVTests passed: 171 tests, 0 failures.
  • scripts/ci/check-no-api-v1.sh passed. No auth/login or auth/logout v1 literal remains in the app or tests. The owner-fence count in iosApp/iosApp stays at 60 lines.
  • New or changed tests:
    • Login installs into the injected store over /api/v2/auth/login and binds captureDurableAccountAuth().accountID to the returned user.id.
    • A 401 problem leaves the previous session, profile and binding in place without a refresh.
    • The sign-out revoke goes to /api/v2/auth/logout with the bearer and no X-Profile-Id/X-Profile-Token.
    • APIv2Client.logout sends only the bearer, both for a persistent session with a profile proof and for a SiloRemote temporary scope. The resend after a refresh also omits the profile header.
    • APIv2Client.logout requires 204, and a v1-only verdict sends nothing.
    • The account-bound logout still refuses to dispatch after a server switch.
    • Login messages follow the problem status, and update requirements take precedence. A bare 401 or 403 shows the generic HTTP text; a bare 429 shows the rate-limit copy.
  • Mutation check: with sendsProfile: true in APIv2Client.logout, testLogoutSendsOnlyTheBearer fails on the leaked remote-proof header.
  • 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 sign-in and sign-out against a bridge server on a simulator or device, and a signed build.

Risks and follow-ups

  • Error copy: the client now picks the login copy from the status and no longer shows the server's own message for those statuses. Unlisted statuses (for example 500 or 503) still show the problem detail.
  • Stale v1-only verdict: if the server is updated in place while the login screen is open, sign-in keeps showing the server-update message until the verdict is re-probed on the next foreground or server refresh. W1-B added a re-probe for the restored-session validator. Sign-in could reuse it, but that needs an injectable verdict in AuthService to test, so this PR leaves it out.
  • Temporary-scope logout: sendsProfile applies only to account-scoped requestData calls; scoped calls with a requestIdentity always name a profile. The server contract does not state the X-Profile-Id refusal as an explicit gate. This PR follows the plan and inventory (I15) and never sends the header on logout.
  • Open PR fix(downloads): retarget store on profile and server switch #181 (download retarget) also edits AuthService.swift. The plan asks for it to land first if possible. Whichever lands second needs a rebase; the installSession signature change is the likely conflict.
  • W5-B moves the QR poll to v2 and passes the verified user.id into installSession. AuthUser and ImpersonationInfo can go once pairing (W5-C) moves too.

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 11 of 50 in the Apple API v2 stack. Base: apiv2-stack/10-validate-servers-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

Warning

Review limit reached

Next included review available in 9 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 9df92211-b1a6-4971-a362-8aa87f51dc97

📥 Commits

Reviewing files that changed from the base of the PR and between e39d74c and ca3214f.

📒 Files selected for processing (10)
  • iosApp/Tests/AccountSessionPersistenceTests.swift
  • iosApp/Tests/AuthDeviceV2Tests.swift
  • iosApp/Tests/SettingValuesAPITests.swift
  • iosApp/iosApp/Control/tvOS/RemotePlaybackIdentityManager.swift
  • iosApp/iosApp/Networking/APIv2/APIv2Client.swift
  • iosApp/iosApp/Networking/AuthTokenModels.swift
  • iosApp/iosApp/Networking/HTTPClient.swift
  • iosApp/iosApp/Screens/Auth/AuthService.swift
  • iosApp/iosApp/Screens/Auth/LoginViewModel.swift
  • iosApp/iosApp/Screens/Auth/QRLoginViewModel.swift

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:36:49.224650Z ca3214f 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: 78cd1d21 (apiv2-stack/11-sign-in-through-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: Non-problem 401/403 responses show credential or account-disabled copy

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).

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 78cd1d21e3

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

"/api/v1/auth/logout",
// Best effort: the temporary session expires server-side when the
// revoke is refused, fails, or is skipped for a v1-only server.
try? await SiloAPI.shared.apiV2Client.logout(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Gate temporary logout against its own server

When SiloRemote is using a different server from the saved active server, this call still enters APIv2Client.gate(), whose default verdict is ConnectionMonitor.isServerUpdateRequired for the registry's active server rather than scope.serverId. If the saved server is marked v1-only while the temporary server supports v2, ending remote playback skips the logout and leaves that temporary server session valid until expiry. Bypass the unrelated global gate here or evaluate the contract verdict for the temporary scope's server.

Useful? React with 👍 / 👎.

@Quick104
Quick104 force-pushed the apiv2-stack/11-sign-in-through-v2 branch from 78cd1d2 to 62dfb06 Compare September 23, 2026 13:19
@Quick104
Quick104 force-pushed the apiv2-stack/10-validate-servers-through-v2 branch from 05908e5 to 5d7ac1b Compare September 23, 2026 13:32
Base automatically changed from apiv2-stack/10-validate-servers-through-v2 to main September 23, 2026 13:32
Quick104 and others added 2 commits September 23, 2026 09:32
Password sign-in still posted to /api/v1/auth/login, and both sign-out
revocations posted to /api/v1/auth/logout. A 1.0 server answers every
v1 path except health with 410, so these calls would stop working. The
v1 login answer also carried an integer user id that the app never
bound, so a fresh sign-in left the session without a verified account.

AuthService.login now calls APIv2Client.login (POST /api/v2/auth/login)
and installs the session with TokenPair.user.id as its verified account
id. The QR sign-in keeps an unverified binding until it moves to v2.
LoginViewModel maps v2 problem statuses (401, 403, 400/422, 429) to form
copy and shows the existing update messages for a v1-only server or a
410 client_upgrade_required answer.

Sign-out stays local-first: HTTPClient.revokeSession only changes its
path to /api/v2/auth/logout and still sends just the captured bearer.
The SiloRemote temporary scope on tvOS now ends through
APIv2Client.logout. HTTPClient.requestData gains sendsProfile so that
call drops X-Profile-Id and X-Profile-Token, which v2 logout does not
accept, including on the resend after a token refresh. The unused v1
LoginResponse model is deleted.

Stack-PR: W5-A
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A proxy or WAF in front of the server can answer the v2 login with a
401 or 403 and a non-problem body. The login form mapped those bare
statuses to "Incorrect username or password." or the account-disabled
copy, even though Silo's login only rejects with problem documents.

Map 401, 403, 400 and 422 only for problem documents. A bare 429 still
reads as rate limiting; other bare statuses show the generic HTTP text.

Stack-PR: W5-A
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Quick104
Quick104 force-pushed the apiv2-stack/11-sign-in-through-v2 branch from 62dfb06 to ca3214f Compare September 23, 2026 13:32
@Quick104
Quick104 merged commit b217760 into main Sep 23, 2026
3 of 6 checks passed
@Quick104
Quick104 deleted the apiv2-stack/11-sign-in-through-v2 branch September 23, 2026 13:32
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