| 1 |
/* |
| 2 |
* This file is part of the 2FA Manager App for Ubuntu Touch |
| 3 |
* Copyright (C) 2019 Joan CiberSheep |
| 4 |
* |
| 5 |
* This App is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 3 as |
| 7 |
* published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* |
| 10 |
* This App is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
import QtQuick 2.9 |
| 21 |
import Lomiri.Components 1.3 |
| 22 |
import Lomiri.Components.Popups 1.3 |
| 23 |
|
| 24 |
import QtQuick.LocalStorage 2.0 |
| 25 |
//import "../js/otpauth.min.js" as OTPAuth |
| 26 |
import "../js/db.js" as KeysDB |
| 27 |
|
| 28 |
Page { |
| 29 |
id: keyInfoPage |
| 30 |
objectName: "keyInfoPage" |
| 31 |
anchors.fill: parent |
| 32 |
|
| 33 |
property var key |
| 34 |
property int id |
| 35 |
property int reminingTime |
| 36 |
property string secret |
| 37 |
property string url |
| 38 |
property string desc |
| 39 |
|
| 40 |
property bool isTOTP |
| 41 |
property string authType |
| 42 |
|
| 43 |
property bool showPrivateInfo: false |
| 44 |
property string counterText: i18n.tr("Counter: %1").arg(key.counter) |
| 45 |
|
| 46 |
header: HeaderInfo { |
| 47 |
id: pageHeader |
| 48 |
title: i18n.tr("%1 Key Information").arg(desc) |
| 49 |
flickable: mainFlickable |
| 50 |
|
| 51 |
onEditKey: { |
| 52 |
mainStack.pop() |
| 53 |
mainStack.push(Qt.resolvedUrl("PageEditAddKey.qml"), { |
| 54 |
"editMode": true, |
| 55 |
"uriToEdit": url, |
| 56 |
"descriptionToEdit": desc, |
| 57 |
"idToUpdate": id |
| 58 |
}); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
ProgressBar { |
| 63 |
id: bar |
| 64 |
visible: isTOTP |
| 65 |
width: parent.width |
| 66 |
anchors.top: header.bottom |
| 67 |
|
| 68 |
maximumValue: key.period || 0 |
| 69 |
minimumValue: 0 |
| 70 |
} |
| 71 |
|
| 72 |
ScrollView { |
| 73 |
width: parent.width |
| 74 |
height: parent.height |
| 75 |
contentItem: mainFlickable |
| 76 |
} |
| 77 |
|
| 78 |
Flickable { |
| 79 |
id: mainFlickable |
| 80 |
width: parent.width |
| 81 |
height: parent.height |
| 82 |
contentHeight: mainColumn.height |
| 83 |
topMargin: units.gu(2) |
| 84 |
|
| 85 |
Column { |
| 86 |
id: mainColumn |
| 87 |
width: parent.width |
| 88 |
anchors.top: parent.top |
| 89 |
anchors.topMargin: units.gu(1) |
| 90 |
spacing: units.gu(2) |
| 91 |
|
| 92 |
Label { |
| 93 |
text: i18n.tr("%1 generator: %2 at %32").arg(authType).arg(key.label).arg(key.issuer) |
| 94 |
textSize: Label.Small |
| 95 |
anchors.horizontalCenter: parent.horizontalCenter |
| 96 |
} |
| 97 |
|
| 98 |
LomiriShape { |
| 99 |
id: contentShape |
| 100 |
anchors.horizontalCenter: parent.horizontalCenter |
| 101 |
width: Math.min(parent.width - units.gu(8), units.gu(68)) |
| 102 |
height: generatedKey.height + units.gu(8) |
| 103 |
aspect: LomiriShape.Flat |
| 104 |
color: root.highlightColor |
| 105 |
|
| 106 |
onWidthChanged: generatedKey.adjustTextSize() |
| 107 |
|
| 108 |
Label { |
| 109 |
id: generatedKey |
| 110 |
anchors.centerIn: parent |
| 111 |
|
| 112 |
text: root.formatCode(generateKey(isTOTP), true) |
| 113 |
|
| 114 |
//TODO: This might be too slow for what it does. Consider |
| 115 |
//From Dialer App |
| 116 |
property double maximumFontSize: units.dp(35) |
| 117 |
readonly property double minimumFontSize: FontUtils.sizeToPixels("medium") |
| 118 |
property bool __adjusting: false |
| 119 |
|
| 120 |
fontSizeMode: Text.HorizontalFit |
| 121 |
|
| 122 |
function adjustTextSize(){ |
| 123 |
// avoid infinite recursion here |
| 124 |
if (__adjusting) return; |
| 125 |
|
| 126 |
__adjusting = true; |
| 127 |
|
| 128 |
// start by resetting the font size to discover the scale that should be used |
| 129 |
font.pixelSize = maximumFontSize |
| 130 |
|
| 131 |
// check if it really needs to be scaled |
| 132 |
if (contentShape.width < generatedKey.width) { |
| 133 |
var factor = contentShape.width / generatedKey.width; |
| 134 |
font.pixelSize = Math.max(font.pixelSize * factor, minimumFontSize); |
| 135 |
} |
| 136 |
__adjusting = false |
| 137 |
} |
| 138 |
|
| 139 |
Connections { |
| 140 |
target: units |
| 141 |
onGridUnitChanged: adjustTextSize() |
| 142 |
} |
| 143 |
|
| 144 |
Component.onCompleted: adjustTextSize() |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
Button { |
| 149 |
anchors.horizontalCenter: parent.horizontalCenter |
| 150 |
width: contentShape.width |
| 151 |
text: i18n.tr("Copy to clipboard") |
| 152 |
onClicked: Clipboard.push(root.formatCode(generatedKey.text, false)) |
| 153 |
color: theme.palette.normal.focus |
| 154 |
} |
| 155 |
|
| 156 |
Button { |
| 157 |
visible: !isTOTP |
| 158 |
anchors.horizontalCenter: parent.horizontalCenter |
| 159 |
width: contentShape.width |
| 160 |
text: i18n.tr("Generate password") |
| 161 |
|
| 162 |
onClicked: { |
| 163 |
var difference = 1; |
| 164 |
url = KeysDB.updateCounter(id, difference); |
| 165 |
keyInfoPage.key = OTPAuth.URI.parse(url); |
| 166 |
generatedKey.text = generateKey(isTOTP); |
| 167 |
counterText = i18n.tr("Counter: %1").arg(key.counter) |
| 168 |
root.initDB(); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
Rectangle { |
| 173 |
width: parent.width |
| 174 |
height: units.gu(2) |
| 175 |
color: "transparent" |
| 176 |
} |
| 177 |
|
| 178 |
Item { |
| 179 |
width: parent.width |
| 180 |
height: moreMenu.height + units.dp(1) |
| 181 |
|
| 182 |
ListItem { |
| 183 |
id: moreMenu |
| 184 |
highlightColor: root.highlightColor |
| 185 |
divider.visible: !showPrivateInfo |
| 186 |
|
| 187 |
Divider {} |
| 188 |
|
| 189 |
ListItemLayout { |
| 190 |
title.text: i18n.tr("Complete Key Info") |
| 191 |
ProgressionSlot { name: showPrivateInfo ? "go-up" : "go-down"} |
| 192 |
} |
| 193 |
|
| 194 |
onClicked: showPrivateInfo = !showPrivateInfo |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
Loader { |
| 199 |
id: privateInfo |
| 200 |
active: showPrivateInfo |
| 201 |
width: parent.width |
| 202 |
|
| 203 |
sourceComponent: privateInfoComponent |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
Component { |
| 209 |
id: privateInfoComponent |
| 210 |
|
| 211 |
Column { |
| 212 |
LomiriShape { |
| 213 |
anchors.horizontalCenter: parent.horizontalCenter |
| 214 |
width: units.gu(21) |
| 215 |
height: width |
| 216 |
sourceFillMode: LomiriShape.Pad |
| 217 |
aspect: LomiriShape.Flat |
| 218 |
backgroundColor: "#ffffff" |
| 219 |
|
| 220 |
source: QRCode { |
| 221 |
id: qr |
| 222 |
width: parent.width - units.gu(4) |
| 223 |
height: width |
| 224 |
anchors.bottom: parent.bottom |
| 225 |
value : url |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
Text { |
| 230 |
text: i18n.tr("Secret Key") |
| 231 |
anchors.horizontalCenter: parent.horizontalCenter |
| 232 |
height: units.gu(5) |
| 233 |
verticalAlignment: Text.AlignBottom |
| 234 |
color: theme.palette.normal.baseText |
| 235 |
} |
| 236 |
|
| 237 |
Label { |
| 238 |
text: secret |
| 239 |
textSize: Label.XSmall |
| 240 |
anchors.horizontalCenter: parent.horizontalCenter |
| 241 |
color: theme.palette.normal.baseText |
| 242 |
} |
| 243 |
|
| 244 |
Text { |
| 245 |
text: i18n.tr("Algorithm: %1").arg(key.algorithm) |
| 246 |
anchors.horizontalCenter: parent.horizontalCenter |
| 247 |
color: theme.palette.normal.baseText |
| 248 |
} |
| 249 |
|
| 250 |
Text { |
| 251 |
text: i18n.tr("Password Length: %1").arg(key.digits) |
| 252 |
anchors.horizontalCenter: parent.horizontalCenter |
| 253 |
color: theme.palette.normal.baseText |
| 254 |
} |
| 255 |
|
| 256 |
Text { |
| 257 |
id: keyPeriod |
| 258 |
visible: isTOTP |
| 259 |
text: i18n.tr("Interval: %1").arg(key.period) |
| 260 |
anchors.horizontalCenter: parent.horizontalCenter |
| 261 |
color: theme.palette.normal.baseText |
| 262 |
} |
| 263 |
|
| 264 |
Text { |
| 265 |
id: counterHtop |
| 266 |
text: counterText |
| 267 |
visible: !isTOTP |
| 268 |
anchors.horizontalCenter: parent.horizontalCenter |
| 269 |
color: theme.palette.normal.baseText |
| 270 |
} |
| 271 |
|
| 272 |
Rectangle { |
| 273 |
width: parent.width |
| 274 |
height: units.gu(4) |
| 275 |
color: "transparent" |
| 276 |
} |
| 277 |
|
| 278 |
Divider {} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
//Let's connect with the main ticker |
| 283 |
Connections { |
| 284 |
target: timer |
| 285 |
|
| 286 |
onTriggered: { |
| 287 |
generatedKey.text = generateKey(isTOTP) |
| 288 |
|
| 289 |
bar.value = timer.time % key.period |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
Component.onCompleted: { |
| 294 |
isTOTP = key.counter == undefined; |
| 295 |
bar.value = reminingTime; |
| 296 |
|
| 297 |
authType = isTOTP |
| 298 |
? "TOTP" |
| 299 |
: "HTOP" |
| 300 |
} |
| 301 |
|
| 302 |
function generateKey(boolVar) { |
| 303 |
return boolVar |
| 304 |
? root.formatCode(OTPAuth.TOTP.generate(key), true) |
| 305 |
: root.formatCode(OTPAuth.HOTP.generate(key), true) |
| 306 |
} |
| 307 |
} |