aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/PrivateDataConfirmation.swift
blob: c03e64a79f54a85ccd710d66eb9ceb147b3f67ed (plain) (blame)
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
// SPDX-License-Identifier: MIT
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.

import Foundation
import LocalAuthentication
#if os(macOS)
import AppKit
#endif

class PrivateDataConfirmation {
    static func confirmAccess(to reason: String, _ after: @escaping () -> Void) {
        let context = LAContext()

        var error: NSError?
        if !context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
            guard let error = error as? LAError else { return }
            if error.code == .passcodeNotSet {
                // We give no protection to folks who just don't set a passcode.
                after()
            }
            return
        }

        context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { success, _ in
            DispatchQueue.main.async {
                #if os(macOS)
                if !NSApp.isActive {
                    NSApp.activate(ignoringOtherApps: true)
                }
                #endif
                if success {
                    after()
                }
            }
        }
    }
}