1
0
Fork 0
mirror of https://github.com/seiichiro0185/sailotp.git synced 2024-11-14 21:16:42 +00:00

Merge branch 'feat-edit' into develop

This commit is contained in:
seiichiro 2014-01-08 19:19:14 +01:00
commit 67820a1e55
4 changed files with 48 additions and 13 deletions

View file

@ -75,15 +75,19 @@ function calcOTP(secret) {
// Get last full 30 / 60 Seconds and convert to HEX // Get last full 30 / 60 Seconds and convert to HEX
var time = leftpad(dec2hex(Math.floor(epoch / 30)), 16, '0'); var time = leftpad(dec2hex(Math.floor(epoch / 30)), 16, '0');
// Calculate the SHA-1 HMAC Value from time and key try {
var hmacObj = new SHA.jsSHA(time, 'HEX'); // Calculate the SHA-1 HMAC Value from time and key
var hmac = hmacObj.getHMAC(key, 'HEX', 'SHA-1', "HEX"); var hmacObj = new SHA.jsSHA(time, 'HEX');
var hmac = hmacObj.getHMAC(key, 'HEX', 'SHA-1', "HEX");
// Finally convert the HMAC-Value to the corresponding 6-digit token // Finally convert the HMAC-Value to the corresponding 6-digit token
var offset = hex2dec(hmac.substring(hmac.length - 1)); var offset = hex2dec(hmac.substring(hmac.length - 1));
var otp = (hex2dec(hmac.substr(offset * 2, 8)) & hex2dec('7fffffff')) + ''; var otp = (hex2dec(hmac.substr(offset * 2, 8)) & hex2dec('7fffffff')) + '';
otp = (otp).substr(otp.length - 6, 6); otp = (otp).substr(otp.length - 6, 6);
} catch (e) {
otp = "Invalid Secret!"
}
// return the calculated token // return the calculated token
return otp; return otp;

View file

@ -80,3 +80,14 @@ function removeOTP(title, secret) {
} }
) )
} }
// Change an existing OTP
function changeOTP(title, secret, oldtitle, oldsecret) {
var db = getDB();
db.transaction(
function(tx) {
tx.executeSql("UPDATE OTPStorage SET title=?, secret=? WHERE title=? and secret=?;", [title, secret, oldtitle, oldsecret]);
}
)
}

View file

@ -39,6 +39,10 @@ Dialog {
// We get the Object of the parent page on call to refresh it after adding a new Entry // We get the Object of the parent page on call to refresh it after adding a new Entry
property QtObject parentPage: null property QtObject parentPage: null
// If we want to edit a Key we get title and key from the calling page
property string paramLabel: ""
property string paramKey: ""
SilicaFlickable { SilicaFlickable {
id: addOtpList id: addOtpList
anchors.fill: parent anchors.fill: parent
@ -48,20 +52,22 @@ Dialog {
Column { Column {
anchors.fill: parent anchors.fill: parent
DialogHeader { DialogHeader {
acceptText: "Add" acceptText: paramLabel != "" ? "Save" : "Add"
} }
TextField { TextField {
id: otpLabel id: otpLabel
width: parent.width width: parent.width
label: "Title" label: "Title"
placeholderText: "Title for the OTP" placeholderText: "Title for the OTP"
text: paramLabel != "" ? paramLabel : ""
focus: true focus: true
horizontalAlignment: TextInput.AlignLeft horizontalAlignment: TextInput.AlignLeft
} }
TextField { TextField {
id: otpSecret id: otpSecret
width: parent.width width: parent.width
label: "Secret" label: "Secret (at least 16 characters)"
text: paramKey != "" ? paramKey : ""
placeholderText: "Secret OTP Key" placeholderText: "Secret OTP Key"
focus: true focus: true
horizontalAlignment: TextInput.AlignLeft horizontalAlignment: TextInput.AlignLeft
@ -69,12 +75,20 @@ Dialog {
} }
} }
// Save if page is Left with Add // Check if we can Save
canAccept: otpLabel.text.length > 0 && otpSecret.text.length >= 16 ? true : false
// Save if page is Left with Add
onDone: { onDone: {
// Some basic Input Check, we need both Values to work if (result == DialogResult.Accepted) {
if (otpLabel.text != "" && otpSecret.text != "") {
// Save the entry to the Config DB // Save the entry to the Config DB
DB.addOTP(otpLabel.text, otpSecret.text); if (paramLabel != "" && paramKey != "") {
// Parameters where filled -> Change existing entry
DB.changeOTP(otpLabel.text, otpSecret.text, paramLabel, paramKey)
} else {
// There were no parameters -> Add new entry
DB.addOTP(otpLabel.text, otpSecret.text);
}
// Refresh the main Page // Refresh the main Page
parentPage.refreshOTPList(); parentPage.refreshOTPList();
} }

View file

@ -158,6 +158,12 @@ Page {
Component { Component {
id: otpContextMenu id: otpContextMenu
ContextMenu { ContextMenu {
MenuItem {
text: "Edit"
onClicked: {
pageStack.push(Qt.resolvedUrl("AddOTP.qml"), {parentPage: mainPage, paramLabel: title, paramKey: secret})
}
}
MenuItem { MenuItem {
text: "Delete" text: "Delete"
onClicked: remove() onClicked: remove()