Skip to content

Adds mobile token has auth header - #96

Open
cb-haripriyan wants to merge 1 commit into
masterfrom
feat/OMNISUB-7609
Open

Adds mobile token has auth header#96
cb-haripriyan wants to merge 1 commit into
masterfrom
feat/OMNISUB-7609

Conversation

@cb-haripriyan

@cb-haripriyan cb-haripriyan commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

CHANGELOG

REPLACE_ME_WITH_CHANGELOG

SUMMARY

REPLACE_ME_WITH_SUMMARY_OF_THE_CHANGES

FUNCTIONAL AUTOMATION CHANGES PR

  • Yes
    • If Yes, PR :
  • No
    • If No, Reason:

AUTOMATION TEST REPORT URL

REPLACE_ME_WITH_TEST_REPORT_URL

AREAS OF IMPACT

REPLACE_ME_WITH_AREAS_OF_IMPACT_OR_NA

TYPE OF CHANGE

  • 🐞 Bugfix
  • 🌟 Feature
  • ✨ Enhancement
  • 🧪 Unit Test Cases
  • 📔 Documentation
  • ⚙️ Chore - Build Related / Configuration / Others

DOCUMENTATION

REPLACE_ME_WITH_DOCUMENTATION_LINK_OR_NA

Adds mobile-token authentication through a new Chargebee.configure overload 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.

@snyk-io

snyk-io Bot commented Aug 6, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues
Secrets 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

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

Changes

Mobile token authentication

Layer / File(s) Summary
Configure mobile token authentication
Chargebee/Classes/Configuration/CBEnvironment.swift, Chargebee/Classes/Configuration/Chargebee.swift
Adds the public token provider contract, token state, token refresh handling, and a Chargebee.configure overload for mobile-token authentication.
Resolve request authorization
Chargebee/Classes/Network/CBAPIRequest.swift, Chargebee/Classes/Authentication/CBAuthenticationResource.swift
Uses the encoded mobile token when available and adds the Authorization header only when a value resolves.
Refresh and retry unauthorized requests
Chargebee/Classes/Network/CBNetworkRequest.swift, Example/Chargebee/AppDelegate.swift
Retries one eligible HTTP 401 response after token refresh. The example app supplies a token provider and logs configuration results.

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
Loading
🚥 Pre-merge checks | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

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

@coderabbitai coderabbitai 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.

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

📥 Commits

Reviewing files that changed from the base of the PR and between a2f0eaa and 503b910.

📒 Files selected for processing (6)
  • Chargebee/Classes/Authentication/CBAuthenticationResource.swift
  • Chargebee/Classes/Configuration/CBEnvironment.swift
  • Chargebee/Classes/Configuration/Chargebee.swift
  • Chargebee/Classes/Network/CBAPIRequest.swift
  • Chargebee/Classes/Network/CBNetworkRequest.swift
  • Example/Chargebee/AppDelegate.swift

Comment on lines +81 to +100
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)))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 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

Comment on lines +116 to +123
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 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

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