Skip to content

Commit

Permalink
Update Configuration.swift handleOpenURL and authorize to support…
Browse files Browse the repository at this point in the history
… async await (#192)

* Update Configuration.swift

Added handleOpenURL and Authorize support async await

* Update Configuration.swift

lint
  • Loading branch information
LucasCoderT authored Aug 26, 2024
1 parent 4419245 commit 590b60a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions OctoKit/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 590b60a

Please sign in to comment.