Generate and delete API tokens

This commit is contained in:
Simon
2018-06-09 12:41:28 -04:00
parent de717d3992
commit a25976d24a
5 changed files with 131 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ import datetime
import json
import os
import bcrypt
import uuid
class InvalidQueryException(Exception):
@@ -29,6 +30,13 @@ class File:
self.website_id = website_id
class ApiToken:
def __init__(self, token, description):
self.token = token
self.description = description
class Database:
SORT_ORDERS = {
@@ -391,6 +399,44 @@ class Database:
cursor.execute("INSERT INTO Admin (username, password) VALUES (?,?)", (username, hashed_pw))
conn.commit()
def check_api_token(self, token) -> bool:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT token FROM ApiToken WHERE token=?", (token, ))
return cursor.fetchone() is not None
def generate_api_token(self, description: str) -> str:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
token = str(uuid.uuid4())
cursor.execute("INSERT INTO ApiToken (token, description) VALUES (?, ?)", (token, description))
conn.commit()
return token
def get_tokens(self) -> list:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM ApiToken")
return [ApiToken(x[0], x[1]) for x in cursor.fetchall()]
def delete_token(self, token: str) -> None:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("DELETE FROM ApiToken WHERE token=?", (token, ))
conn.commit()