Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import Account
7
import Shared
8
import SwiftyJSON
9
10
open class KeysPayload: CleartextPayloadJSON {
11
override open func isValid() -> Bool {
12
return super.isValid() &&
13
self["default"].isArray()
14
}
15
16
fileprivate func keyBundleFromPair(_ input: JSON) -> KeyBundle? {
17
if let pair: [JSON] = input.array {
18
if let encKey = pair[0].string {
19
if let hmacKey = pair[1].string {
20
return KeyBundle(encKeyB64: encKey, hmacKeyB64: hmacKey)
21
}
22
}
23
}
24
return nil
25
}
26
27
var defaultKeys: KeyBundle? {
28
return self.keyBundleFromPair(self["default"])
29
}
30
31
var collectionKeys: [String: KeyBundle] {
32
if let collections: [String: JSON] = self["collections"].dictionary {
33
return optFilter(mapValues(collections, f: self.keyBundleFromPair))
34
}
35
return [:]
36
}
37
38
override open func equalPayloads(_ obj: CleartextPayloadJSON) -> Bool {
39
if !(obj is KeysPayload) {
40
return false
41
}
42
43
if !super.equalPayloads(obj) {
44
return false
45
}
46
47
let p = obj as! KeysPayload
48
if p.defaultKeys != self.defaultKeys {
49
return false
50
}
51
52
// TODO: check collections.
53
54
return true
55
}
56
}