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.
67 lines
2.5 KiB
Swift
67 lines
2.5 KiB
Swift
import Foundation
|
|
|
|
class AuthRepository: PAuthRepository {
|
|
private let api: PAPI
|
|
private let settingsRepository: PSettingsRepository
|
|
private let getUserProfileUseCase: PGetUserProfileUseCase
|
|
|
|
init(api: PAPI, settingsRepository: PSettingsRepository, getUserProfileUseCase: PGetUserProfileUseCase) {
|
|
self.api = api
|
|
self.settingsRepository = settingsRepository
|
|
self.getUserProfileUseCase = getUserProfileUseCase
|
|
}
|
|
|
|
func login(endpoint: String, username: String, password: String) async throws -> User {
|
|
let userDto = try await api.login(endpoint: endpoint, username: username, password: password)
|
|
// Token wird automatisch von der API gespeichert
|
|
await api.tokenProvider.setAuthMethod(.apiToken)
|
|
return User(id: userDto.id, token: userDto.token)
|
|
}
|
|
|
|
func logout() async throws {
|
|
await api.tokenProvider.clearToken()
|
|
await api.tokenProvider.setAuthMethod(.apiToken)
|
|
}
|
|
|
|
func getCurrentSettings() async throws -> Settings? {
|
|
return try await settingsRepository.loadSettings()
|
|
}
|
|
|
|
func loginWithOAuth(endpoint: String, token: OAuthToken) async throws {
|
|
// Save OAuth token, auth method, and endpoint
|
|
await api.tokenProvider.setOAuthToken(token)
|
|
await api.tokenProvider.setAuthMethod(.oauth)
|
|
await api.tokenProvider.setEndpoint(endpoint)
|
|
|
|
// Fetch username from user profile
|
|
let username = try await getUserProfileUseCase.execute()
|
|
|
|
// Save endpoint and username to settings (token is stored in keychain via tokenProvider)
|
|
if var settings = try await settingsRepository.loadSettings() {
|
|
settings.endpoint = endpoint
|
|
settings.username = username
|
|
// Note: isLoggedIn is a computed property based on token presence
|
|
// The OAuth token is already saved via tokenProvider above
|
|
try await settingsRepository.saveSettings(settings)
|
|
}
|
|
}
|
|
|
|
func getAuthenticationMethod() async -> AuthenticationMethod? {
|
|
return await api.tokenProvider.getAuthMethod()
|
|
}
|
|
|
|
func switchToClassicAuth(endpoint: String, username: String, password: String) async throws -> User {
|
|
// Clear OAuth token first
|
|
await api.tokenProvider.clearToken()
|
|
await api.tokenProvider.setAuthMethod(.apiToken)
|
|
|
|
// Then do regular login
|
|
return try await login(endpoint: endpoint, username: username, password: password)
|
|
}
|
|
}
|
|
|
|
struct User {
|
|
let id: String
|
|
let token: String
|
|
}
|