store session-key in database (once generated on db creation), #74
Store a random session key used to sign and verify comment ownership once the database is initialized, not on every application startup. Currently fixed session keys in [general] session-key are migrated into the database on startup. The configuration parser will notice you about the change and suggest you to remove this option.pull/80/merge
parent
8f293ad435
commit
0b816a0677
@ -0,0 +1,36 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
import os
|
||||||
|
import binascii
|
||||||
|
|
||||||
|
|
||||||
|
class Preferences:
|
||||||
|
|
||||||
|
defaults = [
|
||||||
|
("session-key", binascii.b2a_hex(os.urandom(24))),
|
||||||
|
]
|
||||||
|
|
||||||
|
def __init__(self, db):
|
||||||
|
|
||||||
|
self.db = db
|
||||||
|
self.db.execute([
|
||||||
|
'CREATE TABLE IF NOT EXISTS preferences (',
|
||||||
|
' key VARCHAR PRIMARY KEY, value VARCHAR',
|
||||||
|
');'])
|
||||||
|
|
||||||
|
for (key, value) in Preferences.defaults:
|
||||||
|
if self.get(key) is None:
|
||||||
|
self.set(key, value)
|
||||||
|
|
||||||
|
def get(self, key, default=None):
|
||||||
|
rv = self.db.execute(
|
||||||
|
'SELECT value FROM preferences WHERE key=?', (key, )).fetchone()
|
||||||
|
|
||||||
|
if rv is None:
|
||||||
|
return default
|
||||||
|
|
||||||
|
return rv[0]
|
||||||
|
|
||||||
|
def set(self, key, value):
|
||||||
|
self.db.execute(
|
||||||
|
'INSERT INTO preferences (key, value) VALUES (?, ?)', (key, value))
|
Loading…
Reference in new issue