Skip to content

Commit

Permalink
Update Configuration.swift
Browse files Browse the repository at this point in the history
Added handleOpenURL and Authorize support async await
  • Loading branch information
LucasCoderT committed Aug 25, 2024
1 parent 4419245 commit 15f4e27
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions OctoKit/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ public struct OAuthConfiguration: Configuration {
task.resume()
}
}

#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 = self.accessTokenFromResponse(string)
if let accessToken = accessToken {
return TokenConfiguration(accessToken, url: self.apiEndpoint)
}
}
}
}
}
return nil
}
#endif


public func handleOpenURL(url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) {
if let code = url.URLParameters["code"] {
Expand All @@ -110,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
Expand Down

0 comments on commit 15f4e27

Please sign in to comment.