From 5d40c4460785c3474142714bbf44a794258cd54e Mon Sep 17 00:00:00 2001 From: Kevin McKee Date: Sun, 22 Jun 2025 10:06:48 -0700 Subject: [PATCH 1/4] WIP --- Package.swift | 3 ++- Sources/OAuthKit/Views/OAWebView.swift | 2 +- Sources/OAuthKit/Views/OAWebViewCoordinator.swift | 2 +- Tests/OAuthKitTests/OAWebViewTests.swift | 2 +- Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index 50d8491..8f3ef2d 100644 --- a/Package.swift +++ b/Package.swift @@ -9,7 +9,8 @@ let package = Package( .iOS(.v18), .macOS(.v15), .tvOS(.v18), - .visionOS(.v2) + .visionOS(.v2), + .watchOS(.v11) ], products: [ .library( diff --git a/Sources/OAuthKit/Views/OAWebView.swift b/Sources/OAuthKit/Views/OAWebView.swift index c85c18f..49f5666 100644 --- a/Sources/OAuthKit/Views/OAWebView.swift +++ b/Sources/OAuthKit/Views/OAWebView.swift @@ -5,7 +5,7 @@ // Created by Kevin McKee on 5/16/24. // -#if !os(tvOS) +#if !os(tvOS) && !os(watchOS) import SwiftUI import WebKit diff --git a/Sources/OAuthKit/Views/OAWebViewCoordinator.swift b/Sources/OAuthKit/Views/OAWebViewCoordinator.swift index 6c6f4d7..2eab791 100644 --- a/Sources/OAuthKit/Views/OAWebViewCoordinator.swift +++ b/Sources/OAuthKit/Views/OAWebViewCoordinator.swift @@ -5,7 +5,7 @@ // Created by Kevin McKee on 5/16/24. // -#if !os(tvOS) +#if !os(tvOS) && !os(watchOS) import Combine import SwiftUI import WebKit diff --git a/Tests/OAuthKitTests/OAWebViewTests.swift b/Tests/OAuthKitTests/OAWebViewTests.swift index 8bad741..41e8d86 100644 --- a/Tests/OAuthKitTests/OAWebViewTests.swift +++ b/Tests/OAuthKitTests/OAWebViewTests.swift @@ -5,7 +5,7 @@ // Created by Kevin McKee // -#if !os(tvOS) +#if !os(tvOS) && !os(watchOS) import Foundation @testable import OAuthKit import SwiftUI diff --git a/Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift b/Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift index 0a80013..2558dc8 100644 --- a/Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift +++ b/Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift @@ -5,7 +5,7 @@ // Created by Kevin McKee // -#if !os(tvOS) +#if !os(tvOS) && !os(watchOS) import WebKit /// OAuth Test WKNavigationAction that can be used for testing From 6f7266acf511320344ef47b6dd771d269349dc40 Mon Sep 17 00:00:00 2001 From: Kevin McKee Date: Sun, 22 Jun 2025 10:50:07 -0700 Subject: [PATCH 2/4] WIP --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 88bc27e..5cb6756 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ ![iOS 18.0+](https://img.shields.io/badge/iOS-18.0%2B-crimson.svg) ![macOS 15.0+](https://img.shields.io/badge/macOS-15.0%2B-skyblue.svg) ![tvOS 18.0+](https://img.shields.io/badge/tvOS-18.0%2B-blue.svg) -![visionOS 2.0+](https://img.shields.io/badge/visionOS-2.0%2B-magenta.svg) +![visionOS 2.0+](https://img.shields.io/badge/visionOS-2.0%2B-violet.svg) +![watchOS 11.0+](https://img.shields.io/badge/watchOS-11.0%2B-magenta.svg) [![License: MIT](https://img.shields.io/badge/License-MIT-indigo.svg)](https://opensource.org/licenses/MIT) ![Code Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/codefiesta/87655b6e3c89b9198287b2fefbfa641f/raw/oauthkit-coverage.json) From 44741ae26f21c498bbc3ff2111024b9b35381a88 Mon Sep 17 00:00:00 2001 From: Kevin McKee Date: Sun, 22 Jun 2025 11:50:18 -0700 Subject: [PATCH 3/4] Simplifying macro to simply test if the target destination can import WebKit. --- Sources/OAuthKit/Views/OAWebView.swift | 2 +- Sources/OAuthKit/Views/OAWebViewCoordinator.swift | 2 +- Tests/OAuthKitTests/OAWebViewTests.swift | 2 +- Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/OAuthKit/Views/OAWebView.swift b/Sources/OAuthKit/Views/OAWebView.swift index 49f5666..57bfcd4 100644 --- a/Sources/OAuthKit/Views/OAWebView.swift +++ b/Sources/OAuthKit/Views/OAWebView.swift @@ -5,7 +5,7 @@ // Created by Kevin McKee on 5/16/24. // -#if !os(tvOS) && !os(watchOS) +#if canImport(WebKit) import SwiftUI import WebKit diff --git a/Sources/OAuthKit/Views/OAWebViewCoordinator.swift b/Sources/OAuthKit/Views/OAWebViewCoordinator.swift index 2eab791..6e94e94 100644 --- a/Sources/OAuthKit/Views/OAWebViewCoordinator.swift +++ b/Sources/OAuthKit/Views/OAWebViewCoordinator.swift @@ -5,7 +5,7 @@ // Created by Kevin McKee on 5/16/24. // -#if !os(tvOS) && !os(watchOS) +#if canImport(WebKit) import Combine import SwiftUI import WebKit diff --git a/Tests/OAuthKitTests/OAWebViewTests.swift b/Tests/OAuthKitTests/OAWebViewTests.swift index 41e8d86..7d6181a 100644 --- a/Tests/OAuthKitTests/OAWebViewTests.swift +++ b/Tests/OAuthKitTests/OAWebViewTests.swift @@ -5,7 +5,7 @@ // Created by Kevin McKee // -#if !os(tvOS) && !os(watchOS) +#if canImport(WebKit) import Foundation @testable import OAuthKit import SwiftUI diff --git a/Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift b/Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift index 2558dc8..ee1a8fd 100644 --- a/Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift +++ b/Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift @@ -5,7 +5,7 @@ // Created by Kevin McKee // -#if !os(tvOS) && !os(watchOS) +#if canImport(WebKit) import WebKit /// OAuth Test WKNavigationAction that can be used for testing From c42bcf59cb08cc6492cd1c43020ca083dc76dd8f Mon Sep 17 00:00:00 2001 From: Kevin McKee Date: Sun, 22 Jun 2025 12:28:19 -0700 Subject: [PATCH 4/4] README updates for multiple platforms. --- README.md | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5cb6756..681df76 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ OAuthKit is a contemporary, event-driven Swift Package that utilizes the [Observ ## OAuthKit Usage -The following is an example of the simplest usage of using OAuthKit in macOS: +The following is an example of the simplest usage of using OAuthKit across multiple platforms (iOS, macOS, visionOS, tvOS, watchOS): ```swift import OAuthKit @@ -37,19 +37,23 @@ struct OAuthApp: App { } .environment(\.oauth, oauth) + #if canImport(WebKit) WindowGroup(id: "oauth") { OAWebView(oauth: oauth) } + #endif } } struct ContentView: View { + #if canImport(WebKit) @Environment(\.openWindow) var openWindow @Environment(\.dismissWindow) private var dismissWindow + #endif @Environment(\.oauth) var oauth: OAuth @@ -71,7 +75,8 @@ struct ContentView: View { } case .receivedDeviceCode(_, let deviceCode): Text("To login, visit") - Text(deviceCode.verificationUri).foregroundStyle(.blue) + Text(.init("[\(deviceCode.verificationUri)](\(deviceCode.verificationUri))")) + .foregroundStyle(.blue) Text("and enter the following code:") Text(deviceCode.userCode) .padding() @@ -88,11 +93,24 @@ struct ContentView: View { var providerList: some View { List(oauth.providers) { provider in Button(provider.id) { - // Start the default PKCE flow (.pkce) - oauth.authorize(provider: provider) + authorize(provider: provider) } } } + + /// Starts the authorization process for the specified provider. + /// - Parameter provider: the provider to begin authorization for + private func authorize(provider: OAuth.Provider) { + #if canImport(WebKit) + // Use the PKCE grantType for iOS, macOS, visionOS + let grantType: OAuth.GrantType = .pkce(.init()) + #else + // Use the Device Code grantType for tvOS, watchOS + let grantType: OAuth.GrantType = .deviceCode + #endif + // Start the authorization flow + oauth.authorize(provider: provider, grantType: grantType) + } /// Reacts to oauth state changes by opening or closing authorization windows. /// - Parameter state: the published state change @@ -101,9 +119,13 @@ struct ContentView: View { case .empty, .requestingAccessToken, .requestingDeviceCode: break case .authorizing, .receivedDeviceCode: + #if canImport(WebKit) openWindow(id: "oauth") + #endif case .authorized(_, _): + #if canImport(WebKit) dismissWindow(id: "oauth") + #endif } } }