Juice/db.py

194 lines
3.8 KiB
Python
Raw Normal View History

2019-06-16 18:22:47 -04:00
#!/usr/bin/env python3
"""
A module for interacting with Juice's database.
"""
2019-06-22 18:37:16 -04:00
import time
2019-06-16 18:22:47 -04:00
import sqlite3
import threading
DB_LOCK = threading.Lock()
def init_db():
"""
Checks to see if the database is initialized yet or not. If not,
the appropriate tables are created.
"""
con = sqlite3.connect('juice.db')
cur = con.cursor()
try:
cur.execute("SELECT * FROM user LIMIT 1").fetchone()
cur.execute("SELECT * FROM credential LIMIT 1").fetchone()
2019-06-22 18:37:16 -04:00
cur.execute("SELECT * FROM token LIMIT 1").fetchone()
2019-06-16 18:22:47 -04:00
except sqlite3.OperationalError:
cur.execute("CREATE TABLE user("
2019-06-22 18:37:16 -04:00
"id INTEGER PRIMARY KEY, "
"username TEXT UNIQUE, "
"email TEXT UNIQUE"
2019-06-16 18:22:47 -04:00
")"
)
cur.execute("CREATE TABLE credential("
2019-06-22 18:37:16 -04:00
"id INTEGER PRIMARY KEY, "
2019-06-16 18:22:47 -04:00
"user_id INTEGER REFERENCES user(id) ON UPDATE CASCADE, "
2019-06-22 18:37:16 -04:00
"nick TEXT, "
2019-06-16 18:22:47 -04:00
"credential BLOB"
")"
)
2019-06-22 18:37:16 -04:00
cur.execute("CREATE TABLE token("
"id INTEGER PRIMARY KEY, "
"user_id INTEGER REFERENCES user(id) ON UPDATE CASCADE, "
2019-06-23 14:01:03 -04:00
"user_agent TEXT, "
"ip_address TEXT, "
2019-06-22 18:37:16 -04:00
"token_hash TEXT, "
"date_issued INTEGER, "
"date_expired INTEGER"
")"
)
2019-06-16 18:22:47 -04:00
con.commit()
con.close()
init_db()
def db_execute(*args, **kwargs):
"""
Opens a connection to the app's database and executes the SQL
statements passed to this function.
"""
with sqlite3.connect('juice.db') as con:
DB_LOCK.acquire()
2019-06-23 14:01:03 -04:00
con.row_factory = sqlite3.Row
2019-06-16 18:22:47 -04:00
cur = con.cursor()
res = cur.execute(*args, **kwargs)
DB_LOCK.release()
return res
2019-06-22 18:37:16 -04:00
def set_user(username, email):
2019-06-16 18:22:47 -04:00
"""
Adds a new user.
"""
2019-06-22 18:37:16 -04:00
user_id = db_execute(
"INSERT INTO user(username, email) VALUES (?, ?)",
(username, email)
).lastrowid
return user_id
2019-06-16 18:22:47 -04:00
def get_user(username):
"""
Returns a user entry.
"""
data = db_execute(
2019-06-22 18:37:16 -04:00
"SELECT * FROM user WHERE username = ?",
2019-06-16 18:22:47 -04:00
(username,)
).fetchone()
return data
2019-06-23 14:01:03 -04:00
def set_token(user_id, user_agent, ip_address, token_hash):
2019-06-16 18:22:47 -04:00
"""
2019-06-22 18:37:16 -04:00
Sets a user's token hash.
"""
date_issued = int(time.time())
date_expired = date_issued + 30*24*60*60
token_id = db_execute(
"INSERT INTO "
2019-06-23 14:01:03 -04:00
"token("
"user_id, user_agent, ip_address, token_hash, date_issued,date_expired"
") "
"VALUES (?,?,?,?,?,?)",
(user_id, user_agent, ip_address, token_hash, date_issued,date_expired)
2019-06-22 18:37:16 -04:00
).lastrowid
return token_id
2019-06-23 14:01:03 -04:00
def get_token(token_id):
"""
Returns the token of the specified id.
"""
data = db_execute(
"SELECT * FROM token WHERE id = ?",
(token_id,)
).fetchone()
return data
def delete_token(token_id):
"""
Deletes the token of the specified id.
"""
db_execute(
"DELETE FROM token WHERE id = ?",
(token_id,)
).fetchone()
return True
2019-06-22 18:37:16 -04:00
def get_tokens(user_id):
"""
Returns all tokens assigned to a user.
"""
data = db_execute(
"SELECT * FROM token WHERE user_id = ?",
(user_id,)
).fetchall()
return data
def refresh_token(token_id):
2019-06-16 18:22:47 -04:00
"""
2019-06-22 18:37:16 -04:00
Extends a token's expiration date.
"""
new_date_expired = int(time.time()) + 30*24*60*60
2019-06-16 18:22:47 -04:00
db_execute(
2019-06-22 18:37:16 -04:00
"UPDATE token SET date_expired = ? WHERE id = ?",
(new_date_expired, token_id)
2019-06-16 18:22:47 -04:00
)
return True
2019-06-22 18:37:16 -04:00
def set_credential(user_id, nick, credential):
"""
Adds a credential to the database.
"""
cred_id = db_execute(
"INSERT INTO credential(user_id, nick, credential) "
"VALUES (?, ?, ?)",
(user_id, nick, credential)
).lastrowid
return cred_id
2019-06-23 14:01:03 -04:00
def get_credential(cred_id):
"""
Returns the credential of the specified id.
"""
data = db_execute(
"SELECT * FROM credential WHERE id = ?",
(cred_id,)
).fetchone()
return data
def delete_credential(cred_id):
"""
Deletes the credential of the specified id.
"""
db_execute(
"DELETE FROM credential WHERE id = ?",
(cred_id,)
).fetchone()
return True
2019-06-16 18:22:47 -04:00
def get_credentials(user_id):
"""
Returns all credentials registered to a user.
"""
data = db_execute(
"SELECT * FROM credential WHERE user_id = ?",
(user_id,)
).fetchall()
return data