Skip to content

Commit

Permalink
Merge pull request #139 from openziti/dh_dev
Browse files Browse the repository at this point in the history
Improved Export of MFA recovery codes
  • Loading branch information
smilindave26 authored Apr 5, 2023
2 parents c3241b4 + 61aeb7d commit 2a02085
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 32 deletions.
2 changes: 1 addition & 1 deletion ZitiPacketTunnel/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ Gw
</tableView>
</subviews>
</clipView>
<scroller key="horizontalScroller" wantsLayer="YES" verticalHuggingPriority="750" doubleValue="0.0070921985815602835" horizontal="YES" id="C7A-EH-yEL">
<scroller key="horizontalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="C7A-EH-yEL">
<rect key="frame" x="1" y="264" width="442" height="16"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
Expand Down
63 changes: 39 additions & 24 deletions ZitiPacketTunnel/EditableTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,48 @@
import Foundation
import Cocoa

class EditableNSTextField: NSTextField {
func performKeyEquivalent(with event: NSEvent, from target:Any) -> Bool {
let commandKey = NSEvent.ModifierFlags.command.rawValue
let commandShiftKey = NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.shift.rawValue

if event.type == NSEvent.EventType.keyDown {
if (event.modifierFlags.rawValue & NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue) == commandKey {
switch event.charactersIgnoringModifiers! {
case "x":
if NSApp.sendAction(#selector(NSText.cut(_:)), to: nil, from: target) { return true }
case "c":
if NSApp.sendAction(#selector(NSText.copy(_:)), to: nil, from: target) { return true }
case "v":
if NSApp.sendAction(#selector(NSText.paste(_:)), to: nil, from: target) { return true }
case "z":
if NSApp.sendAction(Selector(("undo:")), to: nil, from: target) { return true }
case "a":
if NSApp.sendAction(#selector(NSResponder.selectAll(_:)), to: nil, from: target) { return true }
default:
break
}
} else if (event.modifierFlags.rawValue & NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue) == commandShiftKey {
if event.charactersIgnoringModifiers == "Z" {
if NSApp.sendAction(Selector(("redo:")), to: nil, from: target) { return true }
}
}
}
return false
}

private let commandKey = NSEvent.ModifierFlags.command.rawValue
private let commandShiftKey = NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.shift.rawValue
class EditableNSTextField: NSTextField {
override func performKeyEquivalent(with event: NSEvent) -> Bool {
if Ziti_Desktop_Edge.performKeyEquivalent(with: event, from: self) {
return true
}
return super.performKeyEquivalent(with: event)
}
}

class EditableNSTextView: NSTextView {
override func performKeyEquivalent(with event: NSEvent) -> Bool {
if event.type == NSEvent.EventType.keyDown {
if (event.modifierFlags.rawValue & NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue) == commandKey {
switch event.charactersIgnoringModifiers! {
case "x":
if NSApp.sendAction(#selector(NSText.cut(_:)), to: nil, from: self) { return true }
case "c":
if NSApp.sendAction(#selector(NSText.copy(_:)), to: nil, from: self) { return true }
case "v":
if NSApp.sendAction(#selector(NSText.paste(_:)), to: nil, from: self) { return true }
case "z":
if NSApp.sendAction(Selector(("undo:")), to: nil, from: self) { return true }
case "a":
if NSApp.sendAction(#selector(NSResponder.selectAll(_:)), to: nil, from: self) { return true }
default:
break
}
} else if (event.modifierFlags.rawValue & NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue) == commandShiftKey {
if event.charactersIgnoringModifiers == "Z" {
if NSApp.sendAction(Selector(("redo:")), to: nil, from: self) { return true }
}
}
if Ziti_Desktop_Edge.performKeyEquivalent(with: event, from: self) {
return true
}
return super.performKeyEquivalent(with: event)
}
Expand Down
66 changes: 60 additions & 6 deletions ZitiPacketTunnel/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,63 @@ class ViewController: NSViewController, NSTextFieldDelegate {
return alert.runModal() == .alertFirstButtonReturn
}

// simple dialog for showing recovery codes
func showRecoveryCodes(_ zid:ZitiIdentity, _ codes:[String]?) {
guard let codesStr = codes?.joined(separator: ", ") else {
self.dialogAlert("Recovery Codes", "no recovery codes available")
return
}

let alert = NSAlert()
alert.messageText = "Recovery Codes"
alert.alertStyle = .informational
alert.addButton(withTitle: "Save")
alert.addButton(withTitle: "Dismiss")

let scrollView = NSScrollView(frame: NSRect(x:0, y:0, width: 200, height: 100))
scrollView.hasVerticalScroller = true

let clipView = NSClipView(frame: scrollView.bounds)
clipView.autoresizingMask = [.width, .height]

let textView = EditableNSTextView(frame: clipView.bounds)
textView.autoresizingMask = [.width, .height]
textView.isEditable = false
textView.string = codesStr

clipView.documentView = textView
scrollView.contentView = clipView

alert.accessoryView = scrollView

let response = alert.runModal()
switch response {
case .alertFirstButtonReturn:
if textView.string.isEmpty {
return
}
let savePanel = NSSavePanel()
savePanel.title = "Save MFA Recovery Codes"
savePanel.nameFieldStringValue = "RecoveryCodes-\(zid.name).txt"
savePanel.prompt = "Save"
savePanel.allowedContentTypes = [UTType.text]

savePanel.begin { (result: NSApplication.ModalResponse) -> Void in
if result == NSApplication.ModalResponse.OK {
if let panelURL = savePanel.url {
do {
try codesStr.write(to: panelURL, atomically: true, encoding: .utf8)
} catch {
self.dialogAlert("Unable to store file", error.localizedDescription)
}
}
}
}

default: return
}
}

// brute force a QR code to setup MFA for now...
func setupMfaDialog(_ provisioningUrl:String) -> String? {
let alert = NSAlert()
Expand Down Expand Up @@ -640,8 +697,7 @@ class ViewController: NSViewController, NSTextFieldDelegate {
let updatedZid = self.zidStore.update(zId, [.Mfa])
self.updateServiceUI(zId:updatedZid)

let codes = mfaEnrollment.recoveryCodes?.joined(separator: ", ")
self.dialogAlert("Recovery Codes", codes ?? "no recovery codes available")
self.showRecoveryCodes(zId, mfaEnrollment.recoveryCodes)
}
}
} else {
Expand Down Expand Up @@ -803,8 +859,7 @@ class ViewController: NSViewController, NSTextFieldDelegate {
}

// Success!
let codes = codesMsg.codes?.joined(separator: ", ")
self.dialogAlert("Recovery Codes", codes ?? "no recovery codes available")
self.showRecoveryCodes(zid, codesMsg.codes)
}
}
}
Expand Down Expand Up @@ -834,8 +889,7 @@ class ViewController: NSViewController, NSTextFieldDelegate {
}

// Success!
let codes = codesMsg.codes?.joined(separator: ", ")
self.dialogAlert("Recovery Codes", codes ?? "no recovery codes available")
self.showRecoveryCodes(zid, codesMsg.codes)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ZitiPacketTunnel/ZitiPacketTunnel.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</array>
<key>com.apple.security.files.downloads.read-only</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
Expand Down

0 comments on commit 2a02085

Please sign in to comment.