Skip to content

Commit

Permalink
Merge pull request #61 from 3lvis/feature/custom-authorization-header
Browse files Browse the repository at this point in the history
Feature: custom authorization header
  • Loading branch information
3lvis committed Feb 5, 2016
2 parents a71ec1d + bf6647c commit f531d4c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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...
})
Expand Down
41 changes: 12 additions & 29 deletions Source/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f531d4c

Please sign in to comment.