latosa/auth.py

31 lines
909 B
Python
Raw Normal View History

2024-03-02 23:59:31 +01:00
import json
from cryptography.fernet import Fernet
2024-03-03 11:28:05 +01:00
from db import DB
2024-03-02 23:59:31 +01:00
class Auth:
2024-03-03 11:28:05 +01:00
def __init__(self):
self.db = DB()
2024-03-02 23:59:31 +01:00
def get_key(self, client_id):
2024-03-03 11:28:05 +01:00
bind_params = {'client_id': client_id}
return self.db.execute(
'SELECT client_secret FROM auth WHERE client_id = :client_id',
bind_params)[0]
def set_key(self, client_id, client_secret):
bind_params = {'client_id': client_id, 'client_secret': client_secret}
self.db.execute(
'INSERT INTO auth (client_id, client_secret) VALUES (:client_id, :client_secret)',
bind_params)
2024-03-02 23:59:31 +01:00
def decrypt(self, cyphertext, client_id) -> dict:
return json.loads(
Fernet(self.get_key(client_id)).decrypt(cyphertext).decode())
2024-03-03 11:28:05 +01:00
def encrypt(self, session_key, data) -> bytes:
return Fernet(session_key).encrypt(json.dumps(data).encode('utf-8'))