-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic FIDO reset functionality implemented for NFC keys.
- Loading branch information
1 parent
10d974c
commit 840d544
Showing
6 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2022 Yubico. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Foundation | ||
|
||
class FIDOResetViewModel: ObservableObject { | ||
|
||
@Published var state: ResetState = .ready | ||
|
||
enum ResetState: Equatable { | ||
case ready, success, error(String) | ||
} | ||
|
||
private let connection = Connection() | ||
|
||
func reset() { | ||
connection.startConnection { connection in | ||
connection.fido2Session { session, error in | ||
guard let session = session else { | ||
let errorMessage = error?.localizedDescription ?? "Unknown error" | ||
YubiKitManager.shared.stopNFCConnection(withErrorMessage: errorMessage) | ||
self.state = .error(errorMessage) | ||
return | ||
} | ||
session.reset { error in | ||
if let error = error { | ||
YubiKitManager.shared.stopNFCConnection(withErrorMessage: error.localizedDescription) | ||
self.state = .error(error.localizedDescription) | ||
} else { | ||
let message = String(localized: "FIDO accounts deleted and FIDO application reset to factory defaults.", comment: "FIDO reset confirmation message") | ||
YubiKitManager.shared.stopNFCConnection(withMessage: message) | ||
self.state = .success | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
deinit { | ||
print("deinit FIDOResetViewModel") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2022 Yubico. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import SwiftUI | ||
|
||
struct FIDOResetView: View { | ||
|
||
@StateObject var model = FIDOResetViewModel() | ||
@State var presentConfirmAlert = false | ||
@State var presentErrorAlert = false | ||
@State var keyHasBeenReset = false | ||
@State var errorMessage: String? = nil | ||
|
||
var body: some View { | ||
SettingsView(image: Image(systemName: "exclamationmark.triangle").foregroundColor(.red)) { | ||
Text(keyHasBeenReset ? "FIDO has been reset" : "Reset FIDO application").font(.headline) | ||
Text("Reset all FIDO accounts stored on YubiKey, make sure they are not in use anywhere before doing this.") | ||
.multilineTextAlignment(.center) | ||
.opacity(keyHasBeenReset ? 0.2 : 1.0) | ||
} buttons: { | ||
SettingsButton("Reset Yubikey") { | ||
presentConfirmAlert.toggle() | ||
} | ||
.disabled(keyHasBeenReset) | ||
} | ||
.navigationBarTitle(Text("Reset FIDO"), displayMode: .inline) | ||
.alert("Confirm FIDO reset", isPresented: $presentConfirmAlert, presenting: model, actions: { model in | ||
Button(role: .destructive) { | ||
presentConfirmAlert.toggle() | ||
model.reset() | ||
} label: { | ||
Text("Reset") | ||
} | ||
Button(role: .cancel) { | ||
presentConfirmAlert.toggle() | ||
} label: { | ||
Text("Cancel") | ||
} | ||
}) | ||
.alert(errorMessage ?? "Unknown error", isPresented: $presentErrorAlert, actions: { }) | ||
.onChange(of: model.state) { state in | ||
withAnimation { | ||
switch state { | ||
case .ready: | ||
self.keyHasBeenReset = false | ||
case .success: | ||
self.keyHasBeenReset = true | ||
case .error(let message): | ||
self.presentErrorAlert = true | ||
self.errorMessage = message | ||
} | ||
} | ||
} | ||
} | ||
} |