Skip to content

Commit

Permalink
Fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mohannad-hassan committed Dec 24, 2024
1 parent 0a7da60 commit 7825940
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 54 deletions.
73 changes: 45 additions & 28 deletions Data/OAuthClient/Sources/AppAuthOAuthClient.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
//
// File.swift
// AppAuthOAuthClient.swift
// QuranEngine
//
// Created by Mohannad Hassan on 23/12/2024.
//

import AppAuth
import Foundation
import UIKit
import AppAuth
import VLogging

public final class AppAuthOAuthClient: OAuthClient {

// Needed mainly for retention.
private var authFlow: (any OIDExternalUserAgentSession)?
private var appConfiguration: OAuthAppConfiguration?
// MARK: Lifecycle

public init() {}

// MARK: Public

public func set(appConfiguration: OAuthAppConfiguration) {
self.appConfiguration = appConfiguration
}

public func login(on viewController: UIViewController) async throws {
guard let configuration = self.appConfiguration else {
guard let configuration = appConfiguration else {
logger.error("login invoked without OAuth client configurations being set")
throw OAuthClientError.oauthClientHasNotBeenSet
}

// Quran.com relies on dicovering the service configuration from the issuer,
// and not using a static configuration.
let serviceConfiguration = try await discoverConfiguration(forIssuer: configuration.authorizationIssuerURL)
try await login(withConfiguration: serviceConfiguration,
appConfiguration: configuration,
on: viewController)
try await login(
withConfiguration: serviceConfiguration,
appConfiguration: configuration,
on: viewController
)
}

// MARK: Private

// Needed mainly for retention.
private var authFlow: (any OIDExternalUserAgentSession)?
private var appConfiguration: OAuthAppConfiguration?

// MARK: - Authenication Flow

private func discoverConfiguration(forIssuer issuer: URL) async throws -> OIDServiceConfiguration {
logger.info("Discovering configuration for OAuth")
return try await withCheckedThrowingContinuation { continuation in
Expand All @@ -46,7 +55,7 @@ public final class AppAuthOAuthClient: OAuthClient {
continuation.resume(throwing: OAuthClientError.errorFetchingConfiguration(error))
return
}
guard let configuration = configuration else {
guard let configuration else {
// This should not happen
logger.error("Error fetching OAuth configuration: no cofniguration was loaded. An unexpected situtation.")
continuation.resume(throwing: OAuthClientError.errorFetchingConfiguration(nil))
Expand All @@ -58,17 +67,21 @@ public final class AppAuthOAuthClient: OAuthClient {
}
}

private func login(withConfiguration configuration: OIDServiceConfiguration,
appConfiguration: OAuthAppConfiguration,
on viewController: UIViewController) async throws {
private func login(
withConfiguration configuration: OIDServiceConfiguration,
appConfiguration: OAuthAppConfiguration,
on viewController: UIViewController
) async throws {
let scopes = [OIDScopeOpenID, OIDScopeProfile] + appConfiguration.scopes
let request = OIDAuthorizationRequest(configuration: configuration,
clientId: appConfiguration.clientID,
clientSecret: nil,
scopes: scopes,
redirectURL: appConfiguration.redirectURL,
responseType: OIDResponseTypeCode,
additionalParameters: [:])
let request = OIDAuthorizationRequest(
configuration: configuration,
clientId: appConfiguration.clientID,
clientSecret: nil,
scopes: scopes,
redirectURL: appConfiguration.redirectURL,
responseType: OIDResponseTypeCode,
additionalParameters: [:]
)

logger.info("Starting OAuth flow")
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
Expand All @@ -78,7 +91,7 @@ public final class AppAuthOAuthClient: OAuthClient {
continuation.resume(throwing: OAuthClientError.errorAuthenticating(error))
return
}
guard let state = state else {
guard let _ = state else {
logger.error("Error authenticating: no state returned. An unexpected situtation.")
continuation.resume(throwing: OAuthClientError.errorAuthenticating(nil))
return
Expand All @@ -90,14 +103,18 @@ public final class AppAuthOAuthClient: OAuthClient {
}

/// Executes the request on the main actor.
private func fire(loginRequest: OIDAuthorizationRequest,
on viewController: UIViewController,
callback: @escaping OIDAuthStateAuthorizationCallback) {
private func fire(
loginRequest: OIDAuthorizationRequest,
on viewController: UIViewController,
callback: @escaping OIDAuthStateAuthorizationCallback
) {
Task {
await MainActor.run {
self.authFlow = OIDAuthState.authState(byPresenting: loginRequest,
presenting: viewController) { state, error in
self.authFlow = nil
authFlow = OIDAuthState.authState(
byPresenting: loginRequest,
presenting: viewController
) { [weak self] state, error in
self?.authFlow = nil
callback(state, error)
}
}
Expand Down
3 changes: 1 addition & 2 deletions Data/OAuthClient/Sources/OAuthClient.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// OAuthClient.swift
// QuranEngine
//
// Created by Mohannad Hassan on 19/12/2024.
Expand Down Expand Up @@ -34,7 +34,6 @@ public struct OAuthAppConfiguration {
///
/// Note that the connection relies on dicvoering the configuration from the issuer service.
public protocol OAuthClient {

func set(appConfiguration: OAuthAppConfiguration)

/// Performs the login flow to Quran.com
Expand Down
6 changes: 2 additions & 4 deletions Domain/QuranProfileService/Sources/QuranProfileService.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//
// File.swift
// QuranProfileService.swift
// QuranEngine
//
// Created by Mohannad Hassan on 23/12/2024.
//

import UIKit
import OAuthClient
import UIKit

public class QuranProfileService {

private let oauthClient: OAuthClient

public init(oauthClient: OAuthClient) {
Expand All @@ -24,4 +23,3 @@ public class QuranProfileService {
try await oauthClient.login(on: viewController)
}
}

2 changes: 1 addition & 1 deletion Example/QuranEngineApp/Classes/Container.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import CoreDataPersistence
import Foundation
import LastPagePersistence
import NotePersistence
import OAuthClient
import PageBookmarkPersistence
import ReadingService
import OAuthClient
import UIKit

/// Hosts singleton dependencies
Expand Down
2 changes: 1 addition & 1 deletion Features/AppDependencies/AppDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import BatchDownloader
import Foundation
import LastPagePersistence
import NotePersistence
import OAuthClient
import PageBookmarkPersistence
import QuranResources
import QuranTextKit
import ReadingService
import OAuthClient

public protocol AppDependencies {
var databasesURL: URL { get }
Expand Down
2 changes: 1 addition & 1 deletion Features/SettingsFeature/SettingsBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import AppDependencies
import AudioDownloadsFeature
import Localization
import QuranProfileService
import ReadingSelectorFeature
import SettingsService
import QuranProfileService
import SwiftUI
import TranslationsFeature
import UIKit
Expand Down
1 change: 1 addition & 0 deletions Features/SettingsFeature/SettingsRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct SettingsRootView: View {

private struct SettingsRootViewUI: View {
@Binding var theme: Theme

let audioEnd: String
let navigateToAudioEndSelector: AsyncAction
let navigateToAudioManager: AsyncAction
Expand Down
26 changes: 13 additions & 13 deletions Features/SettingsFeature/SettingsRootViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import Localization
import NoorUI
import QuranAudio
import QuranAudioKit
import QuranProfileService
import ReadingSelectorFeature
import SettingsService
import QuranProfileService
import TranslationsFeature
import UIKit
import UIx
Expand Down Expand Up @@ -51,19 +51,19 @@ final class SettingsRootViewModel: ObservableObject {

// MARK: Internal

private let analytics: AnalyticsLibrary
private let reviewService: ReviewService
private let quranProfileService: QuranProfileService
private let audioDownloadsBuilder: AudioDownloadsBuilder
private let translationsListBuilder: TranslationsListBuilder
private let readingSelectorBuilder: ReadingSelectorBuilder
private let diagnosticsBuilder: DiagnosticsBuilder
let analytics: AnalyticsLibrary
let reviewService: ReviewService
let quranProfileService: QuranProfileService
let audioDownloadsBuilder: AudioDownloadsBuilder
let translationsListBuilder: TranslationsListBuilder
let readingSelectorBuilder: ReadingSelectorBuilder
let diagnosticsBuilder: DiagnosticsBuilder

private let contactUsService = ContactUsService()
private let themeService = ThemeService.shared
private let audioPreferences = AudioPreferences.shared
let contactUsService = ContactUsService()
let themeService = ThemeService.shared
let audioPreferences = AudioPreferences.shared

private weak var navigationController: UINavigationController?
weak var navigationController: UINavigationController?

@Published var audioEnd: AudioEnd

Expand Down Expand Up @@ -138,7 +138,7 @@ final class SettingsRootViewModel: ObservableObject {
return
}
do {
try await self.quranProfileService.login(on: viewController)
try await quranProfileService.login(on: viewController)
// TODO: Replace with the needed UI changes.
print("Login seems successful")
} catch {
Expand Down
9 changes: 5 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let package = Package(

// Async
.package(url: "https://github.com/apple/swift-async-algorithms", from: "0.1.0"),

// OAuth
.package(url: "https://github.com/openid/AppAuth-iOS", .upToNextMajor(from: "1.3.0")),

Expand Down Expand Up @@ -298,11 +298,12 @@ private func dataTargets() -> [[Target]] {
"BatchDownloader",
"NetworkSupportFake",
]),

// MARK: - Quran.com OAuth

target(type, name: "OAuthClient", hasTests: false, dependencies: [
.product(name: "AppAuth", package: "AppAuth-iOS"),
])
]),
]
}

Expand Down Expand Up @@ -469,7 +470,7 @@ private func domainTargets() -> [[Target]] {

target(type, name: "QuranProfileService", hasTests: false, dependencies: [
"OAuthClient",
])
]),
]
}

Expand Down

0 comments on commit 7825940

Please sign in to comment.