Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ApiKit-OAuth, important fixes for ApiKit and concurrent calls.
- Loading branch information
1 parent
dd6b835
commit 169bb2d
Showing
22 changed files
with
552 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// OAuthProvider.swift | ||
// | ||
// | ||
// Created by Nicholas Mata on 6/14/22. | ||
// | ||
|
||
import Foundation | ||
|
||
/// An OAuth provider that manages access token. | ||
public protocol OAuthProvider { | ||
|
||
/// Get the current state of the access token. | ||
var tokenState: TokenState { get } | ||
|
||
/// Refresh the token provided | ||
/// - Parameter completion: A function to call when the token is refreshed or fails to refresh. | ||
func refreshToken(completion: @escaping (Result<String, Error>)-> Void) | ||
|
||
/// Attach the token to the request. The default implementation will add token to "Authorization" header as Bearer token. | ||
/// - Parameters: | ||
/// - token: The token to add to the request | ||
/// - request: The request to add the token too | ||
/// - Returns: The new request with the token attached to it | ||
func attach(token: String, to request: URLRequest) -> URLRequest | ||
} | ||
|
||
public extension OAuthProvider where Self: AnyObject { | ||
func attach(token: String, to request: URLRequest) -> URLRequest { | ||
let bearerToken = "Bearer \(token)" | ||
var request = request | ||
if request.allHTTPHeaderFields != nil { | ||
request.allHTTPHeaderFields?.updateValue(bearerToken, forKey: "Authorization") | ||
} else { | ||
request.allHTTPHeaderFields = ["Authorization": bearerToken] | ||
} | ||
return request | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// OAuthTokenManagedProvider.swift | ||
// | ||
// | ||
// Created by Nicholas Mata on 4/15/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol OAuthTokenManagedProvider: OAuthProvider { | ||
/// The token manager that will be used to store and retrieve the access and refresh token | ||
var tokenManager: TokenManager { get set } | ||
|
||
/// Refresh the token provided | ||
/// - Parameter completion: A function to call when the token is refreshed or fails to refresh. | ||
func refreshToken(completion: @escaping (Result<String, Error>) -> Void) | ||
|
||
/// Attach the token to the request. The default implementation will add token to "Authorization" header as Bearer token. | ||
/// - Parameters: | ||
/// - token: The token to add to the request | ||
/// - request: The request to add the token too | ||
/// - Returns: The new request with the token attached to it | ||
func attach(token: String, to request: URLRequest) -> URLRequest | ||
} | ||
|
||
public extension OAuthTokenManagedProvider where Self: AnyObject { | ||
var tokenState: TokenState { | ||
guard let accessToken = tokenManager.accessToken, !accessToken.isExpired() else { | ||
guard let refreshToken = tokenManager.refreshToken, !refreshToken.isExpired() else { | ||
return .missing | ||
} | ||
return .expired | ||
} | ||
return .valid(token: accessToken.token) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// OIDCResponse.swift | ||
// | ||
// | ||
// Created by Nicholas Mata on 4/15/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public class OIDCTokenResponse: Codable { | ||
public var accessToken: String | ||
public var tokenType: String | ||
public var expiresIn: Int | ||
public var refreshToken: String? | ||
public var refreshTokenExpiresIn: Int? | ||
public var idToken: String? | ||
} |
Oops, something went wrong.