diff --git a/OctoKit/Configuration.swift b/OctoKit/Configuration.swift index 730f380..6b8af5c 100644 --- a/OctoKit/Configuration.swift +++ b/OctoKit/Configuration.swift @@ -103,6 +103,29 @@ public struct OAuthConfiguration: Configuration { } } + #if compiler(>=5.5.2) && canImport(_Concurrency) + @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) + public func authorize(code: String) async throws -> TokenConfiguration? { + let request = OAuthRouter.accessToken(self, code).URLRequest + if let request = request { + let (data, response) = try await session.data(for: request, delegate: nil) + if let response = response as? HTTPURLResponse { + if response.statusCode != 200 { + return nil + } else { + if let string = String(data: data, encoding: .utf8) { + let accessToken = accessTokenFromResponse(string) + if let accessToken = accessToken { + return TokenConfiguration(accessToken, url: apiEndpoint) + } + } + } + } + } + return nil + } + #endif + public func handleOpenURL(url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) { if let code = url.URLParameters["code"] { authorize(code: code) { config in @@ -111,6 +134,16 @@ public struct OAuthConfiguration: Configuration { } } + #if compiler(>=5.5.2) && canImport(_Concurrency) + @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) + public func handleOpenURL(url: URL) async throws -> TokenConfiguration? { + if let code = url.URLParameters["code"] { + return try await authorize(code: code) + } + return nil + } + #endif + public func accessTokenFromResponse(_ response: String) -> String? { let accessTokenParam = response.components(separatedBy: "&").first if let accessTokenParam = accessTokenParam {