mirror of
https://github.com/Comfy-Org/comfy-swift-sdk.git
synced 2026-11-04 23:47:26 +00:00
[PR #46] [MERGED] refactor(auth): extract shared normalizeToken(_:) helper across auth sites #47
Labels
No labels
enhancement
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Comfy-Org/comfy-swift-sdk#47
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Comfy-Org/comfy-swift-sdk/pull/46
Author: @mattmillerai
Created: 7/11/2026
Status: ✅ Merged
Merged: 7/16/2026
Merged by: @mattmillerai
Base:
main← Head:matt/be-2862-normalize-token-helper📝 Commits (2)
458f818refactor(auth): extract shared normalizeToken(_:) helper across auth sites3284b74fix(auth): surface cancellation as .cancelled in normalizeToken, not .authInvalid📊 Changes
4 files changed (+173 additions, -33 deletions)
View changed files
➕
Sources/ComfySwiftSDK/Internal/Auth/TokenNormalization.swift(+30 -0)📝
Sources/ComfySwiftSDK/Internal/Transport.swift(+8 -24)📝
Sources/ComfySwiftSDK/Internal/WebSocketSession.swift(+2 -9)📝
Tests/ComfySwiftSDKTests/BearerInjectionTests.swift(+133 -0)📄 Description
ELI-5
Three different places in the SDK all had the same little rule copy-pasted: "ask for a
token; if it comes back empty or the token-fetcher blows up, treat it as a bad login
(
.authInvalid) — but if it throws a known auth error, pass that through untouched."Two of those places set the HTTP
Authorization: Bearerheader; the third builds theWebSocket
?token=URL. Because the rule was duplicated, the HTTP path and the WebSocketpath could silently drift apart over time on what "empty token" or "fetcher threw" means.
This PR moves that rule into one tiny helper,
normalizeToken(_:), and has all threesites call it. No behavior changes — same inputs still produce the exact same errors.
What changed
Sources/ComfySwiftSDK/Internal/Auth/TokenNormalization.swift— a pure,stateless
internal func normalizeToken(_:)that is the single source of truth for thecontract:
ComfyError.authInvalidComfyError→ rethrown unchangedComfyError.authInvalidTransport.applyAuth.oauthand.oauthRefreshablecases now delegate to it.The
refreshIfNearExpiry(...)call is kept inside the closure, so a non-ComfyErrorthrown by the refresh/expiry providers still maps to
.authInvalidexactly as before.WebSocketSession.buildWebSocketURL.oauth/.oauthRefreshablecase delegates toit, preserving the existing quirk that the WebSocket path does not refresh on
.oauthRefreshable(it reads thetokenProvideronly).BearerInjectionTestscovering the branches not directly exercised before — the.oauthHTTP header path for a throwing provider and an empty token, and the
.oauthRefreshableHTTP path where
refreshIfNearExpirythrows a non-ComfyError.Why it's safe (behavior preservation)
The old per-site
do/catchshape andnormalizeToken(_:)implement byte-identicalmapping logic. The one load-bearing subtlety is keeping
refreshIfNearExpiry(...)insidethe
normalizeTokenclosure in the refreshable case; moving it out would let anon-
ComfyErrorrefresh/expiry failure escape unmapped — a security-behavior change. Iverified the new refreshable test genuinely pins this by temporarily hoisting the call out
of the closure and confirming the test fails (
Expected .authInvalid, got ProviderFailure()),then reverted.
Verification
swift buildclean.swift testfully green — 229 tests, 28 suites, all pre-existing BearerInjection /CredentialMode / RefreshOn401 tests unchanged and passing.
grep -rn 'isEmpty else { throw ComfyError.authInvalid' Sources/returns onlyTokenNormalization.swift— no inline guard remains at any call site.⚠️ Security review requested
This touches the credential-injection predicate (Bearer header + WebSocket token query).
The change is a strictly structural refactor with no intended error-mapping outcome change,
but please give the auth-mapping equivalence a human security review before merge.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.