You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/OAuthKit/OAuthKit.docc/GettingStarted.md
+64Lines changed: 64 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,11 @@ Create an ``OAuth`` instance and start an authorization flow.
4
4
5
5
@Metadata {
6
6
@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")
7
12
}
8
13
9
14
## Overview
@@ -120,3 +125,62 @@ struct ContentView: View {
120
125
}
121
126
```
122
127
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
+
funchandle(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.
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.
0 commit comments