Add complete OAuth 2.0 Authorization Code Flow with PKCE as alternative to API token authentication, with automatic server detection and graceful fallback to classic login. **OAuth Core (RFC 7636 PKCE):** - PKCEGenerator: S256 challenge generation for secure code exchange - OAuth DTOs: Client registration, token request/response models - OAuthClient, OAuthToken, AuthenticationMethod domain models - API.swift: registerOAuthClient() and exchangeOAuthToken() endpoints - OAuthRepository + POAuthRepository protocol **Browser Integration (ASWebAuthenticationSession):** - OAuthSession: Wraps native authentication session - OAuthFlowCoordinator: Orchestrates 5-phase OAuth flow - readeck:// URL scheme for OAuth callback handling - State verification for CSRF protection - User cancellation handling **Token Management:** - KeychainHelper: OAuth token storage alongside API tokens - TokenProvider: getOAuthToken(), setOAuthToken(), getAuthMethod() - AuthenticationMethod enum to distinguish token types - AuthRepository: loginWithOAuth(), getAuthenticationMethod() - Endpoint persistence in both Keychain and Settings **Server Feature Detection:** - ServerInfo extended with features array and supportsOAuth flag - GET /api/info endpoint integration (backward compatible) - GetServerInfoUseCase with optional endpoint parameter **User Profile Integration:** - ProfileApiClient: Fetch user data via GET /api/profile - UserProfileDto with username, email, provider information - GetUserProfileUseCase: Extract username from profile - Username saved and displayed for OAuth users (like classic auth) **Automatic OAuth Flow (No User Selection):** - OnboardingServerView: 2-phase flow (endpoint → auto-OAuth or classic) - OAuth attempted automatically if server supports it - Fallback to username/password on OAuth failure or unsupported - SettingsServerViewModel: checkServerOAuthSupport(), loginWithOAuth() **Cleanup & Refactoring:** - Remove all #if os(iOS) && !APP_EXTENSION conditionals - Remove LoginMethodSelectionView (no longer needed) - Remove switchToClassicLogin() method - Factories updated with OAuth dependencies **Testing:** - PKCEGeneratorTests: Verify RFC 7636 compliance - ServerInfoTests: Feature detection and backward compatibility - Mock implementations for all OAuth components **Documentation:** - docs/OAuth2-Implementation-Plan.md: Complete implementation guide - openapi.json: Readeck API specification **Scopes Requested:** - bookmarks:read, bookmarks:write, profile:read OAuth users now have full feature parity with classic authentication. Server auto-detects OAuth support via /info endpoint. Seamless UX with browser-based login and automatic fallback.
109 lines
2.9 KiB
Swift
109 lines
2.9 KiB
Swift
import Foundation
|
|
|
|
protocol TokenProvider {
|
|
func getToken() async -> String?
|
|
func getEndpoint() async -> String?
|
|
func setToken(_ token: String) async
|
|
func setEndpoint(_ endpoint: String) async
|
|
func clearToken() async
|
|
|
|
// OAuth methods
|
|
func getOAuthToken() async -> OAuthToken?
|
|
func setOAuthToken(_ token: OAuthToken) async
|
|
func getAuthMethod() async -> AuthenticationMethod?
|
|
func setAuthMethod(_ method: AuthenticationMethod) async
|
|
}
|
|
|
|
class KeychainTokenProvider: TokenProvider {
|
|
private let keychainHelper = KeychainHelper.shared
|
|
|
|
// Cache to avoid repeated keychain access
|
|
private var cachedToken: String?
|
|
private var cachedEndpoint: String?
|
|
private var cachedOAuthToken: OAuthToken?
|
|
private var cachedAuthMethod: AuthenticationMethod?
|
|
|
|
func getToken() async -> String? {
|
|
// Check auth method first
|
|
if let method = await getAuthMethod(), method == .oauth {
|
|
// Return OAuth access token if using OAuth
|
|
if let oauthToken = await getOAuthToken() {
|
|
return oauthToken.accessToken
|
|
}
|
|
}
|
|
|
|
// Otherwise return API token
|
|
if let cached = cachedToken {
|
|
return cached
|
|
}
|
|
|
|
let token = keychainHelper.loadToken()
|
|
cachedToken = token
|
|
return token
|
|
}
|
|
|
|
func getEndpoint() async -> String? {
|
|
if let cached = cachedEndpoint {
|
|
return cached
|
|
}
|
|
|
|
let endpoint = keychainHelper.loadEndpoint()
|
|
cachedEndpoint = endpoint
|
|
return endpoint
|
|
}
|
|
|
|
func setToken(_ token: String) async {
|
|
keychainHelper.saveToken(token)
|
|
keychainHelper.saveAuthMethod(.apiToken)
|
|
cachedAuthMethod = .apiToken
|
|
cachedToken = token
|
|
}
|
|
|
|
func setEndpoint(_ endpoint: String) async {
|
|
keychainHelper.saveEndpoint(endpoint)
|
|
cachedEndpoint = endpoint
|
|
}
|
|
|
|
func clearToken() async {
|
|
keychainHelper.clearCredentials()
|
|
cachedToken = nil
|
|
cachedEndpoint = nil
|
|
cachedOAuthToken = nil
|
|
cachedAuthMethod = nil
|
|
}
|
|
|
|
// MARK: - OAuth Methods
|
|
|
|
func getOAuthToken() async -> OAuthToken? {
|
|
if let cached = cachedOAuthToken {
|
|
return cached
|
|
}
|
|
|
|
let token = keychainHelper.loadOAuthToken()
|
|
cachedOAuthToken = token
|
|
return token
|
|
}
|
|
|
|
func setOAuthToken(_ token: OAuthToken) async {
|
|
keychainHelper.saveOAuthToken(token)
|
|
keychainHelper.saveAuthMethod(.oauth)
|
|
cachedOAuthToken = token
|
|
cachedAuthMethod = .oauth
|
|
}
|
|
|
|
func getAuthMethod() async -> AuthenticationMethod? {
|
|
if let cached = cachedAuthMethod {
|
|
return cached
|
|
}
|
|
|
|
let method = keychainHelper.loadAuthMethod()
|
|
cachedAuthMethod = method
|
|
return method
|
|
}
|
|
|
|
func setAuthMethod(_ method: AuthenticationMethod) async {
|
|
keychainHelper.saveAuthMethod(method)
|
|
cachedAuthMethod = method
|
|
}
|
|
}
|