Adds mobile token has auth header - #96
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
WalkthroughChargebee adds mobile-token provider configuration, stores and encodes refreshed tokens, uses them for authorization, and retries one HTTP 401 request after token refresh. The example app now uses token-provider configuration. ChangesMobile token authentication
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant AppDelegate
participant Chargebee
participant CBEnvironment
participant CBMobileTokenProvider
participant CBNetworkRequest
participant CBAPIRequest
AppDelegate->>Chargebee: configure with tokenProvider
Chargebee->>CBEnvironment: forward configuration
CBEnvironment->>CBMobileTokenProvider: request mobile token
CBMobileTokenProvider-->>CBEnvironment: return raw token
CBEnvironment->>CBAPIRequest: provide encoded token authorization
CBNetworkRequest->>CBAPIRequest: send authorized request
CBAPIRequest-->>CBNetworkRequest: return HTTP 401
CBNetworkRequest->>CBEnvironment: refresh mobile token
CBEnvironment->>CBMobileTokenProvider: request fresh token
CBMobileTokenProvider-->>CBEnvironment: return fresh token
CBNetworkRequest->>CBAPIRequest: retry once with refreshed authorization
🚥 Pre-merge checks | ❌ 1❌ Failed checks (1 warning)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@Chargebee/Classes/Configuration/CBEnvironment.swift`:
- Around line 81-100: Update configure so CBEnvironment.sdkKey is always reset
from the current optional value, using an empty value when sdkKey is nil,
instead of retaining prior state. In the API-key configuration path, also clear
the existing mobileToken and tokenProvider state so resolvedAuthHeader cannot
reuse a previous mobile-token configuration.
- Around line 116-123: Synchronize all accesses to CBEnvironment.mobileToken
across refreshMobileToken, request construction, and 401 retry handling. Use one
shared lock or serial state queue for both token reads and writes, ensuring
Authorization headers observe a consistent token value without changing the
existing refresh completion behavior.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Enterprise
Run ID: 636d631a-c0c7-4da9-8d21-60405e81f6a6
📒 Files selected for processing (6)
Chargebee/Classes/Authentication/CBAuthenticationResource.swiftChargebee/Classes/Configuration/CBEnvironment.swiftChargebee/Classes/Configuration/Chargebee.swiftChargebee/Classes/Network/CBAPIRequest.swiftChargebee/Classes/Network/CBNetworkRequest.swiftExample/Chargebee/AppDelegate.swift
| func configure(site: String, sdkKey: String? = nil, allowErrorLogging: Bool, tokenProvider: @escaping CBMobileTokenProvider, handler: @escaping CBAuthenticationHandler) { | ||
| CBEnvironment.site = site | ||
| CBEnvironment.apiKey = "" | ||
| CBEnvironment.encodedApiKey = "" | ||
| CBEnvironment.allowErrorLogging = allowErrorLogging | ||
| CBEnvironment.baseUrl = "https://\(site).chargebee.com/api" | ||
| CBEnvironment.version = .unknown | ||
| CBEnvironment.tokenProvider = tokenProvider | ||
| if let sdkKey = sdkKey { | ||
| CBEnvironment.sdkKey = sdkKey | ||
| } | ||
|
|
||
| let (onSuccess, onError) = CBResult.buildResultHandlers(handler, nil) | ||
| CBEnvironment.refreshMobileToken { success in | ||
| guard success else { | ||
| return onError(CBError.defaultSytemError(statusCode: 401, message: "Unable to fetch a mobile token from the token provider")) | ||
| } | ||
| guard CBEnvironment.sdkKey.isNotEmpty else { | ||
| // Nothing to verify without an SDK key; environment is ready. | ||
| return onSuccess(CBAuthenticationStatus(details: CBAuthentication(appId: nil, status: "ok", version: .unknown))) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Reset authentication state on reconfiguration.
If this overload receives sdkKey: nil, it retains a previous CBEnvironment.sdkKey. If API-key configuration follows mobile-token configuration, mobileToken and tokenProvider remain set, so resolvedAuthHeader still selects the old mobile token. Assign sdkKey = sdkKey ?? "" here, and clear mobile-token state in the API-key configuration path.
As per path instructions, “focus solely on correctness and safety.”
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@Chargebee/Classes/Configuration/CBEnvironment.swift` around lines 81 - 100,
Update configure so CBEnvironment.sdkKey is always reset from the current
optional value, using an empty value when sdkKey is nil, instead of retaining
prior state. In the API-key configuration path, also clear the existing
mobileToken and tokenProvider state so resolvedAuthHeader cannot reuse a
previous mobile-token configuration.
Source: Path instructions
| static func refreshMobileToken(completion: @escaping (Bool) -> Void) { | ||
| guard let provider = tokenProvider else { | ||
| return completion(false) | ||
| } | ||
| provider { token in | ||
| if let token = token, token.isNotEmpty { | ||
| CBEnvironment.mobileToken = token | ||
| completion(true) |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Synchronize mobile-token state.
The token-provider callback writes CBEnvironment.mobileToken while request construction and 401 retry handling read it on other execution paths. Concurrent mutable String access can send a stale or invalid Authorization header. Protect token reads and writes with one lock or serial state queue.
As per path instructions, “focus strictly on merge-blocking concerns.”
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@Chargebee/Classes/Configuration/CBEnvironment.swift` around lines 116 - 123,
Synchronize all accesses to CBEnvironment.mobileToken across refreshMobileToken,
request construction, and 401 retry handling. Use one shared lock or serial
state queue for both token reads and writes, ensuring Authorization headers
observe a consistent token value without changing the existing refresh
completion behavior.
Source: Path instructions
CHANGELOG
REPLACE_ME_WITH_CHANGELOG
SUMMARY
REPLACE_ME_WITH_SUMMARY_OF_THE_CHANGES
FUNCTIONAL AUTOMATION CHANGES PR
AUTOMATION TEST REPORT URL
REPLACE_ME_WITH_TEST_REPORT_URL
AREAS OF IMPACT
REPLACE_ME_WITH_AREAS_OF_IMPACT_OR_NA
TYPE OF CHANGE
DOCUMENTATION
REPLACE_ME_WITH_DOCUMENTATION_LINK_OR_NA
Adds mobile-token authentication through a new
Chargebee.configureoverload and token provider. Resolves mobile-token authorization headers, supports token refresh, and retries one HTTP 401 response. Updates the example app to use token-based configuration.