encryption timeout is now configurable in the db

This commit is contained in:
azivner 2017-09-12 23:04:17 -04:00
parent 3e97cdf085
commit 5b84487aac
7 changed files with 79 additions and 5 deletions

View file

@ -11,6 +11,7 @@ from sql import connect, getOption
from tree_api import tree_api
from notes_move_api import notes_move_api
from password_api import password_api
from settings_api import settings_api
import config_provider
import my_scrypt
@ -31,6 +32,7 @@ app.register_blueprint(tree_api)
app.register_blueprint(notes_api)
app.register_blueprint(notes_move_api)
app.register_blueprint(password_api)
app.register_blueprint(settings_api)
class User(UserMixin):
pass

33
src/settings_api.py Normal file
View file

@ -0,0 +1,33 @@
from flask import Blueprint, jsonify, request
from flask_login import login_required
import sql
settings_api = Blueprint('settings_api', __name__)
allowed_options = [ 'encryption_session_timeout' ]
@settings_api.route('/settings', methods = ['GET'])
@login_required
def get_settings():
dict = {}
settings = sql.getResults("SELECT opt_name, opt_value FROM options WHERE opt_name IN (%s)" % ',' . join('?'*len(allowed_options)), allowed_options)
for set in settings:
dict[set['opt_name']] = set['opt_value']
return jsonify(dict)
@settings_api.route('/settings', methods = ['POST'])
@login_required
def set_settings():
req = request.get_json(force=True)
if req['name'] in allowed_options:
sql.setOption(req['name'], req['value'])
sql.commit()
return jsonify({})
else:
return jsonify("not allowed option to set")

View file

@ -152,7 +152,17 @@
</form>
</div>
<div id="encryptionTimeout">
<p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
<p>Encryption timeout is a time period after which the encryption key and encrypted data is wiped out from
browser's memory. This is measured from the last encryption / decryption activity.</p>
<form id="encryptionTimeoutForm">
<div class="form-group">
<label for="encryptionTimeoutInSeconds">Encryption timeout (in seconds)</label>
<input class="form-control" id="encryptionTimeoutInSeconds" type="number">
</div>
<button class="btn btn-sm">Save</button>
</form>
</div>
</div>
</div>

View file

@ -42,5 +42,6 @@ def getTree():
retObject['start_note_id'] = getSingleResult('select * from options where opt_name = "start_node"')['opt_value'];
retObject['verification_salt'] = getOption('verification_salt')
retObject['encryption_salt'] = getOption('encryption_salt')
retObject['encryption_session_timeout'] = getOption('encryption_session_timeout')
return jsonify(retObject)

View file

@ -24,9 +24,6 @@ function handleEncryption(requireEncryption, modal, callback) {
}
}
// currently not configurable
const globalEncryptionKeyTimeToLive = 10 * 60 * 1000; // in milliseconds
let globalEncryptionKey = null;
let globalLastEncryptionOperationDate = null;
@ -119,7 +116,7 @@ $("#encryptionPasswordForm").submit(function() {
});
setInterval(function() {
if (globalLastEncryptionOperationDate !== null && new Date().getTime() - globalLastEncryptionOperationDate.getTime() > globalEncryptionKeyTimeToLive) {
if (globalLastEncryptionOperationDate !== null && new Date().getTime() - globalLastEncryptionOperationDate.getTime() > globalEncryptionSessionTimeout * 1000) {
globalEncryptionKey = null;
if (globalCurrentNote.detail.encryption > 0) {

View file

@ -1,4 +1,13 @@
function displaySettings() {
$.ajax({
url: baseUrl + 'settings',
type: 'GET',
success: function (result) {
$("#encryptionTimeoutInSeconds").val(result['encryption_session_timeout']);
},
error: () => alert("Error getting settings.")
});
$("#settingsDialog").dialog({
modal: true,
width: 600
@ -42,3 +51,23 @@ $("#changePasswordForm").submit(() => {
return false;
});
$("#encryptionTimeoutForm").submit(() => {
const encryptionTimeout = $("#encryptionTimeoutInSeconds").val();
$.ajax({
url: baseUrl + 'settings',
type: 'POST',
data: JSON.stringify({
name: 'encryption_session_timeout',
value: encryptionTimeout
}),
contentType: "application/json",
success: function () {
alert("Encryption timeout has been changed.");
},
error: () => alert("Error occurred during changing encryption timeout.")
});
return false;
});

View file

@ -85,6 +85,7 @@ function setExpandedToServer(note_id, is_expanded) {
let globalVerificationSalt;
let globalEncryptionSalt;
let globalEncryptionSessionTimeout;
$(function(){
$.get(baseUrl + 'tree').then(resp => {
@ -92,6 +93,7 @@ $(function(){
let startNoteId = resp.start_note_id;
globalVerificationSalt = resp.verification_salt;
globalEncryptionSalt = resp.encryption_salt;
globalEncryptionSessionTimeout = resp.encryption_session_timeout;
if (document.location.hash) {
startNoteId = document.location.hash.substr(1); // strip initial #