-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathAuthenticationSettingsViewController.swift
342 lines (283 loc) · 13.5 KB
/
AuthenticationSettingsViewController.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* 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 UIKit
import Shared
import SwiftKeychainWrapper
import LocalAuthentication
private let logger = Logger.browserLogger
private func presentNavAsFormSheet(presented: UINavigationController, presenter: UINavigationController?, settings: AuthenticationSettingsViewController?) {
presented.modalPresentationStyle = .FormSheet
presenter?.presentViewController(presented, animated: true, completion: {
if let selectedRow = settings?.tableView.indexPathForSelectedRow {
settings?.tableView.deselectRowAtIndexPath(selectedRow, animated: false)
}
})
}
class TurnPasscodeOnSetting: Setting {
weak var settings: AuthenticationSettingsViewController?
init(settings: SettingsTableViewController, delegate: SettingsDelegate? = nil) {
self.settings = settings as? AuthenticationSettingsViewController
super.init(title: NSAttributedString.tableRowTitle(AuthenticationStrings.turnOnPasscode),
delegate: delegate)
}
override func onClick(navigationController: UINavigationController?) {
presentNavAsFormSheet(UINavigationController(rootViewController: SetupPasscodeViewController()),
presenter: navigationController,
settings: settings)
}
}
class TurnPasscodeOffSetting: Setting {
weak var settings: AuthenticationSettingsViewController?
init(settings: SettingsTableViewController, delegate: SettingsDelegate? = nil) {
self.settings = settings as? AuthenticationSettingsViewController
super.init(title: NSAttributedString.tableRowTitle(AuthenticationStrings.turnOffPasscode),
delegate: delegate)
}
override func onClick(navigationController: UINavigationController?) {
presentNavAsFormSheet(UINavigationController(rootViewController: RemovePasscodeViewController()),
presenter: navigationController,
settings: settings)
}
}
class ChangePasscodeSetting: Setting {
weak var settings: AuthenticationSettingsViewController?
init(settings: SettingsTableViewController, delegate: SettingsDelegate? = nil, enabled: Bool) {
self.settings = settings as? AuthenticationSettingsViewController
let attributedTitle: NSAttributedString = (enabled ?? false) ?
NSAttributedString.tableRowTitle(AuthenticationStrings.changePasscode) :
NSAttributedString.disabledTableRowTitle(AuthenticationStrings.changePasscode)
super.init(title: attributedTitle,
delegate: delegate,
enabled: enabled)
}
override func onClick(navigationController: UINavigationController?) {
presentNavAsFormSheet(UINavigationController(rootViewController: ChangePasscodeViewController()),
presenter: navigationController,
settings: settings)
}
}
class RequirePasscodeSetting: Setting {
weak var settings: AuthenticationSettingsViewController?
private weak var navigationController: UINavigationController?
override var accessoryType: UITableViewCellAccessoryType { return .DisclosureIndicator }
override var style: UITableViewCellStyle { return .Value1 }
override var status: NSAttributedString {
// Only show the interval if we are enabled and have an interval set.
let authenticationInterval = KeychainWrapper.defaultKeychainWrapper().authenticationInfo()
if let interval = authenticationInterval?.requiredPasscodeInterval where enabled {
return NSAttributedString.disabledTableRowTitle(interval.settingTitle)
}
return NSAttributedString(string: "")
}
init(settings: SettingsTableViewController, delegate: SettingsDelegate? = nil, enabled: Bool? = nil) {
self.navigationController = settings.navigationController
self.settings = settings as? AuthenticationSettingsViewController
let title = AuthenticationStrings.requirePasscode
let attributedTitle = (enabled ?? true) ? NSAttributedString.tableRowTitle(title) : NSAttributedString.disabledTableRowTitle(title)
super.init(title: attributedTitle,
delegate: delegate,
enabled: enabled)
}
func deselectRow () {
if let selectedRow = self.settings?.tableView.indexPathForSelectedRow {
self.settings?.tableView.deselectRowAtIndexPath(selectedRow, animated: true)
}
}
override func onClick(_: UINavigationController?) {
guard let authInfo = KeychainWrapper.defaultKeychainWrapper().authenticationInfo() else {
navigateToRequireInterval()
return
}
if authInfo.requiresValidation() {
AppAuthenticator.presentAuthenticationUsingInfo(authInfo,
touchIDReason: AuthenticationStrings.requirePasscodeTouchReason,
success: {
self.navigateToRequireInterval()
},
cancel: nil,
fallback: {
AppAuthenticator.presentPasscodeAuthentication(self.navigationController, delegate: self)
self.deselectRow()
})
} else {
self.navigateToRequireInterval()
}
}
private func navigateToRequireInterval() {
navigationController?.pushViewController(RequirePasscodeIntervalViewController(), animated: true)
}
}
extension RequirePasscodeSetting: PasscodeEntryDelegate {
@objc func passcodeValidationDidSucceed() {
navigationController?.dismissViewControllerAnimated(true) {
self.navigateToRequireInterval()
}
}
}
class TouchIDSetting: Setting {
private let authInfo: AuthenticationKeychainInfo?
private weak var navigationController: UINavigationController?
private weak var switchControl: UISwitch?
private var touchIDSuccess: (() -> Void)? = nil
private var touchIDFallback: (() -> Void)? = nil
init(
title: NSAttributedString?,
navigationController: UINavigationController? = nil,
delegate: SettingsDelegate? = nil,
enabled: Bool? = nil,
touchIDSuccess: (() -> Void)? = nil,
touchIDFallback: (() -> Void)? = nil) {
self.touchIDSuccess = touchIDSuccess
self.touchIDFallback = touchIDFallback
self.navigationController = navigationController
self.authInfo = KeychainWrapper.defaultKeychainWrapper().authenticationInfo()
super.init(title: title, delegate: delegate, enabled: enabled)
}
override func onConfigureCell(cell: UITableViewCell) {
super.onConfigureCell(cell)
cell.selectionStyle = .None
// In order for us to recognize a tap gesture without toggling the switch,
// the switch is wrapped in a UIView which has a tap gesture recognizer. This way
// we can disable interaction of the switch and still handle tap events.
let control = UISwitch()
control.onTintColor = UIConstants.ControlTintColor
control.on = authInfo?.useTouchID ?? false
control.userInteractionEnabled = false
switchControl = control
let accessoryContainer = UIView(frame: control.frame)
accessoryContainer.addSubview(control)
let gesture = UITapGestureRecognizer(target: self, action: #selector(TouchIDSetting.switchTapped))
accessoryContainer.addGestureRecognizer(gesture)
cell.accessoryView = accessoryContainer
}
@objc private func switchTapped() {
guard let authInfo = authInfo else {
logger.error("Authentication info should always be present when modifying Touch ID preference.")
return
}
if authInfo.useTouchID {
AppAuthenticator.presentAuthenticationUsingInfo(
authInfo,
touchIDReason: AuthenticationStrings.disableTouchReason,
success: self.touchIDSuccess,
cancel: nil,
fallback: self.touchIDFallback
)
} else {
toggleTouchID(enabled: true)
}
}
func toggleTouchID(enabled enabled: Bool) {
authInfo?.useTouchID = enabled
KeychainWrapper.defaultKeychainWrapper().setAuthenticationInfo(authInfo)
switchControl?.setOn(enabled, animated: true)
}
}
class AuthenticationSettingsViewController: SettingsTableViewController {
override func viewDidLoad() {
super.viewDidLoad()
updateTitleForTouchIDState()
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: #selector(AuthenticationSettingsViewController.refreshSettings(_:)), name: NotificationPasscodeDidRemove, object: nil)
notificationCenter.addObserver(self, selector: #selector(AuthenticationSettingsViewController.refreshSettings(_:)), name: NotificationPasscodeDidCreate, object: nil)
notificationCenter.addObserver(self, selector: #selector(AuthenticationSettingsViewController.refreshSettings(_:)), name: UIApplicationDidBecomeActiveNotification, object: nil)
tableView.accessibilityIdentifier = "AuthenticationManager.settingsTableView"
}
deinit {
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.removeObserver(self, name: NotificationPasscodeDidRemove, object: nil)
notificationCenter.removeObserver(self, name: NotificationPasscodeDidCreate, object: nil)
notificationCenter.removeObserver(self, name: UIApplicationDidBecomeActiveNotification, object: nil)
}
override func generateSettings() -> [SettingSection] {
if let _ = KeychainWrapper.defaultKeychainWrapper().authenticationInfo() {
return passcodeEnabledSettings()
} else {
return passcodeDisabledSettings()
}
}
private func updateTitleForTouchIDState() {
if LAContext().canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil) {
navigationItem.title = AuthenticationStrings.touchIDPasscodeSetting
} else {
navigationItem.title = AuthenticationStrings.passcode
}
}
private func passcodeEnabledSettings() -> [SettingSection] {
var settings = [SettingSection]()
let passcodeSectionTitle = NSAttributedString(string: AuthenticationStrings.passcode)
let passcodeSection = SettingSection(title: passcodeSectionTitle, children: [
TurnPasscodeOffSetting(settings: self),
ChangePasscodeSetting(settings: self, delegate: nil, enabled: true)
])
var requirePasscodeSectionChildren: [Setting] = [RequirePasscodeSetting(settings: self)]
let localAuthContext = LAContext()
if localAuthContext.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil) {
requirePasscodeSectionChildren.append(
TouchIDSetting(
title: NSAttributedString.tableRowTitle(
NSLocalizedString("Use Touch ID", tableName: "AuthenticationManager", comment: "List section title for when to use Touch ID")
),
navigationController: self.navigationController,
delegate: nil,
enabled: true,
touchIDSuccess: { [unowned self] in
self.touchIDAuthenticationSucceeded()
},
touchIDFallback: { [unowned self] in
self.fallbackOnTouchIDFailure()
}
)
)
}
let requirePasscodeSection = SettingSection(title: nil, children: requirePasscodeSectionChildren)
settings += [
passcodeSection,
requirePasscodeSection,
]
return settings
}
private func passcodeDisabledSettings() -> [SettingSection] {
var settings = [SettingSection]()
let passcodeSectionTitle = NSAttributedString(string: AuthenticationStrings.passcode)
let passcodeSection = SettingSection(title: passcodeSectionTitle, children: [
TurnPasscodeOnSetting(settings: self),
ChangePasscodeSetting(settings: self, delegate: nil, enabled: false)
])
let requirePasscodeSection = SettingSection(title: nil, children: [
RequirePasscodeSetting(settings: self, delegate: nil, enabled: false),
])
settings += [
passcodeSection,
requirePasscodeSection,
]
return settings
}
}
extension AuthenticationSettingsViewController {
func refreshSettings(notification: NSNotification) {
updateTitleForTouchIDState()
settings = generateSettings()
tableView.reloadData()
}
}
extension AuthenticationSettingsViewController: PasscodeEntryDelegate {
private func getTouchIDSetting() -> TouchIDSetting? {
guard settings.count >= 2 && settings[1].count >= 2 else {
return nil
}
return settings[1][1] as? TouchIDSetting
}
func touchIDAuthenticationSucceeded() {
getTouchIDSetting()?.toggleTouchID(enabled: false)
}
func fallbackOnTouchIDFailure() {
AppAuthenticator.presentPasscodeAuthentication(self.navigationController, delegate: self)
}
@objc func passcodeValidationDidSucceed() {
getTouchIDSetting()?.toggleTouchID(enabled: false)
navigationController?.dismissViewControllerAnimated(true, completion: nil)
}
}