/
TrueVaultClient.swift
277 lines (220 loc) · 10.9 KB
/
TrueVaultClient.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import Foundation
import Alamofire
class User: ResponseObjectSerializable {
let accessToken: String?
let accountId: String;
let id: String;
let username: String;
required convenience init?(response: HTTPURLResponse, representation: [String: Any]) {
guard
let user = representation["user"] as? [String: Any]
else { return nil }
self.init(dict: user)
}
required init?(dict: [String: Any]) {
guard
let id = dict["id"] as? String,
let username = dict["username"] as? String,
let accountId = dict["account_id"] as? String
else { return nil }
self.id = id
self.username = username
self.accountId = accountId
self.accessToken = dict["access_token"] as? String
}
}
class FullUser: User {
let attributes: [String: Any]
required init?(response: HTTPURLResponse, representation: [String: Any]) {
guard
let user = representation["user"] as? [String: Any],
let attributes = user["attributes"] as? String
else { return nil }
self.attributes = try! decodeTrueVaultDocument(base64JSON: attributes)
super.init(dict: user)
}
required init?(dict: [String: Any]) {
if let attributes = dict["attributes"] as? String {
self.attributes = try! decodeTrueVaultDocument(base64JSON: attributes)
} else {
self.attributes = [:]
}
super.init(dict: dict)
}
}
struct Document: ResponseObjectSerializable {
let documentId: String
init?(response: HTTPURLResponse, representation: [String: Any]) {
guard
let documentId = representation["document_id"] as? String
else { return nil }
self.documentId = documentId
}
}
struct FullDocument {
let documentId: String
let document: [String: Any]
init(documentId: String, document: [String: Any]) {
self.documentId = documentId
self.document = document
}
}
func decodeTrueVaultDocument(base64JSON: String) throws -> [String:Any] {
let json = NSData(base64Encoded: base64JSON, options: [])!
return try JSONSerialization.jsonObject(with: json as Data, options: .allowFragments) as! [String:Any]
}
struct DocumentList: ResponseObjectSerializable {
let page: Int
let perPage: Int
let total: Int
let documents: [FullDocument]
init?(response: HTTPURLResponse, representation: [String: Any]) {
guard
let data = representation["data"] as? [String: Any],
let page = data["page"] as? Int,
let perPage = data["per_page"] as? Int,
let total = data["total"] as? Int,
let items = data["items"] as? [[String: Any]]
else { return nil }
self.page = page
self.perPage = perPage
self.total = total
self.documents = items.map { item in
let documentBase64 = item["document"] as! String
let document = try! decodeTrueVaultDocument(base64JSON: documentBase64)
return FullDocument(documentId: item["id"] as! String, document: document)
}
}
}
struct FullUserList: ResponseObjectSerializable {
let users: [FullUser]
init?(response: HTTPURLResponse, representation: [String: Any]) {
guard
let users = representation["users"] as? [[String: Any]]
else { return nil }
self.users = users.map { item in
return FullUser(dict: item)!
}
}
}
struct DocumentMultiget: ResponseObjectSerializable {
let documents: [FullDocument]
init?(response: HTTPURLResponse, representation: [String: Any]) {
guard
let items = representation["documents"] as? [[String: Any]]
else { return nil }
self.documents = items.map { item in
let documentBase64 = item["document"] as! String
let document = try! decodeTrueVaultDocument(base64JSON: documentBase64)
return FullDocument(documentId: item["id"] as! String, document: document)
}
}
init(documents: [FullDocument]) {
self.documents = documents
}
}
enum TrueVaultRequestError: Error {
case network(error: Error)
case dataSerialization(error: Error)
case TrueVaultError(code: Int?, message: String, type: String)
case jsonSerialization(jsonObject: Any)
}
protocol ResponseObjectSerializable {
init?(response: HTTPURLResponse, representation: [String: Any])
}
extension DataRequest {
func responseObject<T: ResponseObjectSerializable>(queue: DispatchQueue? = nil, completionHandler: @escaping (DataResponse<T>) -> Void) {
let responseSerializer = DataResponseSerializer<T> { request, response, data, error in
guard error == nil else { return .failure(TrueVaultRequestError.network(error: error!)) }
let jsonSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
let result = jsonSerializer.serializeResponse(request, response, data, nil)
guard case let .success(jsonObj) = result, let jsonObject = jsonObj as? [String: Any] else {
return .failure(TrueVaultRequestError.dataSerialization(error: result.error!))
}
// TODO: Handle non-200 responses that aren't TV error responses
if jsonObject["result"] as? String == "error" {
let error = jsonObject["error"] as? [String: Any]
let code = error!["code"] as? Int
let message = error!["message"] as! String
let type = error!["type"] as! String
return .failure(TrueVaultRequestError.TrueVaultError(code: code, message: message, type: type))
}
guard let response = response, let responseObject = T(response: response, representation: jsonObject) else {
return .failure(TrueVaultRequestError.jsonSerialization(jsonObject: jsonObject))
}
return .success(responseObject)
}
response(queue: queue, responseSerializer: responseSerializer, completionHandler: completionHandler)
}
}
public class TrueVaultClient {
let accessToken: String
init(accessToken: String) {
self.accessToken = accessToken
}
class func login(accountId: String, username: String, password: String, notValidAfter: Date, completionHandler: @escaping ((DataResponse<User>) -> Void)) {
let formattedDate = ISO8601DateFormatter.string(from: notValidAfter, timeZone: TimeZone.init(identifier: "UTC")!, formatOptions: [.withInternetDateTime, .withFractionalSeconds])
Alamofire.request("https://api.truevault.com/v1/auth/login", method: .post, parameters: [
"account_id": accountId,
"username": username,
"password": password,
"not_valid_after": formattedDate
]).responseObject { (response: DataResponse<User>) in
DispatchQueue.main.async {
completionHandler(response)
}
}
}
private func trueVaultRequest(path: String, method: HTTPMethod, parameters: [String: Any]) -> DataRequest {
let authHeader = Request.authorizationHeader(user: self.accessToken, password: "")!
let headers: HTTPHeaders = [authHeader.key: authHeader.value]
return Alamofire.request("https://api.truevault.com/\(path)", method: method, parameters: parameters, headers: headers)
}
private func trueVaultRequestJSON<T: ResponseObjectSerializable>(path: String, method: HTTPMethod, parameters: [String: Any], completionHandler: @escaping ((DataResponse<T>) -> Void)) {
trueVaultRequest(path: path, method: method, parameters: parameters).responseObject { (response: DataResponse<T>) in
DispatchQueue.main.async {
completionHandler(response)
}
}
}
func createDocument(vaultId: String, document: [String: Any], completionHandler: @escaping ((DataResponse<Document>) -> Void)) {
let documentJSON = try! JSONSerialization.data(withJSONObject: document, options: .init(rawValue: 0))
let documentBase64 = documentJSON.base64EncodedString()
trueVaultRequestJSON(path: "v1/vaults/\(vaultId)/documents", method: .post, parameters: ["document": documentBase64], completionHandler: completionHandler)
}
func listDocuments(vaultId: String, completionHandler: @escaping ((DataResponse<DocumentList>) -> Void)) {
trueVaultRequestJSON(path: "v1/vaults/\(vaultId)/documents", method: .get, parameters: ["full": true, "per_page": 500], completionHandler: completionHandler)
}
func readUser(userId: String, completionHandler: @escaping ((DataResponse<FullUser>) -> Void)) {
trueVaultRequestJSON(path: "v1/users/\(userId)", method: .get, parameters: ["full": true], completionHandler: completionHandler)
}
func listUsers(completionHandler: @escaping ((DataResponse<FullUserList>) -> Void)) {
trueVaultRequestJSON(path: "v1/users", method: .get, parameters: ["full": true], completionHandler: completionHandler)
}
func getDocuments(vaultId: String, docIds: [String], completionHandler: @escaping ((DocumentMultiget?) -> Void)) {
if docIds.count == 0 {
completionHandler(DocumentMultiget(documents: []))
} else if docIds.count == 1 {
let docId = docIds.first!
self.trueVaultRequest(path: "v1/vaults/\(vaultId)/documents/\(docId)", method: HTTPMethod.get, parameters: [:]).responseString { response in
let decodedDocument = try! decodeTrueVaultDocument(base64JSON: response.value!)
let fullDoc = FullDocument(documentId: docId, document: decodedDocument)
completionHandler(DocumentMultiget(documents: [fullDoc]))
}
} else {
let csvDocIds = docIds.joined(separator: ",")
self.trueVaultRequestJSON(path: "v1/vaults/\(vaultId)/documents/\(csvDocIds)", method: .get, parameters: [:]) { response in
completionHandler(response.result.value)
}
}
}
func createUser(accountId: String, username: String, password: String, attributes: [String: Any], groupIds: [String], completionHandler: @escaping ((DataResponse<FullUser>) -> Void)) {
let attributesJSON = try! JSONSerialization.data(withJSONObject: attributes, options: .init(rawValue: 0))
let attributesBase64 = attributesJSON.base64EncodedString()
self.trueVaultRequestJSON(path: "v1/users?full=true", method: .post, parameters: [
"username": username,
"password": password,
"attributes": attributesBase64,
"group_ids": groupIds.joined(separator: ",")], completionHandler: completionHandler)
}
}