Skip to content

Commit

Permalink
fix: ASWebAuthenticationPresentationContextProviding requires @mainactor
Browse files Browse the repository at this point in the history
 (#108)

* chore: update dependencies

* chore: fix issue with MainActor presentation context

* chore: make requests numbered

* chore: update tests

---------

Co-authored-by: danthorpe <[email protected]>
  • Loading branch information
danthorpe and danthorpe authored Dec 8, 2024
1 parent 5642665 commit 08f020f
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-composable-architecture",
"state" : {
"revision" : "8013f1a72af8ccb2b1735d7aed831a8dc07c6fd0",
"version" : "1.15.0"
"revision" : "69247baf7be2fd6f5820192caef0082d01849cd0",
"version" : "1.16.1"
}
},
{
Expand All @@ -86,8 +86,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-concurrency-extras",
"state" : {
"revision" : "bb5059bde9022d69ac516803f4f227d8ac967f71",
"version" : "1.1.0"
"revision" : "163409ef7dae9d960b87f34b51587b6609a76c1f",
"version" : "1.3.0"
}
},
{
Expand Down Expand Up @@ -149,8 +149,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-navigation",
"state" : {
"revision" : "e834b3760731160d7d448509ee6a1408c8582a6b",
"version" : "2.2.0"
"revision" : "16a27ab7ae0abfefbbcba73581b3e2380b47a579",
"version" : "2.2.2"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Examples/Spotify/App/SignedOut/SignedOutFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ struct SignedOutFeature {
state = .failed
return .none
case .view(.signInButtonTapped):
return .run { send in
try await spotify.signIn(nil)
return .run { @MainActor send in
try await spotify.signIn(presentationContext: DefaultPresentationContext())
await send(.signInResponse(.success(true)))
} catch: { error, send in
await send(.signInResponse(.failure(error)))
Expand Down
1 change: 1 addition & 0 deletions Examples/Spotify/App/SpotifyClient/SpotifyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extension Spotify {
.logged(using: Logger(subsystem: "works.dan.networking.examples.spotify", category: "Spotify API"))
.server(prefixPath: "v1")
.server(authority: "api.spotify.com")
.numbered()
.authenticated(
oauth: .spotify(
clientId: "b4937bc99da547b4b90559f5024d8467",
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-concurrency-extras",
"state" : {
"revision" : "6054df64b55186f08b6d0fd87152081b8ad8d613",
"version" : "1.2.0"
"revision" : "163409ef7dae9d960b87f34b51587b6609a76c1f",
"version" : "1.3.0"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Sources/OAuth/ASWebAuthenticationSession+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ extension ASWebAuthenticationSession {
}
}

final class DefaultPresentationContext: NSObject, ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
@MainActor public final class DefaultPresentationContext: NSObject, ASWebAuthenticationPresentationContextProviding {
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
#if os(macOS)
ASPresentationAnchor()
#else
Expand Down
6 changes: 5 additions & 1 deletion Sources/OAuth/OAuthDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension OAuth {
let upstream: any NetworkingComponent
var system: any OAuthSystem<Credentials>

var presentationContext: (any ASWebAuthenticationPresentationContextProviding) = DefaultPresentationContext()
var presentationContext: (any ASWebAuthenticationPresentationContextProviding)?

@Dependency(\.webAuthenticationSession) var webAuthenticationSession

Expand Down Expand Up @@ -46,6 +46,10 @@ extension OAuth.Delegate: AuthenticationDelegate {
codeChallenge: codeChallenge
)

guard let presentationContext else {
throw OAuth.Error.presentationContextNotSet
}

let callbackURL = try await webAuthenticationSession.start(
state: state,
authorizationURL: url,
Expand Down
1 change: 1 addition & 0 deletions Sources/OAuth/OAuthError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Foundation
extension OAuth {
public enum Error: Swift.Error, Equatable {
case oauthNotInstalled
case presentationContextNotSet
case invalidAuthorizationEndpoint(String)
case invalidTokenEndpoint(String)

Expand Down
5 changes: 5 additions & 0 deletions Tests/OAuthTests/OAuthDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ final class OAuthDelegateTests: OAuthTestCase {
.server(authenticationMethod: .stub)
.authenticated(oauth: stub)

// Configure Network
try await network.stubOAuthSystem {
await $0.set(presentationContext: DefaultPresentationContext())
}

// Make a request to protected resource
var request = HTTPRequestData(path: "/protected-resource")
request.authenticationMethod = .stub
Expand Down
2 changes: 1 addition & 1 deletion Tests/OAuthTests/StubOAuthSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct StubOAuthSystem: StandardOAuthSystem {

extension NetworkingComponent {
func stubOAuthSystem<ReturnValue>(
perform: (any OAuthProxy<StubOAuthSystem.Credentials>) async throws -> ReturnValue
perform: @MainActor (any OAuthProxy<StubOAuthSystem.Credentials>) async throws -> ReturnValue
) async throws -> ReturnValue {
try await oauth(of: StubOAuthSystem.Credentials.self, perform: perform)
}
Expand Down

0 comments on commit 08f020f

Please sign in to comment.