Skip to content
Closed
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
36 changes: 18 additions & 18 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ jobs:
strategy:
fail-fast: false
matrix:
swift: ['swift6', 'swift5']
swift: ['swift6.2']
include:
- swift: 'swift6'
xcode-path: '/Applications/Xcode_16.2.app'
macos: 'macos-14'
- swift: 'swift5'
xcode-path: '/Applications/Xcode_15.4.app'
macos: 'macos-14'
- swift: 'swift6.2'
xcode-path: '/Applications/Xcode_26.2.app'
macos: 'macos-26'

env:
DEVELOPER_DIR: ${{ matrix.xcode-path }}
Expand All @@ -30,7 +27,7 @@ jobs:

steps:
- name: checkout
uses: actions/checkout@v4
uses: actions/checkout@v7.0.1
with:
submodules: true

Expand All @@ -47,7 +44,7 @@ jobs:
swift test --parallel --xunit-output 'reports/TestReport.xml'

- name: report unit test results
uses: dorny/test-reporter@eaa763f6ffc21c7a37837f56cd5f9737f27fc6c8
uses: dorny/test-reporter@a43b3a5f7366b97d083190328d2c652e1a8b6aa2
if: always()
id: results-swift-tests
with:
Expand Down Expand Up @@ -78,25 +75,28 @@ jobs:
strategy:
fail-fast: false
matrix:
macos: [ 'macos-14' ]
macos: [ 'macos-26' ]
platform: [ 'macOS', 'iOS', 'tvOS' ]
include:
- platform: 'macOS'
scheme: 'OAuth2macOS'
destination: 'platform=macOS'
- platform: 'iOS'
scheme: 'OAuth2iOS'
destination: 'generic/platform=iOS Simulator'
- platform: 'tvOS'
scheme: 'OAuth2tvOS'
destination: 'generic/platform=tvOS Simulator'

env:
DEVELOPER_DIR: '/Applications/Xcode_15.4.0.app'
DEVELOPER_DIR: '/Applications/Xcode_26.2.app'
TEST_RESULTS_PATH: 'OAuth2Tests_${{ matrix.platform }}_run${{ github.run_number }}.xcresult'

runs-on: ${{ matrix.macos }}

steps:
- name: checkout
uses: actions/checkout@v4
uses: actions/checkout@v7.0.1
with:
submodules: true

Expand All @@ -111,25 +111,25 @@ jobs:
xcodebuild
-scheme '${{ matrix.scheme }}'
-configuration Debug
-destination 'platform=${{ matrix.platform }}'
-destination '${{ matrix.destination }}'
-showBuildTimingSummary
build

- name: test
if: success()
if: success() && matrix.platform == 'macOS'
continue-on-error: true
run: >
xcodebuild
-scheme '${{ matrix.scheme }}'
-configuration Debug
-destination 'platform=${{ matrix.platform }}'
-destination '${{ matrix.destination }}'
-resultBundlePath ${{ env.TEST_RESULTS_PATH }}
test

