Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(auth): increase code coverage #648

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions .swiftpm/xcode/xcshareddata/xcschemes/Auth.xcscheme
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1530"
version = "1.7">
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
Expand All @@ -27,8 +27,29 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES">
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Auth"
BuildableName = "Auth"
BlueprintName = "Auth"
ReferencedContainer = "container:">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AuthTests"
BuildableName = "AuthTests"
BlueprintName = "AuthTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
5 changes: 3 additions & 2 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
private var eventEmitter: AuthStateChangeEventEmitter { Dependencies[clientID].eventEmitter }
private var logger: (any SupabaseLogger)? { Dependencies[clientID].configuration.logger }
private var sessionStorage: SessionStorage { Dependencies[clientID].sessionStorage }
private var pkce: PKCE { Dependencies[clientID].pkce }

/// Returns the session, refreshing it if necessary.
///
Expand Down Expand Up @@ -1310,10 +1311,10 @@
return (nil, nil)
}

let codeVerifier = PKCE.generateCodeVerifier()
let codeVerifier = pkce.generateCodeVerifier()
codeVerifierStorage.set(codeVerifier)

let codeChallenge = PKCE.generateCodeChallenge(from: codeVerifier)
let codeChallenge = pkce.generateCodeChallenge(codeVerifier)
let codeChallengeMethod = codeVerifier == codeChallenge ? "plain" : "s256"

return (codeChallenge, codeChallengeMethod)
Expand Down Expand Up @@ -1403,7 +1404,7 @@
final class DefaultPresentationContextProvider: NSObject,
ASWebAuthenticationPresentationContextProviding
{
func presentationAnchor(for _: ASWebAuthenticationSession) -> ASPresentationAnchor {

Check warning on line 1407 in Sources/Auth/AuthClient.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (15) (IOS, 15.2)

main actor-isolated instance method 'presentationAnchor(for:)' cannot be used to satisfy nonisolated protocol requirement

Check warning on line 1407 in Sources/Auth/AuthClient.swift

View workflow job for this annotation

GitHub Actions / xcodebuild (15) (MACOS, 15.2)

main actor-isolated instance method 'presentationAnchor(for:)' cannot be used to satisfy nonisolated protocol requirement
ASPresentationAnchor()
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/Auth/AuthMFA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public struct AuthMFA: Sendable {
///
/// - Parameter params: The parameters for verifying the MFA factor.
/// - Returns: An authentication response after verifying the factor.
@discardableResult
public func verify(params: MFAVerifyParams) async throws -> AuthMFAVerifyResponse {
let response: AuthMFAVerifyResponse = try await api.authorizedExecute(
HTTPRequest(
Expand Down
1 change: 1 addition & 0 deletions Sources/Auth/Internal/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct Dependencies: Sendable {
var date: @Sendable () -> Date = { Date() }

var urlOpener: URLOpener = .live
var pkce: PKCE = .live

var encoder: JSONEncoder { configuration.encoder }
var decoder: JSONDecoder { configuration.decoder }
Expand Down
34 changes: 20 additions & 14 deletions Sources/Auth/Internal/PKCE.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import Crypto
import Foundation

enum PKCE {
static func generateCodeVerifier() -> String {
let buffer = [UInt8].random(count: 64)
return Data(buffer).pkceBase64EncodedString()
}
struct PKCE {
var generateCodeVerifier: @Sendable () -> String
var generateCodeChallenge: @Sendable (_ codeVerifier: String) -> String
}

static func generateCodeChallenge(from string: String) -> String {
guard let data = string.data(using: .utf8) else {
preconditionFailure("provided string should be utf8 encoded.")
}
extension PKCE {
static let live = PKCE(
generateCodeVerifier: {
let buffer = [UInt8].random(count: 64)
return Data(buffer).pkceBase64EncodedString()
},
generateCodeChallenge: { codeVerifier in
guard let data = codeVerifier.data(using: .utf8) else {
preconditionFailure("provided string should be utf8 encoded.")
}

var hasher = SHA256()
hasher.update(data: data)
let hashed = hasher.finalize()
return Data(hashed).pkceBase64EncodedString()
}
var hasher = SHA256()
hasher.update(data: data)
let hashed = hasher.finalize()
return Data(hashed).pkceBase64EncodedString()
}
)
}

extension Data {
Expand Down
3 changes: 2 additions & 1 deletion Tests/AuthTests/AuthClientMultipleInstancesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
// Created by Guilherme Souza on 05/07/24.
//

@testable import Auth
import TestHelpers
import XCTest

@testable import Auth

final class AuthClientMultipleInstancesTests: XCTestCase {
func testMultipleAuthClientInstances() {
let url = URL(string: "http://localhost:54321/auth")!
Expand Down
Loading
Loading