Skip to content

Commit

Permalink
Format code with swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugo Arangino committed Feb 22, 2017
1 parent 57ef652 commit 6c8a5b3
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 37 deletions.
43 changes: 22 additions & 21 deletions Heimdallr/Core/Heimdallr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public let HeimdallrErrorNotAuthorized = 2
accessTokenStore.storeAccessToken(newValue)
}
}

private let accessTokenParser: OAuthAccessTokenParser
private let httpClient: HeimdallrHTTPClient

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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<Void, NSError>) -> ()) {
open func requestAccessToken(username: String, password: String, completion: @escaping (Result<Void, NSError>) -> Void) {
requestAccessToken(grant: .resourceOwnerPasswordCredentials(username, password)) { result in
completion(result.map { _ in return })
}
Expand All @@ -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<Void, NSError>) -> ()) {
open func requestAccessToken(grantType: String, parameters: [String: String], completion: @escaping (Result<Void, NSError>) -> Void) {
requestAccessToken(grant: .extension(grantType, parameters)) { result in
completion(result.map { _ in return })
}
Expand All @@ -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<OAuthAccessToken, NSError>) -> ()) {
private func requestAccessToken(grant: OAuthAuthorizationGrant, completion: @escaping (Result<OAuthAccessToken, NSError>) -> Void) {
var request = URLRequest(url: tokenURL)

var parameters = grant.parameters
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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<URLRequest, NSError>) -> ()) {
open func authenticateRequest(_ request: URLRequest, completion: @escaping (Result<URLRequest, NSError>) -> Void) {
requestQueue.async {
self.blockRequestQueue()
self.authenticateRequestConcurrently(request, completion: completion)
}
}

private func authenticateRequestConcurrently(_ request: URLRequest, completion: @escaping (Result<URLRequest, NSError>) -> ()) {
private func authenticateRequestConcurrently(_ request: URLRequest, completion: @escaping (Result<URLRequest, NSError>) -> 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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Heimdallr/Core/HeimdallrHTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion Heimdallr/Core/HeimdallrHTTPClientURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
1 change: 0 additions & 1 deletion Heimdallr/Core/HeimdallrResourceRequestAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ public protocol HeimdallResourceRequestAuthenticator {
///
/// - returns: The authenticated request.
func authenticateResourceRequest(_ request: URLRequest, accessToken: OAuthAccessToken) -> URLRequest

}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -21,5 +21,4 @@ public class HeimdallResourceRequestAuthenticatorHTTPAuthorizationHeader: NSObje
mutableRequest.setHTTPAuthorization(.accessTokenAuthentication(accessToken))
return mutableRequest
}

}
8 changes: 4 additions & 4 deletions Heimdallr/Core/OAuthAccessToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Heimdallr/Core/OAuthAccessTokenKeychainStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
}

Expand Down
4 changes: 2 additions & 2 deletions Heimdallr/Core/OAuthAuthorizationGrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Heimdallr/Core/OAuthClientCredentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Heimdallr/Core/OAuthError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Heimdallr/Core/URLRequestExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6c8a5b3

Please sign in to comment.