diff --git a/Heimdallr/Core/Heimdallr.swift b/Heimdallr/Core/Heimdallr.swift index 364b7b3..d3f49c8 100644 --- a/Heimdallr/Core/Heimdallr.swift +++ b/Heimdallr/Core/Heimdallr.swift @@ -24,6 +24,7 @@ public let HeimdallrErrorNotAuthorized = 2 accessTokenStore.storeAccessToken(newValue) } } + private let accessTokenParser: OAuthAccessTokenParser private let httpClient: HeimdallrHTTPClient @@ -54,8 +55,8 @@ public let HeimdallrErrorNotAuthorized = 2 /// Default: `OAuthAccessTokenDefaultParser`. /// - parameter httpClient: The HTTP client that should be used for requesting /// access tokens. Default: `HeimdallrHTTPClientURLSession`. - /// - parameter resourceRequestAuthenticator: The request authenticator that is - /// used to authenticate requests. Default: + /// - parameter resourceRequestAuthenticator: The request authenticator that is + /// used to authenticate requests. Default: /// `HeimdallResourceRequestAuthenticatorHTTPAuthorizationHeader`. /// /// - returns: A new client initialized with the given token endpoint URL, @@ -71,8 +72,8 @@ public let HeimdallrErrorNotAuthorized = 2 /// Invalidates the currently stored access token, if any. /// - /// Unlike `clearAccessToken` this will only invalidate the access token so - /// that Heimdallr will try to refresh the token using the refresh token + /// Unlike `clearAccessToken` this will only invalidate the access token so + /// that Heimdallr will try to refresh the token using the refresh token /// automatically. /// /// **Note:** Sets the access token's expiration date to @@ -83,7 +84,7 @@ public let HeimdallrErrorNotAuthorized = 2 /// Clears the currently stored access token, if any. /// - /// After calling this method the user needs to reauthenticate using + /// After calling this method the user needs to reauthenticate using /// `requestAccessToken`. open func clearAccessToken() { accessTokenStore.storeAccessToken(nil) @@ -96,7 +97,7 @@ public let HeimdallrErrorNotAuthorized = 2 /// - parameter username: The resource owner's username. /// - parameter password: The resource owner's password. /// - parameter completion: A callback to invoke when the request completed. - open func requestAccessToken(username: String, password: String, completion: @escaping (Result) -> ()) { + open func requestAccessToken(username: String, password: String, completion: @escaping (Result) -> Void) { requestAccessToken(grant: .resourceOwnerPasswordCredentials(username, password)) { result in completion(result.map { _ in return }) } @@ -109,7 +110,7 @@ public let HeimdallrErrorNotAuthorized = 2 /// - parameter grantType: The grant type URI of the extension grant /// - parameter parameters: The required parameters for the external grant /// - parameter completion: A callback to invoke when the request completed. - open func requestAccessToken(grantType: String, parameters: [String: String], completion: @escaping (Result) -> ()) { + open func requestAccessToken(grantType: String, parameters: [String: String], completion: @escaping (Result) -> Void) { requestAccessToken(grant: .extension(grantType, parameters)) { result in completion(result.map { _ in return }) } @@ -123,7 +124,7 @@ public let HeimdallrErrorNotAuthorized = 2 /// /// - parameter grant: The authorization grant (e.g., refresh). /// - parameter completion: A callback to invoke when the request completed. - private func requestAccessToken(grant: OAuthAuthorizationGrant, completion: @escaping (Result) -> ()) { + private func requestAccessToken(grant: OAuthAuthorizationGrant, completion: @escaping (Result) -> Void) { var request = URLRequest(url: tokenURL) var parameters = grant.parameters @@ -149,7 +150,7 @@ public let HeimdallrErrorNotAuthorized = 2 } else { let userInfo = [ NSLocalizedDescriptionKey: NSLocalizedString("Could not authorize grant", comment: ""), - NSLocalizedFailureReasonErrorKey: String(format: NSLocalizedString("Expected access token, got: %@.", comment: ""), NSString(data: data!, encoding: String.Encoding.utf8.rawValue) ?? "nil") + NSLocalizedFailureReasonErrorKey: String(format: NSLocalizedString("Expected access token, got: %@.", comment: ""), NSString(data: data!, encoding: String.Encoding.utf8.rawValue) ?? "nil"), ] let error = NSError(domain: HeimdallrErrorDomain, code: HeimdallrErrorInvalidData, userInfo: userInfo) @@ -161,7 +162,7 @@ public let HeimdallrErrorNotAuthorized = 2 } else { let userInfo = [ NSLocalizedDescriptionKey: NSLocalizedString("Could not authorize grant", comment: ""), - NSLocalizedFailureReasonErrorKey: String(format: NSLocalizedString("Expected error, got: %@.", comment: ""), NSString(data: data!, encoding: String.Encoding.utf8.rawValue) ?? "nil") + NSLocalizedFailureReasonErrorKey: String(format: NSLocalizedString("Expected error, got: %@.", comment: ""), NSString(data: data!, encoding: String.Encoding.utf8.rawValue) ?? "nil"), ] let error = NSError(domain: HeimdallrErrorDomain, code: HeimdallrErrorInvalidData, userInfo: userInfo) @@ -176,7 +177,7 @@ public let HeimdallrErrorNotAuthorized = 2 /// - parameter request: An unauthenticated URLRequest. /// - parameter accessToken: The access token to be used for authentication. /// - /// - returns: The given request authorized using the resource request + /// - returns: The given request authorized using the resource request /// authenticator. private func authenticateRequest(_ request: URLRequest, accessToken: OAuthAccessToken) -> URLRequest { return self.resourceRequestAuthenticator.authenticateResourceRequest(request, accessToken: accessToken) @@ -197,33 +198,33 @@ public let HeimdallrErrorNotAuthorized = 2 /// /// - parameter request: An unauthenticated URLRequest. /// - parameter completion: A callback to invoke with the authenticated request. - open func authenticateRequest(_ request: URLRequest, completion: @escaping (Result) -> ()) { + open func authenticateRequest(_ request: URLRequest, completion: @escaping (Result) -> Void) { requestQueue.async { self.blockRequestQueue() self.authenticateRequestConcurrently(request, completion: completion) } } - private func authenticateRequestConcurrently(_ request: URLRequest, completion: @escaping (Result) -> ()) { + private func authenticateRequestConcurrently(_ request: URLRequest, completion: @escaping (Result) -> Void) { if let accessToken = accessToken { - if let expiration = accessToken.expiresAt, expiration < Date() { + if let expiration = accessToken.expiresAt, expiration < Date() { if let refreshToken = accessToken.refreshToken { requestAccessToken(grant: .refreshToken(refreshToken)) { result in completion(result.analysis(ifSuccess: { accessToken in let authenticatedRequest = self.authenticateRequest(request, accessToken: accessToken) return .success(authenticatedRequest) - }, ifFailure: { error in - if [ HeimdallrErrorDomain, OAuthErrorDomain ].contains(error.domain) { - self.clearAccessToken() - } - return .failure(error) + }, ifFailure: { error in + if [HeimdallrErrorDomain, OAuthErrorDomain].contains(error.domain) { + self.clearAccessToken() + } + return .failure(error) })) self.releaseRequestQueue() } } else { let userInfo = [ NSLocalizedDescriptionKey: NSLocalizedString("Could not add authorization to request", comment: ""), - NSLocalizedFailureReasonErrorKey: NSLocalizedString("Access token expired, no refresh token available.", comment: "") + NSLocalizedFailureReasonErrorKey: NSLocalizedString("Access token expired, no refresh token available.", comment: ""), ] let error = NSError(domain: HeimdallrErrorDomain, code: HeimdallrErrorNotAuthorized, userInfo: userInfo) @@ -238,7 +239,7 @@ public let HeimdallrErrorNotAuthorized = 2 } else { let userInfo = [ NSLocalizedDescriptionKey: NSLocalizedString("Could not add authorization to request", comment: ""), - NSLocalizedFailureReasonErrorKey: NSLocalizedString("Not authorized.", comment: "") + NSLocalizedFailureReasonErrorKey: NSLocalizedString("Not authorized.", comment: ""), ] let error = NSError(domain: HeimdallrErrorDomain, code: HeimdallrErrorNotAuthorized, userInfo: userInfo) diff --git a/Heimdallr/Core/HeimdallrHTTPClient.swift b/Heimdallr/Core/HeimdallrHTTPClient.swift index 77cc060..4fc8827 100644 --- a/Heimdallr/Core/HeimdallrHTTPClient.swift +++ b/Heimdallr/Core/HeimdallrHTTPClient.swift @@ -4,8 +4,8 @@ import Foundation @objc public protocol HeimdallrHTTPClient { /// Sends the given request. - /// + /// /// - parameter request: The request to be sent. /// - parameter completion: A callback to invoke when the request completed. - func sendRequest(_ request: URLRequest, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> ()) + func sendRequest(_ request: URLRequest, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) } diff --git a/Heimdallr/Core/HeimdallrHTTPClientURLSession.swift b/Heimdallr/Core/HeimdallrHTTPClientURLSession.swift index 78ea83a..115cfe0 100644 --- a/Heimdallr/Core/HeimdallrHTTPClientURLSession.swift +++ b/Heimdallr/Core/HeimdallrHTTPClientURLSession.swift @@ -20,7 +20,7 @@ public class HeimdallrHTTPClientURLSession: NSObject, HeimdallrHTTPClient { /// /// - parameter request: The request to be sent. /// - parameter completion: A callback to invoke when the request completed. - public func sendRequest(_ request: URLRequest, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> ()) { + public func sendRequest(_ request: URLRequest, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) { let task = urlSession.dataTask(with: request, completionHandler: completion) task.resume() } diff --git a/Heimdallr/Core/HeimdallrResourceRequestAuthenticator.swift b/Heimdallr/Core/HeimdallrResourceRequestAuthenticator.swift index fe37618..f07005b 100644 --- a/Heimdallr/Core/HeimdallrResourceRequestAuthenticator.swift +++ b/Heimdallr/Core/HeimdallrResourceRequestAuthenticator.swift @@ -12,5 +12,4 @@ public protocol HeimdallResourceRequestAuthenticator { /// /// - returns: The authenticated request. func authenticateResourceRequest(_ request: URLRequest, accessToken: OAuthAccessToken) -> URLRequest - } diff --git a/Heimdallr/Core/HeimdallrResourceRequestAuthenticatorHTTPAuthorizationHeader.swift b/Heimdallr/Core/HeimdallrResourceRequestAuthenticatorHTTPAuthorizationHeader.swift index 28e379a..32c9fe8 100644 --- a/Heimdallr/Core/HeimdallrResourceRequestAuthenticatorHTTPAuthorizationHeader.swift +++ b/Heimdallr/Core/HeimdallrResourceRequestAuthenticatorHTTPAuthorizationHeader.swift @@ -1,6 +1,6 @@ import Foundation -/// A `HeimdallResourceRequestAuthenticator` which uses the HTTP `Authorization` +/// A `HeimdallResourceRequestAuthenticator` which uses the HTTP `Authorization` /// Header to authorize a request. @objc public class HeimdallResourceRequestAuthenticatorHTTPAuthorizationHeader: NSObject, HeimdallResourceRequestAuthenticator { @@ -21,5 +21,4 @@ public class HeimdallResourceRequestAuthenticatorHTTPAuthorizationHeader: NSObje mutableRequest.setHTTPAuthorization(.accessTokenAuthentication(accessToken)) return mutableRequest } - } diff --git a/Heimdallr/Core/OAuthAccessToken.swift b/Heimdallr/Core/OAuthAccessToken.swift index c2e4d2b..9e7cf90 100644 --- a/Heimdallr/Core/OAuthAccessToken.swift +++ b/Heimdallr/Core/OAuthAccessToken.swift @@ -42,9 +42,9 @@ public class OAuthAccessToken: NSObject { /// properties where new ones are not provided. public func copy(accessToken: String? = nil, tokenType: String? = nil, expiresAt: Date?? = nil, refreshToken: String?? = nil) -> OAuthAccessToken { return OAuthAccessToken(accessToken: accessToken ?? self.accessToken, - tokenType: tokenType ?? self.tokenType, - expiresAt: expiresAt ?? self.expiresAt, - refreshToken: refreshToken ?? self.refreshToken) + tokenType: tokenType ?? self.tokenType, + expiresAt: expiresAt ?? self.expiresAt, + refreshToken: refreshToken ?? self.refreshToken) } } @@ -78,7 +78,7 @@ extension OAuthAccessToken { public class func decode(data: Data) -> OAuthAccessToken? { guard let json = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions(rawValue: 0)), let jsonDictionary = json as? [String: AnyObject] else { - return nil + return nil } return decode(jsonDictionary) diff --git a/Heimdallr/Core/OAuthAccessTokenKeychainStore.swift b/Heimdallr/Core/OAuthAccessTokenKeychainStore.swift index ce7f795..a419130 100644 --- a/Heimdallr/Core/OAuthAccessTokenKeychainStore.swift +++ b/Heimdallr/Core/OAuthAccessTokenKeychainStore.swift @@ -7,7 +7,7 @@ internal struct Keychain { return [ String(kSecClass): String(kSecClassGenericPassword) as AnyObject, String(kSecAttrAccessible): String(kSecAttrAccessibleAfterFirstUnlock) as AnyObject, - String(kSecAttrService): service as AnyObject + String(kSecAttrService): service as AnyObject, ] } diff --git a/Heimdallr/Core/OAuthAuthorizationGrant.swift b/Heimdallr/Core/OAuthAuthorizationGrant.swift index e38e46a..81377a6 100644 --- a/Heimdallr/Core/OAuthAuthorizationGrant.swift +++ b/Heimdallr/Core/OAuthAuthorizationGrant.swift @@ -33,12 +33,12 @@ public enum OAuthAuthorizationGrant { return [ "grant_type": "password", "username": username, - "password": password + "password": password, ] case .refreshToken(let refreshToken): return [ "grant_type": "refresh_token", - "refresh_token": refreshToken + "refresh_token": refreshToken, ] case .extension(let grantType, var parameters): parameters["grant_type"] = grantType diff --git a/Heimdallr/Core/OAuthClientCredentials.swift b/Heimdallr/Core/OAuthClientCredentials.swift index 4c94e4c..a693b4e 100644 --- a/Heimdallr/Core/OAuthClientCredentials.swift +++ b/Heimdallr/Core/OAuthClientCredentials.swift @@ -14,7 +14,7 @@ public class OAuthClientCredentials: NSObject { /// Includes the client identifier as `client_id` and the client secret, /// if set, as `client_secret`. public var parameters: [String: String] { - var parameters = [ "client_id": id ] + var parameters = ["client_id": id] if let secret = secret { parameters["client_secret"] = secret diff --git a/Heimdallr/Core/OAuthError.swift b/Heimdallr/Core/OAuthError.swift index 6cb36c6..2521254 100644 --- a/Heimdallr/Core/OAuthError.swift +++ b/Heimdallr/Core/OAuthError.swift @@ -90,7 +90,7 @@ extension OAuthError { public class func decode(data: Data) -> OAuthError? { guard let json: AnyObject? = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions(rawValue: 0)) as AnyObject?, let jsonDictionary = json as? [String: AnyObject] else { - return nil + return nil } return decode(jsonDictionary) diff --git a/Heimdallr/Core/URLRequestExtensions.swift b/Heimdallr/Core/URLRequestExtensions.swift index 2ce4dc2..a1d5023 100644 --- a/Heimdallr/Core/URLRequestExtensions.swift +++ b/Heimdallr/Core/URLRequestExtensions.swift @@ -80,7 +80,7 @@ public extension URLRequest { for (key, value) in parameters { components += queryComponents(key, value) } - let bodyString = components.map { "\($0)=\($1)" }.joined(separator: "&" ) + let bodyString = components.map { "\($0)=\($1)" }.joined(separator: "&") httpBody = bodyString.data(using: String.Encoding.utf8) } else { httpBody = nil