- name: run xcresulttool
uses: slidoapp/xcresulttool@v2.0.0
if: always()
uses: slidoapp/xcresulttool@v3.1.2
if: always() && matrix.platform == 'macOS'
with:
title: 'results-xcode-tests-${{ matrix.platform }}'
path: ${{ env.TEST_RESULTS_PATH }}
upload-bundles: true
upload-bundles: always
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.5.3
6.2.3
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:6.2
//
// Package.swift
// OAuth2
Expand Down Expand Up @@ -49,5 +49,6 @@ let package = Package(
.testTarget(name: "BaseTests", dependencies: [.target(name: "TestUtils"), .target(name: "Base"), .target(name: "Flows")]),
.testTarget(name: "FlowTests", dependencies: [.target(name: "TestUtils"), .target(name: "Flows")]),
// .testTarget(name: "DataLoaderTests", dependencies: [.target(name: "DataLoader")]),
]
],
swiftLanguageModes: [.v5]
)
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ OAuth2 frameworks for **macOS**, **iOS** and **tvOS** written in Swift 5.
- [🖥 Sample macOS app][sample] (with data loader examples)
- [📖 Technical Documentation](https://p2.github.io/OAuth2)

OAuth2 requires Xcode 12.4, the built framework can be used on **OS X 10.15**, **iOS 13**, **tvOS 13**, **watchOS 6** and later.
OAuth2 requires Xcode 26 or a Swift 6.2 toolchain. The built framework can be used on **OS X 10.15**, **iOS 13**, **tvOS 13**, **watchOS 6** and later.
Happy to accept pull requests, please see [CONTRIBUTING.md](./Docs/CONTRIBUTING.md)

### Swift Version

Swift Package Manager builds require Swift 6.2 or later and use Swift 5 language mode.

Since the Swift language is constantly evolving I have adopted a versioning scheme mirroring Swift versions:
the framework version's **first two digits are always the Swift version** the library is compatible with, see [releases](https://github.com/p2/OAuth2/releases).
Code compatible with brand new Swift versions are to be found on a separate feature branch named appropriately.
Expand Down
35 changes: 18 additions & 17 deletions Sources/DataLoader/OAuth2DataLoaderSessionTaskDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,25 @@ final class OAuth2DataLoaderSessionTaskDelegate: NSObject, URLSessionTaskDelegat

// MARK: - URLSessionTaskDelegate

func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
guard request.url?.host == host else {
loader?.logger?.warning("Redirected to «\(request.url?.host ?? "nil")» but only approving HTTP redirection on «\(host)», not following redirect: \(request)")
completionHandler(nil)
return
}
do {
guard let loader = loader else {
throw OAuth2Error.generic("no loader instance, cannot re-sign")
nonisolated func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping @Sendable (URLRequest?) -> Void) {
Task { @OAuth2Actor in
guard request.url?.host == host else {
loader?.logger?.warning("Redirected to «\(request.url?.host ?? "nil")» but only approving HTTP redirection on «\(host)», not following redirect: \(request)")
completionHandler(nil)
return
}
do {
guard let loader = loader else {
throw OAuth2Error.generic("no loader instance, cannot re-sign")
}
let newRequest = try request.signed(with: loader.oauth2)
loader.logger?.debug("Following HTTP redirection to «\(request.url?.description ?? "nil")»")
completionHandler(newRequest)
}
catch {
loader?.logger?.warning("Failed to re-sign request after HTTP redirection: \(error)")
completionHandler(request)
}
let newRequest = try request.signed(with: loader.oauth2)
loader.logger?.debug("Following HTTP redirection to «\(request.url?.description ?? "nil")»")
completionHandler(newRequest)
}
catch {
loader?.logger?.warning("Failed to re-sign request after HTTP redirection: \(error)")
completionHandler(request)
}
}
}

19 changes: 10 additions & 9 deletions Sources/iOS/OAuth2Authorizer+iOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {

authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: redirectURL.scheme, completionHandler: completionHandler)
if #available(iOS 13.0, macCatalyst 13.1, *) {
webAuthenticationPresentationContextProvider = OAuth2ASWebAuthenticationPresentationContextProvider(authorizer: self)
webAuthenticationPresentationContextProvider = OAuth2ASWebAuthenticationPresentationContextProvider(
authorizeContext: oauth2.authConfig.authorizeContext
)
if let session = authenticationSession as? ASWebAuthenticationSession {
session.presentationContextProvider = webAuthenticationPresentationContextProvider as! OAuth2ASWebAuthenticationPresentationContextProvider
session.prefersEphemeralWebBrowserSession = prefersEphemeralWebBrowserSession
Expand Down Expand Up @@ -283,26 +285,25 @@ class OAuth2SFViewControllerDelegate: NSObject, SFSafariViewControllerDelegate,
#endif

@available(iOS 13.0, *)
@OAuth2Actor
@MainActor
class OAuth2ASWebAuthenticationPresentationContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {

private let authorizer: OAuth2Authorizer
nonisolated(unsafe) private weak var authorizeContext: AnyObject?

init(authorizer: OAuth2Authorizer) {
self.authorizer = authorizer
nonisolated init(authorizeContext: AnyObject?) {
self.authorizeContext = authorizeContext
}

@OAuth2Actor
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
if let context = authorizer.oauth2.authConfig.authorizeContext as? ASPresentationAnchor {
if let context = authorizeContext as? ASPresentationAnchor {
return context
}

if let context = authorizer.oauth2.authConfig.authorizeContext as? UIViewController {
if let context = authorizeContext as? UIViewController {
return context.view.window!
}

fatalError("Invalid authConfig.authorizeContext, must be an ASPresentationAnchor or UIViewController but is \(type(of: authorizer.oauth2.authConfig.authorizeContext))")
fatalError("Invalid authConfig.authorizeContext, must be an ASPresentationAnchor or UIViewController but is \(type(of: authorizeContext))")
}
}

Expand Down
16 changes: 9 additions & 7 deletions Sources/macOS/OAuth2Authorizer+macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
authenticationSession = ASWebAuthenticationSession(url: url,
callbackURLScheme: redirectURL.scheme,
completionHandler: completionHandler)
webAuthenticationPresentationContextProvider = await OAuth2ASWebAuthenticationPresentationContextProvider(authorizer: self)
webAuthenticationPresentationContextProvider = OAuth2ASWebAuthenticationPresentationContextProvider(
authorizeContext: oauth2.authConfig.authorizeContext
)
if let session = authenticationSession as? ASWebAuthenticationSession {
session.presentationContextProvider = webAuthenticationPresentationContextProvider as! OAuth2ASWebAuthenticationPresentationContextProvider
session.prefersEphemeralWebBrowserSession = prefersEphemeralWebBrowserSession
Expand Down Expand Up @@ -269,20 +271,20 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {

#if canImport(AuthenticationServices)
@available(macOS 10.15, *)
@MainActor
class OAuth2ASWebAuthenticationPresentationContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {

private let authorizer: OAuth2Authorizer
nonisolated(unsafe) private weak var authorizeContext: AnyObject?

init(authorizer: OAuth2Authorizer) {
self.authorizer = authorizer
nonisolated init(authorizeContext: AnyObject?) {
self.authorizeContext = authorizeContext
}

@OAuth2Actor /// For Xcode 15, we need to specify the `@OAuth2Actor` explicitly, but in Xcode 16 this is no longer necessary. 🤷‍♂️
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
if let context = authorizer.oauth2.authConfig.authorizeContext as? ASPresentationAnchor {
if let context = authorizeContext as? ASPresentationAnchor {
return context
}
fatalError("Invalid authConfig.authorizeContext, must be an ASPresentationAnchor but is \(type(of: authorizer.oauth2.authConfig.authorizeContext))")
fatalError("Invalid authConfig.authorizeContext, must be an ASPresentationAnchor but is \(type(of: authorizeContext))")
}
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions Tests/FlowTests/OAuth2DeviceGrantTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class OAuth2DeviceGrantTests: XCTestCase {
"expires_in": 3600,
"refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
"foo": "bar & hat"
] as [String: Any]
] as OAuth2JSON

// must throw when "token_type" is missing
do {
Expand Down Expand Up @@ -146,7 +146,7 @@ class OAuth2DeviceGrantTests: XCTestCase {
"expires_in": 3600,
"refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
"foo": "bar & hat"
] as [String : Any]
] as OAuth2JSON

do {
_ = try oauth.parseAccessTokenResponse(params: response2)
Expand Down
3 changes: 1 addition & 2 deletions Tests/FlowTests/OAuth2PasswordGrantTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class OAuth2PasswordGrantTests: XCTestCase {
"expires_in": 3600,
"refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
"foo": "bar"
] as [String: Any]
] as OAuth2JSON
do {
let dict = try oauth.parseAccessTokenResponse(params: response)
XCTAssertEqual("bar", dict["foo"] as? String)
Expand Down Expand Up @@ -114,4 +114,3 @@ class OAuth2PasswordGrantTests: XCTestCase {
XCTAssertTrue(body!.contains("foo=bar+%26+hat"), "Must create correct request body")
}
}