forked from mozilla-mobile/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenServerClient.swift
141 lines (115 loc) · 5.1 KB
/
TokenServerClient.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Alamofire
public let PROD_TOKEN_SERVER_ENDPOINT = "https://token.services.mozilla.com/1.0/sync/1.5";
public let STAGE_TOKEN_SERVER_ENDPOINT = "https://token.stage.mozaws.net/1.0/sync/1.5";
public let TokenServerClientErrorDomain = "org.mozilla.token.error"
public class TokenServerToken {
public let id : String;
public let key : String;
public let api_endpoint : String;
public let uid : UInt32;
public init(id: String, key: String, api_endpoint: String, uid: UInt32) {
self.id = id
self.key = key
self.api_endpoint = api_endpoint
self.uid = uid
}
}
public class TokenServerClient {
public class var requestManager : Alamofire.Manager {
struct Static {
static let manager : Alamofire.Manager = Alamofire.Manager(configuration: Alamofire.Manager.sharedInstance.session.configuration)
}
Static.manager.startRequestsImmediately = false
return Static.manager
}
public let url : String
public init(endpoint: String? = nil) {
self.url = endpoint ?? PROD_TOKEN_SERVER_ENDPOINT
}
public class func getAudienceForEndpoint(endpoint: String) -> String {
let url = NSURL(string: endpoint)!
if let port = url.port {
return "\(url.scheme!)://\(url.host!):\(port)"
} else {
return "\(url.scheme!)://\(url.host!)"
}
}
private class func validateJSON(json : JSON) -> Bool {
return json["id"].isString &&
json["key"].isString &&
json["api_endpoint"].isString &&
json["uid"].isInt
}
private class func tokenFromJSON(json: JSON) -> TokenServerToken {
let id = json["id"].asString!
let key = json["key"].asString!
let api_endpoint = json["api_endpoint"].asString!
let uid = UInt32(json["uid"].asInt!)
return TokenServerToken(id: id, key: key, api_endpoint: api_endpoint, uid: uid)
}
public class Request {
public let queue: dispatch_queue_t
private let request : Alamofire.Request
private var successBlock: (TokenServerToken -> Void)?;
public init(queue: dispatch_queue_t, request: Alamofire.Request) {
self.queue = queue ?? dispatch_get_main_queue()
self.request = request
}
public func onSuccess(block: TokenServerToken -> Void) -> Self {
self.successBlock = block;
return self
}
public func go(errorBlock: NSError -> Void) {
if successBlock == nil {
return dispatch_async(queue, {
errorBlock(NSError(domain: TokenServerClientErrorDomain, code: -1, userInfo: ["message": "no success handler"]))
})
}
request.responseJSON { (request, response, json, error) in
if let error = error {
return dispatch_async(self.queue, {
errorBlock(error)
})
}
if response == nil || json == nil {
return dispatch_async(self.queue, {
errorBlock(NSError(domain: TokenServerClientErrorDomain, code: -1, userInfo: ["message": "malformed JSON response"]))
})
}
let json = JSON(json!)
let statusCode : Int = response!.statusCode
if statusCode != 200 {
return dispatch_async(self.queue, {
errorBlock(NSError(domain: TokenServerClientErrorDomain, code: -1, userInfo: ["message": "bad response code", "code": statusCode,
"body": json.toString(pretty: true)]))
})
}
if !TokenServerClient.validateJSON(json) {
return dispatch_async(self.queue, {
errorBlock(NSError(domain: TokenServerClientErrorDomain, code: -1, userInfo: ["message": "invalid token server response"]))
})
}
let token = TokenServerClient.tokenFromJSON(json)
return dispatch_async(self.queue, {
self.successBlock!(token)
})
}
}
}
public func tokenRequest(queue: dispatch_queue_t? = nil, assertion: String, clientState: String? = nil) -> Request {
let URL = NSURL(string: url)!
var mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.setValue("BrowserID " + assertion, forHTTPHeaderField: "Authorization")
if let clientState = clientState {
mutableURLRequest.setValue(clientState, forHTTPHeaderField: "X-Client-State")
}
let manager = TokenServerClient.requestManager
let request = manager.request(mutableURLRequest)
request.resume()
return Request(queue: queue ?? dispatch_get_main_queue(), request: request)
}
}