2023-08-02 00:03:47 +03:00
|
|
|
from datetime import datetime
|
2023-08-21 15:05:13 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from models.databases.repository import Repository
|
2023-08-02 00:03:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ApiKeyHandler(Repository):
|
|
|
|
def __init__(self, supabase_client):
|
2023-09-14 18:42:15 +03:00
|
|
|
self.db = supabase_client # type: ignore
|
2023-08-02 00:03:47 +03:00
|
|
|
|
|
|
|
def create_api_key(self, new_key_id, new_api_key, user_id):
|
|
|
|
response = (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.insert(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"key_id": str(new_key_id),
|
|
|
|
"user_id": str(user_id),
|
|
|
|
"api_key": str(new_api_key),
|
|
|
|
"creation_time": datetime.utcnow().strftime(
|
|
|
|
"%Y-%m-%d %H:%M:%S"
|
|
|
|
),
|
|
|
|
"is_active": True,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2023-08-21 15:05:13 +03:00
|
|
|
def delete_api_key(self, key_id: str, user_id: UUID):
|
2023-08-02 00:03:47 +03:00
|
|
|
return (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.update(
|
|
|
|
{
|
|
|
|
"is_active": False,
|
|
|
|
"deleted_time": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.match({"key_id": key_id, "user_id": user_id})
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
|
2023-08-21 15:05:13 +03:00
|
|
|
def get_active_api_key(self, api_key: str):
|
2023-08-02 00:03:47 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.select("api_key", "creation_time")
|
|
|
|
.filter("api_key", "eq", api_key)
|
|
|
|
.filter("is_active", "eq", True)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2023-08-21 15:05:13 +03:00
|
|
|
def get_user_id_by_api_key(self, api_key: str):
|
2023-08-02 00:03:47 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.select("user_id")
|
|
|
|
.filter("api_key", "eq", api_key)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2023-08-21 15:05:13 +03:00
|
|
|
def get_user_api_keys(self, user_id: UUID):
|
2023-08-02 00:03:47 +03:00
|
|
|
response = (
|
|
|
|
self.db.table("api_keys")
|
|
|
|
.select("key_id, creation_time")
|
|
|
|
.filter("user_id", "eq", user_id)
|
|
|
|
.filter("is_active", "eq", True)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response.data
|