Skip to content

Commit 351f14b

Browse files
authored
Swift DocC (#111)
1 parent 6388f2d commit 351f14b

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

.spi.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
version: 1
2-
external_links:
3-
documentation: "https://codefiesta.github.io/OAuthKit/documentation/oauthkit"
2+
builder:
3+
configs:
4+
- documentation_targets: [OAuthKit]

Sources/OAuthKit/OAuthKit.docc/Extensions/OAuth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
- ``authorize(provider:grantType:)``
1313

14-
### Observing an OAuth object
14+
### OAuth State Tracking
1515

1616
- ``state``
1717
- ``providers``

Sources/OAuthKit/OAuthKit.docc/GettingStarted.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Create an ``OAuth`` instance and start an authorization flow.
44

55
@Metadata {
66
@PageKind(article)
7+
@Available(iOS, introduced: "18.0")
8+
@Available(macOS, introduced: "15.0")
9+
@Available(tvOS, introduced: "18.0")
10+
@Available(visionOS, introduced: "2.0")
11+
@Available(watchOS, introduced: "11.0")
712
}
813

914
## Overview
@@ -120,3 +125,62 @@ struct ContentView: View {
120125
}
121126
```
122127

128+
## Presenting Authorization Windows
129+
When a ``OAuth/state`` has reached an ``OAuth/State/authorizing(_:_:)`` state, your application should present
130+
a browser window that allows the user to authenticate with an ``OAuth/Provider``.
131+
OAuthKit provides an out of the box SwiftUI view ``OAWebView`` that will automatically handle the rest of the steps in the OAuth authorization flow for you.
132+
133+
```swift
134+
/// Handle `.authorizing` or `.authorized` oauth state changes by opening or closing authorization windows.
135+
/// - Parameter state: the published state change
136+
func handle(state: OAuth.State) {
137+
switch state {
138+
case .empty, .requestingAccessToken, .requestingDeviceCode, .receivedDeviceCode:
139+
break
140+
case .authorizing:
141+
openWindow(id: "oauth")
142+
case .authorized:
143+
dismissWindow(id: "oauth")
144+
}
145+
}
146+
```
147+
Once the user has successfully authorized with the ``OAuth/Provider``, the ``OAuth/state`` will move to an ``OAuth/State/authorized(_:_:)`` state. You can then automaticaly close the ``OAWebView`` window in your SwiftUI application.
148+
149+
> Tip: Once authorized, a ``OAuth/Authorization/token`` will be inserted into the user's Keychain that can be used in subsequent API requests by inserting the `Authorization` header into an ``Foundation/URLRequest`` via ``OAuthKit/Foundation/URLRequest/addAuthorization(auth:)`` .
150+
151+
## Presenting Device Codes (tvOS and watchOS)
152+
OAuthKit also supports the [OAuth 2.0 Device Code Flow Grant](https://alexbilbie.github.io/2016/04/oauth-2-device-flow-grant/), which is used by apps that don't have access to a web browser (like tvOS or watchOS). To leverage OAuthKit in tvOS or watchOS apps, simply add the `deviceCodeURL` to your ``OAuth/Provider`` and start an ``OAuth/authorize(provider:grantType:)`` flow with the ``OAuth/GrantType/deviceCode`` grantType.
153+
154+
```swift
155+
let grantType: OAuth.GrantType = .deviceCode
156+
oauth.authorize(provider: provider, grantType: grantType)
157+
158+
struct ContentView: View {
159+
...
160+
var body: some View {
161+
VStack {
162+
switch oauth.state {
163+
case .empty, .authorizing, .requestingAccessToken, .requestingDeviceCode:
164+
EmptyView()
165+
case .authorized:
166+
Text("Authorized [\(provider.id)]")
167+
case .receivedDeviceCode(_, let deviceCode):
168+
Text("To login, visit")
169+
Text(.init("[\(deviceCode.verificationUri)](\(deviceCode.verificationUri))"))
170+
.foregroundStyle(.blue)
171+
Text("and enter the following code:")
172+
Text(deviceCode.userCode)
173+
.padding()
174+
.border(Color.primary)
175+
.font(.title)
176+
}
177+
}
178+
}
179+
}
180+
```
181+
182+
The observable ``OAuth`` instance will proceed to poll the ``OAuth/Provider`` access token endpoint until the device code has expired or has successfully received an ``OAuth/Token`` and moved to an ``OAuth/State/authorized(_:_:)`` state.
183+
184+
> Tip: Click [here](https://oauth.net/2/grant-types/device-code/) to see details of how the OAuth 2.0 Device Code Grant Type works.
185+
186+

Sources/OAuthKit/OAuthKit.docc/OAuthKit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ A modern and observable framework for implementing OAuth 2.0 authorization flows
44

55
@Metadata {
66
@PageColor(purple)
7+
@Available(iOS, introduced: "18.0")
8+
@Available(macOS, introduced: "15.0")
9+
@Available(tvOS, introduced: "18.0")
10+
@Available(visionOS, introduced: "2.0")
11+
@Available(watchOS, introduced: "11.0")
712
}
813

914
## Overview

Sources/OAuthKit/OAuthKit.docc/SampleCode.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ Integrate OAuthKit into a multiplatform app with support for iOS, macOS, tvOS, v
88
url: "https://github.com/codefiesta/OAuthSample"
99
)
1010
@PageKind(sampleCode)
11+
@Available(iOS, introduced: "18.0")
12+
@Available(macOS, introduced: "15.0")
13+
@Available(tvOS, introduced: "18.0")
14+
@Available(visionOS, introduced: "2.0")
15+
@Available(watchOS, introduced: "11.0")
1116
}
1217

1318
## Overview
1419

15-
This sample creates _OAuthSample_, an app for demonstrating how to get up and running quickly with OAuthKit.
20+
The OAuthSample app demonstrates how to get up and running quickly with OAuthKit.

0 commit comments

Comments
 (0)