Skip to content
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
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ let package = Package(
.iOS(.v18),
.macOS(.v15),
.tvOS(.v18),
.visionOS(.v2)
.visionOS(.v2),
.watchOS(.v11)
],
products: [
.library(
Expand Down
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -16,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
Expand All @@ -36,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
Expand All @@ -70,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()
Expand All @@ -87,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
Expand All @@ -100,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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OAuthKit/Views/OAWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Kevin McKee on 5/16/24.
//

#if !os(tvOS)
#if canImport(WebKit)
import SwiftUI
import WebKit

Expand Down
2 changes: 1 addition & 1 deletion Sources/OAuthKit/Views/OAWebViewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Kevin McKee on 5/16/24.
//

#if !os(tvOS)
#if canImport(WebKit)
import Combine
import SwiftUI
import WebKit
Expand Down
2 changes: 1 addition & 1 deletion Tests/OAuthKitTests/OAWebViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Kevin McKee
//

#if !os(tvOS)
#if canImport(WebKit)
import Foundation
@testable import OAuthKit
import SwiftUI
Expand Down
2 changes: 1 addition & 1 deletion Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Kevin McKee
//

#if !os(tvOS)
#if canImport(WebKit)
import WebKit

/// OAuth Test WKNavigationAction that can be used for testing
Expand Down