-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·59 lines (50 loc) · 1.75 KB
/
server.py
File metadata and controls
executable file
·59 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from dotenv import load_dotenv
import os
import hashlib
import sqlite3
app = FastAPI()
class Code(BaseModel):
code: str
def initialize_database():
conn = sqlite3.connect('opener.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS opener (id INTEGER PRIMARY KEY, counter INTEGER)")
cursor.execute("SELECT counter FROM opener WHERE id = 1")
result = cursor.fetchone()
if result is None:
cursor.execute("INSERT INTO opener (id, counter) VALUES (1, 0)")
conn.commit()
conn.close()
def get_current_counter():
conn = sqlite3.connect('opener.db')
cursor = conn.cursor()
cursor.execute("SELECT counter FROM opener WHERE id = 1")
result = cursor.fetchone()
conn.close()
return result[0]
def update_counter(new_counter):
conn = sqlite3.connect('opener.db')
cursor = conn.cursor()
cursor.execute("UPDATE opener SET counter = ? WHERE id = 1", (new_counter,))
conn.commit()
conn.close()
@app.on_event("startup")
async def startup_event():
initialize_database()
@app.post("/auth")
def open_door(code_data: Code):
seed = os.getenv('SECRET_KEY')
tolerance = 5
current_counter = get_current_counter()
for i in range(tolerance):
expected_code = hashlib.sha256(f"{seed}{current_counter + i + 1}".encode()).hexdigest()
if code_data.code == expected_code:
update_counter(current_counter + i + 1)
return {"status": "Authenticated"}
raise HTTPException(status_code=400, detail="Invalid code")
if __name__ == "__main__":
load_dotenv()
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)