Compare commits

..

No commits in common. "master" and "implementAddingNewKeycards" have entirely different histories.

5 changed files with 15 additions and 67 deletions

View File

@ -16,16 +16,8 @@
## Run dev server ## Run dev server
source venv/bin/activate source venv/bin/activate
python -m kdoorweb
Or
bottle.py --debug --reload kdoorweb:application bottle.py --debug --reload kdoorweb:application
To set the TCP port number add it as the last argument
python -m kdoorweb 8888
## Run unittests ## Run unittests
source venv/bin/activate source venv/bin/activate

View File

@ -1,33 +1,29 @@
import hashlib from bottle import Bottle, request, response
from functools import partial
from bottle import Bottle, request, response, HTTPError
api = Bottle() api = Bottle()
scrypt = partial(hashlib.scrypt, n=16384, r=8, p=1)
# FIXME: Fix door api auth
def check_api_auth(callback): def check_api_auth(callback):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
print("check api auth") print("check api auth")
user, api_key = request.auth or (None, None)
if "db" not in kwargs: if "db" not in kwargs:
return "Auth not possible, db not available" request.current_user = None
user = kwargs["db"].get_door_by_name_and_api_key(user) or {} return callback(*args, **kwargs)
stored_hash, salt = dict(user).get("api_key", ":").split(":") user = None
api_hash = scrypt(api_key, salt=salt) request.current_user = user
if user and api_hash == api_key: if user:
request.current_door = user print(f"logged in as {user['user']}")
print(f"logged in as {user['name']}") print(request.current_user)
print(user)
return callback(*args, **kwargs) return callback(*args, **kwargs)
else: else:
print("not logged in") print("not logged in")
return "Invalid authentication" return "Invalid authentication"
return wrapper return wrapper
# FIXME: db plugin not available yet
api.install(check_api_auth)
@api.route("/") @api.route("/")
def index(): def index():

View File

@ -51,9 +51,6 @@ class DB:
@staticmethod @staticmethod
def create_db(dbfile): def create_db(dbfile):
if type(dbfile) is DB:
db = dbfile.db
else:
db = sqlite3.connect(dbfile) db = sqlite3.connect(dbfile)
db.executescript(""" db.executescript("""
create table versions ( create table versions (
@ -83,7 +80,7 @@ class DB:
); );
create table doors ( create table doors (
id integer primary key, id integer primary key,
name text unique, name text,
note text, note text,
api_key text, api_key text,
created text, created text,
@ -180,24 +177,6 @@ class DB:
""") """)
return cur.fetchall() return cur.fetchall()
def add_door(self, name, note, api_key):
self.add_doors([name, note, api_key, ])
def add_doors(self, doors):
self.db.executemany("""
insert into doors(name, note, api_key, created, disabled)
values(?, ?, ?, ?, ?)
""", doors)
self.db.commit()
def get_door(self, door_id):
cur = self.db.execute("select id, name, note, api_key, created, disabled from doors where id = ?", (door_id,))
return cur.fetchone()
def get_door_by_name(self, name):
cur = self.db.execute("select id, name, note, api_key, created, disabled from doors where name = ?", (name,))
return cur.fetchone()
@staticmethod @staticmethod
def import_ad(json_file): def import_ad(json_file):
with open(json_file) as fp: with open(json_file) as fp:
@ -259,7 +238,7 @@ def initdb():
pass pass
DB.create_db(dbfile) DB.create_db(dbfile)
db = DB(dbfile) db = DB(dbfile)
db.add_users(db.import_ad("ad.json")) db.add_users(db.import_ad("../ad.json"))
users = db.list_users() users = db.list_users()
for user in users: for user in users:
print(dict(user)) print(dict(user))

View File

@ -5,7 +5,7 @@ from bottle import Bottle, view, TEMPLATE_PATH, static_file, \
request, redirect, response, HTTPError request, redirect, response, HTTPError
from .db import SQLitePlugin from .db import SQLitePlugin
from .api import api, check_api_auth from .api import api
application = app = Bottle() application = app = Bottle()
@ -56,7 +56,6 @@ app.install(db_plugin)
app.install(check_auth) app.install(check_auth)
api.install(db_plugin) api.install(db_plugin)
api.install(check_api_auth)
app.mount("/api/v1", api) app.mount("/api/v1", api)

View File

@ -1,18 +0,0 @@
import unittest
from kdoorweb.db import DB
class TestDB(unittest.TestCase):
def setUp(self) -> None:
self.db = DB()
def test_create_db_in_memory(self):
DB.create_db(dbfile=":memory:")
def test_create_db_in_connection(self):
DB.create_db(dbfile=self.db)
if __name__ == '__main__':
unittest.main()