| 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 |
|
| 23 |
Column { |
| 24 |
spacing: units.gu(2) |
| 25 |
|
| 26 |
property var uriJson |
| 27 |
property bool editMode |
| 28 |
property bool isTOTP: typeTF.keyType == "TOTP" |
| 29 |
property bool isInfoEmpty: isTOTP |
| 30 |
? issuerTF.text == "" || userTF.text == "" || intTF.text == "" || secretTF.text == "" |
| 31 |
: issuerTF.text == "" || userTF.text == "" || countTF.text == "" || secretTF.text == "" |
| 32 |
property alias userDesc: userDescTF.text |
| 33 |
|
| 34 |
onUriJsonChanged: { |
| 35 |
issuerTF.text = uriJson.issuer; |
| 36 |
userTF.text = uriJson.label |
| 37 |
secretTF.text = uriJson.secret.b32 |
| 38 |
|
| 39 |
if (isTOTP) { |
| 40 |
intTF.text = uriJson.period |
| 41 |
} else { |
| 42 |
typeTF.keyType = "HOTP" |
| 43 |
cType.selectedType = "HOTP" |
| 44 |
countTF.text = uriJson.counter |
| 45 |
} |
| 46 |
|
| 47 |
lengTF.passwordLength = uriJson.digits |
| 48 |
cLeng.selectedLeng = uriJson.digits |
| 49 |
|
| 50 |
algTF.algorithm = uriJson.algorithm |
| 51 |
cAlg.selectedAlg = uriJson.algorithm |
| 52 |
} |
| 53 |
|
| 54 |
Row { |
| 55 |
width: parent.width |
| 56 |
spacing: units.gu(2) |
| 57 |
|
| 58 |
Text { |
| 59 |
anchors.verticalCenter: parent.verticalCenter |
| 60 |
id: descTxt |
| 61 |
text: "<b>" + i18n.tr("Description") + "</b>" |
| 62 |
color: theme.palette.normal.overlayText |
| 63 |
} |
| 64 |
|
| 65 |
TextField { |
| 66 |
width: parent.width - units.gu(2) - descTxt.width |
| 67 |
id: userDescTF |
| 68 |
placeholderText: i18n.tr("Add a description") |
| 69 |
inputMethodHints: Qt.ImhNoPredictiveText |
| 70 |
|
| 71 |
onAccepted: { |
| 72 |
focus = false; |
| 73 |
issuerTF.focus = true; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
Label { |
| 79 |
visible: !editMode |
| 80 |
text: "<b>" + i18n.tr("Paste an <i>otpauth://</i> URI") + "</b>" |
| 81 |
height: units.gu(6) |
| 82 |
anchors.horizontalCenter: parent.horizontalCenter |
| 83 |
verticalAlignment: Text.AlignBottom |
| 84 |
textSize: Label.Medium |
| 85 |
} |
| 86 |
|
| 87 |
Row { |
| 88 |
visible: !editMode |
| 89 |
width: parent.width |
| 90 |
spacing: units.gu(2) |
| 91 |
|
| 92 |
TextField { |
| 93 |
id: pasteURI |
| 94 |
width: parent.width -renderURI.width -units.gu(2) |
| 95 |
placeholderText: i18n.tr("Paste URI") |
| 96 |
inputMethodHints: Qt.ImhNoPredictiveText |
| 97 |
onAccepted: renderURI.clicked() |
| 98 |
} |
| 99 |
|
| 100 |
Button { |
| 101 |
id: renderURI |
| 102 |
text: i18n.tr("Add") |
| 103 |
color: theme.palette.normal.positive |
| 104 |
enabled: pasteURI.text != "" |
| 105 |
onClicked: fillInfo(pasteURI.text) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
Label { |
| 110 |
id: errorMsg |
| 111 |
visible: false |
| 112 |
width: parent.width |
| 113 |
wrapMode: Text.WordWrap |
| 114 |
} |
| 115 |
|
| 116 |
Label { |
| 117 |
visible: !editMode |
| 118 |
text: "<b>" + i18n.tr("Or type the information") + "</b>" |
| 119 |
height: units.gu(6) |
| 120 |
anchors.horizontalCenter: parent.horizontalCenter |
| 121 |
verticalAlignment: Text.AlignBottom |
| 122 |
textSize: Label.Medium |
| 123 |
} |
| 124 |
|
| 125 |
Row { |
| 126 |
width: parent.width |
| 127 |
spacing: units.gu(2) |
| 128 |
|
| 129 |
Text { |
| 130 |
anchors.top:parent.top |
| 131 |
anchors.topMargin: units.gu(2) |
| 132 |
id: typeTxt |
| 133 |
text: "<b>" + i18n.tr("Type") + "</b>" |
| 134 |
color: theme.palette.normal.overlayText |
| 135 |
} |
| 136 |
|
| 137 |
ListItem { |
| 138 |
id: typeTF |
| 139 |
width: parent.width - units.gu(2) - typeTxt.width |
| 140 |
height: cType.height > 0 ? cType.height : units.gu(7) |
| 141 |
divider.visible: false |
| 142 |
|
| 143 |
property string keyType: "TOTP" |
| 144 |
|
| 145 |
ChooserType { |
| 146 |
id: cType |
| 147 |
width: parent.width |
| 148 |
|
| 149 |
onSelectedTypeChanged: typeTF.keyType = selectedType |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
Row { |
| 155 |
width: parent.width |
| 156 |
spacing: units.gu(2) |
| 157 |
|
| 158 |
Text { |
| 159 |
anchors.verticalCenter: parent.verticalCenter |
| 160 |
id: issuerTxt |
| 161 |
text: i18n.tr("<b>Issuer</b>") |
| 162 |
color: theme.palette.normal.overlayText |
| 163 |
} |
| 164 |
|
| 165 |
TextField { |
| 166 |
width: parent.width - units.gu(2) - issuerTxt.width |
| 167 |
id: issuerTF |
| 168 |
placeholderText: i18n.tr("Who is providing the service") |
| 169 |
inputMethodHints: Qt.ImhNoPredictiveText |
| 170 |
|
| 171 |
onAccepted: { |
| 172 |
focus = false; |
| 173 |
userTF.focus = true; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
Row { |
| 179 |
width: parent.width |
| 180 |
spacing: units.gu(2) |
| 181 |
|
| 182 |
Text { |
| 183 |
anchors.verticalCenter: parent.verticalCenter |
| 184 |
id: usrTxt |
| 185 |
text: "<b>" + i18n.tr("User") + "</b>" |
| 186 |
color: theme.palette.normal.overlayText |
| 187 |
} |
| 188 |
|
| 189 |
TextField { |
| 190 |
width: parent.width - units.gu(2) - usrTxt.width |
| 191 |
id: userTF |
| 192 |
placeholderText: i18n.tr("Who is using the service") |
| 193 |
inputMethodHints: Qt.ImhNoPredictiveText |
| 194 |
|
| 195 |
onAccepted: { |
| 196 |
focus = false; |
| 197 |
intTF.focus = true; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
Column { |
| 203 |
width: parent.width |
| 204 |
|
| 205 |
Row { |
| 206 |
width: parent.width |
| 207 |
spacing: units.gu(2) |
| 208 |
|
| 209 |
Text { |
| 210 |
anchors.top: parent.top |
| 211 |
anchors.topMargin: units.gu(2) |
| 212 |
id: algTxt |
| 213 |
text: "<b>" + i18n.tr("Algorithm") + "</b>" |
| 214 |
color: theme.palette.normal.overlayText |
| 215 |
} |
| 216 |
|
| 217 |
ListItem { |
| 218 |
id: algTF |
| 219 |
width: parent.width - units.gu(2) - algTxt.width |
| 220 |
height: cAlg.height > 0 ? cAlg.height : units.gu(7) |
| 221 |
divider.visible: false |
| 222 |
|
| 223 |
property string algorithm: "SHA1" |
| 224 |
|
| 225 |
ChooserAlgorithm { |
| 226 |
id: cAlg |
| 227 |
width: parent.width |
| 228 |
|
| 229 |
onSelectedAlgChanged: algTF.algorithm = selectedAlg |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
Row { |
| 235 |
width: parent.width |
| 236 |
spacing: units.gu(2) |
| 237 |
|
| 238 |
Text { |
| 239 |
anchors.top: parent.top |
| 240 |
anchors.topMargin: units.gu(2) |
| 241 |
id: passLenTxt |
| 242 |
text: "<b>" + i18n.tr("Password Length") + "</b>" |
| 243 |
color: theme.palette.normal.overlayText |
| 244 |
} |
| 245 |
|
| 246 |
ListItem { |
| 247 |
id: lengTF |
| 248 |
width: parent.width - units.gu(2) - passLenTxt.width |
| 249 |
height: cLeng.height > 0 ? cLeng.height : units.gu(7) |
| 250 |
divider.visible: false |
| 251 |
|
| 252 |
property string passwordLength: "6" |
| 253 |
|
| 254 |
ChooserLength { |
| 255 |
id: cLeng |
| 256 |
width: parent.width |
| 257 |
|
| 258 |
onSelectedLengChanged: lengTF.passwordLength = selectedLeng |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
Row { |
| 265 |
width: parent.width |
| 266 |
spacing: units.gu(2) |
| 267 |
visible: isTOTP |
| 268 |
|
| 269 |
Text { |
| 270 |
anchors.verticalCenter: parent.verticalCenter |
| 271 |
id: keyPeriod |
| 272 |
visible: true //uriJson.counter == undefined |
| 273 |
text: "<b>" + i18n.tr("Interval") + "</b>" |
| 274 |
color: theme.palette.normal.overlayText |
| 275 |
} |
| 276 |
|
| 277 |
TextField { |
| 278 |
width: parent.width - units.gu(2) - keyPeriod.width |
| 279 |
id: intTF |
| 280 |
text: "30" |
| 281 |
placeholderText: i18n.tr("Password life in seconds") |
| 282 |
inputMethodHints: Qt.ImhDigitsOnly |
| 283 |
|
| 284 |
onAccepted: { |
| 285 |
focus = false; |
| 286 |
secretTF.focus = true; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
Row { |
| 292 |
width: parent.width |
| 293 |
spacing: units.gu(2) |
| 294 |
visible: !isTOTP |
| 295 |
|
| 296 |
Text { |
| 297 |
anchors.verticalCenter: parent.verticalCenter |
| 298 |
id: countTxt |
| 299 |
//visible: !keyPeriod.visible |
| 300 |
text: "<b>" + i18n.tr("Counter") + "</b>" |
| 301 |
color: theme.palette.normal.overlayText |
| 302 |
} |
| 303 |
|
| 304 |
TextField { |
| 305 |
width: parent.width - units.gu(2) - countTxt.width |
| 306 |
id: countTF |
| 307 |
placeholderText: i18n.tr("Password initial generation") |
| 308 |
inputMethodHints: Qt.ImhDigitsOnly |
| 309 |
|
| 310 |
onAccepted: { |
| 311 |
focus = false; |
| 312 |
secretTF.focus = true; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
Text { |
| 318 |
text: "<b>" + i18n.tr("Secret Key") + "</b>" |
| 319 |
color: theme.palette.normal.overlayText |
| 320 |
} |
| 321 |
|
| 322 |
TextArea { |
| 323 |
id: secretTF |
| 324 |
width: parent.width |
| 325 |
placeholderText: i18n.tr("Secret key base") |
| 326 |
autoSize: true |
| 327 |
wrapMode: Text.WrapAnywhere |
| 328 |
inputMethodHints: Qt.ImhNoPredictiveText |
| 329 |
} |
| 330 |
|
| 331 |
function checkFields() { |
| 332 |
var isDescEmpty = userDescTF.text == ""; |
| 333 |
var isIssuerEmpty = issuerTF.text == ""; |
| 334 |
var isUserEmpty = userTF.text == "" |
| 335 |
var isIntEmpty = intTF.text == ""; |
| 336 |
var isCountTFEmpty = countTF.text == ""; |
| 337 |
var isSecretTFEmpty = secretTF.text == ""; |
| 338 |
|
| 339 |
console.log(isDescEmpty, isIssuerEmpty, isIntEmpty, isCountTFEmpty, isSecretTFEmpty, algTF.algorithm) |
| 340 |
|
| 341 |
if (isTOTP) { |
| 342 |
var returnKey = { |
| 343 |
issuer: issuerTF.text, |
| 344 |
label: userTF.text, |
| 345 |
algorithm: algTF.algorithm, |
| 346 |
digits: lengTF.passwordLength, |
| 347 |
period: intTF.text, |
| 348 |
secret: OTPAuth.Secret.fromB32(secretTF.text) // Specially for old lib OTPAuth.Secret.fromB32() |
| 349 |
} |
| 350 |
} else { |
| 351 |
var returnKey = { |
| 352 |
issuer: issuerTF.text, |
| 353 |
label: userTF.text, |
| 354 |
algorithm: algTF.algorithm, |
| 355 |
digits: lengTF.passwordLength, |
| 356 |
counter: countTF.text, |
| 357 |
secret: OTPAuth.Secret.fromB32(secretTF.text) // Specially for old lib OTPAuth.Secret.fromB32() |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
return returnKey; |
| 362 |
} |
| 363 |
|
| 364 |
function fillInfo(uri){ |
| 365 |
console.log("Checking pasted URI") |
| 366 |
uri = root.checkUri(uri) |
| 367 |
|
| 368 |
try { |
| 369 |
uriJson = OTPAuth.URI.parse(uri); |
| 370 |
} catch(e) { |
| 371 |
console.log("Error",e); |
| 372 |
errorMsg.text = i18n.tr("URI format is invalid. The expected format is 'otpauth://totp/[ID]?secret=[CODE]`") |
| 373 |
errorMsg.visible = true |
| 374 |
hideErrorMsg.start() |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
Timer { |
| 379 |
//General ticker. It ticks every second |
| 380 |
id: hideErrorMsg |
| 381 |
running: false |
| 382 |
repeat: false |
| 383 |
interval: 6000 |
| 384 |
|
| 385 |
onTriggered: errorMsg.visible = false; |
| 386 |
} |
| 387 |
} |