-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
OWS2FAManager.swift
156 lines (137 loc) · 5.25 KB
/
OWS2FAManager.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
//
// Copyright (c) 2020 Open Whisper Systems. All rights reserved.
//
import Foundation
import PromiseKit
extension OWS2FAManager {
var networkManager: TSNetworkManager {
return .shared()
}
var databaseStorage: SDSDatabaseStorage {
return .shared
}
public static var isRegistrationLockV2EnabledKey = "isRegistrationLockV2Enabled"
@objc
public var isRegistrationLockEnabled: Bool {
switch mode {
case .V2:
return isRegistrationLockV2Enabled
case .V1:
return true // In v1 reg lock and 2fa are the same thing.
case .disabled:
return false
}
}
@objc
public var isRegistrationLockV2Enabled: Bool {
return databaseStorage.read { transaction in
OWS2FAManager.keyValueStore().getBool(
OWS2FAManager.isRegistrationLockV2EnabledKey,
defaultValue: false,
transaction: transaction
)
}
}
public func isRegistrationLockV2Enabled(transaction: SDSAnyReadTransaction) -> Bool {
return OWS2FAManager.keyValueStore().getBool(
OWS2FAManager.isRegistrationLockV2EnabledKey,
defaultValue: false,
transaction: transaction
)
}
public func requestEnable2FA(withPin pin: String, mode: OWS2FAMode, rotateMasterKey: Bool = false) -> Promise<Void> {
return Promise { resolver in
requestEnable2FA(withPin: pin, mode: mode, rotateMasterKey: rotateMasterKey, success: {
resolver.fulfill(())
}) { error in
resolver.reject(error)
}
}
}
@objc
@available(swift, obsoleted: 1.0)
public func enableRegistrationLockV2() -> AnyPromise {
return AnyPromise(enableRegistrationLockV2())
}
public func enableRegistrationLockV2() -> Promise<Void> {
return DispatchQueue.global().async(.promise) { () -> String in
guard let token = KeyBackupService.deriveRegistrationLockToken() else {
throw OWSAssertionError("Cannot enable registration lock without an existing PIN")
}
return token
}.then { token -> Promise<TSNetworkManager.Response> in
let request = OWSRequestFactory.enableRegistrationLockV2Request(withToken: token)
return self.networkManager.makePromise(request: request)
}.done { _ in
self.databaseStorage.write { transaction in
OWS2FAManager.keyValueStore().setBool(
true,
key: OWS2FAManager.isRegistrationLockV2EnabledKey,
transaction: transaction
)
}
firstly {
TSAccountManager.sharedInstance().updateAccountAttributes()
}.catch { error in
Logger.error("Error: \(error)")
}
}
}
public func markRegistrationLockV2Enabled(transaction: SDSAnyWriteTransaction) {
guard !TSAccountManager.sharedInstance().isRegistered else {
return owsFailDebug("Unexpectedly attempted to mark reglock as enabled after registration")
}
OWS2FAManager.keyValueStore().setBool(
true,
key: OWS2FAManager.isRegistrationLockV2EnabledKey,
transaction: transaction
)
}
@objc
@available(swift, obsoleted: 1.0)
public func disableRegistrationLockV2() -> AnyPromise {
return AnyPromise(disableRegistrationLockV2())
}
public func disableRegistrationLockV2() -> Promise<Void> {
return firstly { () -> Promise<TSNetworkManager.Response> in
let request = OWSRequestFactory.disableRegistrationLockV2Request()
return self.networkManager.makePromise(request: request)
}.done { _ in
self.databaseStorage.write { transaction in
OWS2FAManager.keyValueStore().removeValue(
forKey: OWS2FAManager.isRegistrationLockV2EnabledKey,
transaction: transaction
)
}
firstly {
TSAccountManager.sharedInstance().updateAccountAttributes()
}.catch { error in
Logger.error("Error: \(error)")
}
}
}
public func markRegistrationLockV2Disabled(transaction: SDSAnyWriteTransaction) {
guard !TSAccountManager.sharedInstance().isRegistered else {
return owsFailDebug("Unexpectedly attempted to mark reglock as disabled after registration")
}
OWS2FAManager.keyValueStore().removeValue(
forKey: OWS2FAManager.isRegistrationLockV2EnabledKey,
transaction: transaction
)
}
@objc
@available(swift, obsoleted: 1.0)
public func migrateToRegistrationLockV2() -> AnyPromise {
return AnyPromise(migrateToRegistrationLockV2())
}
public func migrateToRegistrationLockV2() -> Promise<Void> {
guard let pinCode = pinCode else {
return Promise(error: OWSAssertionError("tried to migrate to registration lock V2 without legacy PIN"))
}
return firstly {
return requestEnable2FA(withPin: pinCode, mode: .V2)
}.then {
return self.enableRegistrationLockV2()
}
}
}