1import QtQuick 2.9
2import QtQuick.Controls 2.2
3import QtQuick.Layouts 1.2
4import QtQuick.Controls.Material 2.2
5
6ColumnLayout {
7    property bool hasPin
8    property string pinMessage
9    property bool isBusy
10
11    readonly property bool hasProtectedKey: pivData.has_protected_key || false
12    readonly property var pivData: yubiKey.piv || {
13
14                                   }
15    readonly property bool pinBlocked: pinRetries < 1
16    readonly property int pinRetries: pivData.pin_tries || 0
17    readonly property bool pukBlocked: yubiKey.pivPukBlocked
18                                       || pivData.puk_blocked || false
19
20    StackView.onActivating: load()
21
22    objectName: "pivPinManagementView"
23
24    function load() {
25        isBusy = true
26        yubiKey.refreshPivData(function (resp) {
27            isBusy = false
28            if (!resp.success) {
29                snackbarError.showResponseError(resp)
30                views.home()
31            }
32        })
33    }
34
35    function getPinMessage() {
36        if (pinBlocked) {
37            return qsTr("PIN is blocked.")
38        } else {
39            return qsTr("%1 retries left").arg(pinRetries)
40        }
41    }
42
43    function getPukMessage() {
44        if (pukBlocked) {
45            return qsTr("PUK is blocked.")
46        } else {
47            return qsTr("PIN Unlock Key")
48        }
49    }
50
51    function getManagementKeyMessage() {
52        if (pivData.has_derived_key) {
53            return qsTr("Derived from PIN")
54        } else if (pivData.has_stored_key) {
55            if (pinBlocked) {
56                return qsTr("Not readable, PIN is blocked")
57            } else {
58                return qsTr("Protected by PIN")
59            }
60        }
61        return qsTr("Not protected by PIN")
62    }
63
64    BusyIndicator {
65        Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
66        running: isBusy
67        visible: running
68    }
69
70    CustomContentColumn {
71        visible: !isBusy
72
73        ViewHeader {
74            breadcrumbs: [qsTr("PIV"), qsTr("Configure PINs")]
75        }
76
77        RowLayout {
78            Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
79            Layout.fillHeight: true
80            Layout.fillWidth: true
81            spacing: 30
82
83            ColumnLayout {
84
85                Heading2 {
86                    text: qsTr("PIN")
87                    font.pixelSize: constants.h2
88                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
89                }
90                Label {
91                    text: getPinMessage()
92                    font.pixelSize: constants.h3
93                    color: yubicoGrey
94                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
95                }
96                CustomButton {
97                    text: qsTr("Change PIN")
98                    highlighted: true
99                    onClicked: views.pivChangePin()
100                    iconSource: "../images/lock.svg"
101                    visible: !pinBlocked
102                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
103                }
104                CustomButton {
105                    text: qsTr("Unblock PIN")
106                    highlighted: true
107                    onClicked: views.pivUnblockPin()
108                    toolTipText: qsTr("Unblock PIN")
109                    iconSource: "../images/reset.svg"
110                    visible: pinBlocked
111                    enabled: !pukBlocked
112                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
113                }
114            }
115
116            ColumnSeparator {
117            }
118
119            ColumnLayout {
120
121                Heading2 {
122                    text: qsTr("PUK")
123                    font.pixelSize: constants.h2
124                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
125                }
126                Label {
127                    text: getPukMessage()
128                    font.pixelSize: constants.h3
129                    color: yubicoGrey
130                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
131                }
132                CustomButton {
133                    text: qsTr("Change PUK")
134                    highlighted: true
135                    onClicked: views.pivChangePuk()
136                    iconSource: "../images/lock.svg"
137                    enabled: !pukBlocked
138                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
139                }
140            }
141
142            ColumnSeparator {
143            }
144
145            ColumnLayout {
146
147                Heading2 {
148                    text: qsTr("Management Key")
149                    font.pixelSize: constants.h2
150                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
151                }
152                Label {
153                    text: getManagementKeyMessage()
154                    font.pixelSize: constants.h3
155                    color: yubicoGrey
156                    wrapMode: Text.WordWrap
157                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
158                }
159                CustomButton {
160                    text: qsTr("Change Management Key")
161                    highlighted: true
162                    onClicked: views.pivChangeManagementKey()
163                    toolTipText: qsTr("Change the PIV management key")
164                    iconSource: "../images/lock.svg"
165                    enabled: !(hasProtectedKey && pinBlocked)
166                    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
167                }
168            }
169        }
170
171        ButtonsBar {
172        }
173    }
174}
175