From 77605d403b30b297efb9d28a3969492728e6f822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Fri, 5 Feb 2016 11:01:21 +0100 Subject: [PATCH 1/3] Remove HTTP token and add support for custom authorization header --- Source/Networking.swift | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/Source/Networking.swift b/Source/Networking.swift index fe90ad5..8a51899 100644 --- a/Source/Networking.swift +++ b/Source/Networking.swift @@ -50,16 +50,6 @@ public enum NetworkingConfigurationType { case Default, Ephemeral, Background } - -/** - Represents the token type to be used. - - `HTTP:` ? - - `Bearer:` https://tools.ietf.org/html/rfc6750 - */ -enum NetworkingTokenType { - case HTTP, Bearer -} - struct FakeRequest { let response: AnyObject? let statusCode: Int @@ -84,7 +74,7 @@ public class Networking { private let baseURL: String var fakeRequests = [RequestType : [String : FakeRequest]]() var token: String? - var tokenType: NetworkingTokenType? + var customAuthorizationHeader: String? var imageCache = NSCache() var configurationType: NetworkingConfigurationType @@ -127,20 +117,18 @@ public class Networking { /** Authenticates using a Bearer token, sets the Authorization header to "Bearer \(token)" - - parameter bearerToken: The token to be used + - parameter token: The token to be used */ - public func authenticate(bearerToken bearerToken: String) { - self.tokenType = .Bearer - self.token = bearerToken + public func authenticate(token token: String) { + self.token = token } /** - Authenticates using a HTTP token, sets the Authorization header to "Token token=\(token)" - - parameter HTTPToken: The token to be used + Authenticates using a custom HTTP Authorization header + - parameter authorizationHeader: The authorization header to be used */ - public func authenticate(HTTPToken HTTPToken: String) { - self.tokenType = .HTTP - self.token = HTTPToken + public func authenticate(authorizationHeader authorizationHeader: String) { + self.customAuthorizationHeader = authorizationHeader } /** @@ -210,15 +198,10 @@ extension Networking { request.addValue(contentType.rawValue, forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept") - if let token = self.token, type = self.tokenType { - switch type { - case .HTTP: - request.setValue("Token token=\(token)", forHTTPHeaderField: "Authorization") - break - case .Bearer: - request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") - break - } + if let authorizationHeader = self.customAuthorizationHeader { + request.setValue(authorizationHeader, forHTTPHeaderField: "Authorization") + } else if let token = self.token { + request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") } NetworkActivityIndicator.sharedIndicator.visible = true From 1c29fde4b9396d6a3ee5f01345e1b9f4f4ffb3bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Fri, 5 Feb 2016 11:06:52 +0100 Subject: [PATCH 2/3] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f7276e0..17f4522 100755 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ - Optimized for unit testing - Minimal implementation - Fake requests easily (mocking/stubbing) -- Runs synchronously in automatic testing enviroments +- Runs synchronously in automatic testing environments - Image download and caching - Free From bf6647ce395a49af909417938ae5f7214648b823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvis=20Nu=C3=B1ez?= Date: Fri, 5 Feb 2016 11:13:57 +0100 Subject: [PATCH 3/3] Update README with new feature --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 17f4522..bdb7bb0 100755 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ * [Authentication](#authentication) * [HTTP basic](#http-basic) * [Bearer token](#bearer-token) - * [HTTP token](#http-token) + * [Custom authentication header](#custom-authentication-header) * [Making a request](#making-a-request) * [Content Types](#content-types) * [Faking a request](#faking-a-request) @@ -74,21 +74,21 @@ Luckily, **Networking** provides a simpler way to do this: ```swift let networking = Networking(baseURL: "http://sample.org") -networking.authenticate(bearerToken: "AAAFFAAAA3DAAAAAA") +networking.authenticate(token: "AAAFFAAAA3DAAAAAA") networking.GET("/users", completion: { JSON, error in // Do something... }) ``` -### HTTP token +### Custom authentication header -To authenticate using a [HTTP token](http://tools.ietf.org/html/rfc1945) **"AAAFFAAAA3DAAAAAA"**, you would need to set the following header field: `Authorization: Token token=AAAFFAAAA3DAAAAAA`. +To authenticate using a custom authentication header, for example **"Token token=AAAFFAAAA3DAAAAAA"** you would need to set the following header field: `Authorization: Token token=AAAFFAAAA3DAAAAAA`. Luckily, **Networking** also provides a simpler way to do this: ```swift let networking = Networking(baseURL: "http://sample.org") -networking.authenticate(HTTPToken: "AAAFFAAAA3DAAAAAA") +networking.authenticate(authorizationHeader: "Token token=AAAFFAAAA3DAAAAAA") networking.GET("/users", completion: { JSON, error in // Do something... })