Skip to content

Conversation

@augard
Copy link
Owner

@augard augard commented Sep 3, 2025

Summary

  • Implements standard OAuth2 refresh token flow for better token management
  • Adds fallback to credential-based login when refresh token fails
  • Updates ApiRequest protocol to support authorization header control

Changes

  • ✨ Added dedicated refreshToken() method in Api class for OAuth2 refresh token grant type
  • 🔄 Updated refreshTokenIfPossible() to attempt refresh token flow before credential fallback
  • 🛠️ Modified TokenResponse model to make refresh token optional (needed for refresh responses)
  • 🔧 Enhanced ApiRequest protocol with authorization parameter to control header inclusion
  • ✅ Updated test implementations to match the new ApiRequest protocol

Implementation Details

The token refresh now follows OAuth2 best practices with this priority:

  1. Primary: Use refresh_token grant type if a refresh token is available
  2. Fallback: Use stored credentials for full re-authentication if refresh fails
  3. Preservation: Device ID and stamp are maintained during token refresh

Test Plan

  • Verify refresh token flow works when token expires
  • Confirm fallback to credential login when refresh fails
  • Ensure device ID and stamp are preserved
  • Test that authorization headers are properly controlled
  • Verify all existing tests pass with updated ApiRequest protocol

🤖 Generated with Claude Code

- Add dedicated refreshToken() method to Api class for standard OAuth2 token refresh
- Update refreshTokenIfPossible() to first attempt refresh token flow before fallback
- Make TokenResponse.refreshToken optional to handle refresh responses correctly
- Update ApiRequest protocol to support authorization header control
- Add authorization parameter to all ApiRequest initializers
- Fix test implementations to match updated ApiRequest protocol

The implementation now follows OAuth2 best practices:
1. First attempts to refresh using the refresh_token grant type
2. Falls back to credential-based login only if refresh fails
3. Properly preserves device ID and stamp during refresh

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings September 3, 2025 14:10
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements proper OAuth2 refresh token flow for better token management and adds fallback to credential-based login when refresh token fails.

Key changes:

  • Added dedicated OAuth2 refresh token flow with fallback to credential-based authentication
  • Enhanced ApiRequest protocol to support authorization header control
  • Updated TokenResponse model to make refresh token optional for refresh responses

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
KiaMaps/Core/Api/Api.swift Added new refreshToken() method for OAuth2 refresh token grant type
KiaMaps/App/ApiExtensions.swift Updated token refresh logic to try refresh token first, then fallback to credentials
KiaMaps/Core/Api/Models/AuthenticationModels.swift Made refresh token optional in TokenResponse model
KiaMaps/Core/Api/ApiRequest.swift Enhanced protocol and implementation with authorization parameter
KiaTests/AuthenticationTests.swift Updated mock implementation to match new ApiRequest protocol
KiaTests/UIComponentMockDataTests.swift Fixed parameter name casing in test calls
Comments suppressed due to low confidence (1)

KiaMaps/Core/Api/Models/AuthenticationModels.swift:1

  • Using an empty string as fallback for missing refresh token could cause issues. Consider handling the nil case explicitly or documenting why an empty string is appropriate here.
//

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@augard augard merged commit ba95d48 into main Sep 3, 2025
3 checks passed
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