2023-06-15 15:43:40 +03:00
|
|
|
from secrets import token_hex
|
2023-06-14 22:21:13 +03:00
|
|
|
from typing import List
|
2023-08-25 13:03:13 +03:00
|
|
|
from uuid import uuid4
|
2023-06-15 15:43:40 +03:00
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends
|
2023-06-14 22:21:13 +03:00
|
|
|
from logger import get_logger
|
2023-11-14 16:31:02 +03:00
|
|
|
from middlewares.auth import AuthBearer, get_current_user
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.api_key.dto.outputs import ApiKeyInfo
|
|
|
|
from modules.api_key.entity.api_key import ApiKey
|
|
|
|
from modules.api_key.repository.api_keys import ApiKeys
|
2023-11-15 15:17:51 +03:00
|
|
|
from modules.user.entity.user_identity import UserIdentity
|
2023-06-14 22:21:13 +03:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
api_key_router = APIRouter()
|
|
|
|
|
2023-12-01 00:29:28 +03:00
|
|
|
api_keys_repository = ApiKeys()
|
|
|
|
|
2023-06-23 11:36:55 +03:00
|
|
|
|
|
|
|
@api_key_router.post(
|
|
|
|
"/api-key",
|
|
|
|
response_model=ApiKey,
|
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["API Key"],
|
|
|
|
)
|
2023-08-21 15:05:13 +03:00
|
|
|
async def create_api_key(current_user: UserIdentity = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Create new API key for the current user.
|
|
|
|
|
|
|
|
- `current_user`: The current authenticated user.
|
|
|
|
- Returns the newly created API key.
|
|
|
|
|
|
|
|
This endpoint generates a new API key for the current user. The API key is stored in the database and associated with
|
|
|
|
the user. It returns the newly created API key.
|
|
|
|
"""
|
2023-06-14 22:21:13 +03:00
|
|
|
|
2023-07-02 00:30:14 +03:00
|
|
|
new_key_id = uuid4()
|
2023-06-14 22:21:13 +03:00
|
|
|
new_api_key = token_hex(16)
|
|
|
|
|
2023-12-04 01:32:37 +03:00
|
|
|
try:
|
|
|
|
# Attempt to insert new API key into database
|
|
|
|
response = api_keys_repository.create_api_key(
|
|
|
|
new_key_id, new_api_key, current_user.id, "api_key", 30, False
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error creating new API key: {e}")
|
|
|
|
return {"api_key": "Error creating new API key."}
|
2023-06-14 22:21:13 +03:00
|
|
|
logger.info(f"Created new API key for user {current_user.email}.")
|
|
|
|
|
2023-12-04 01:32:37 +03:00
|
|
|
return response # type: ignore
|
2023-06-14 22:21:13 +03:00
|
|
|
|
2023-06-23 11:36:55 +03:00
|
|
|
|
|
|
|
@api_key_router.delete(
|
|
|
|
"/api-key/{key_id}", dependencies=[Depends(AuthBearer())], tags=["API Key"]
|
|
|
|
)
|
2023-08-21 15:05:13 +03:00
|
|
|
async def delete_api_key(
|
|
|
|
key_id: str, current_user: UserIdentity = Depends(get_current_user)
|
|
|
|
):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Delete (deactivate) an API key for the current user.
|
|
|
|
|
|
|
|
- `key_id`: The ID of the API key to delete.
|
|
|
|
|
|
|
|
This endpoint deactivates and deletes the specified API key associated with the current user. The API key is marked
|
|
|
|
as inactive in the database.
|
|
|
|
|
|
|
|
"""
|
2023-12-01 00:29:28 +03:00
|
|
|
api_keys_repository.delete_api_key(key_id, current_user.id)
|
2023-06-14 22:21:13 +03:00
|
|
|
|
|
|
|
return {"message": "API key deleted."}
|
|
|
|
|
2023-06-23 11:36:55 +03:00
|
|
|
|
|
|
|
@api_key_router.get(
|
|
|
|
"/api-keys",
|
|
|
|
response_model=List[ApiKeyInfo],
|
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["API Key"],
|
|
|
|
)
|
2023-08-21 15:05:13 +03:00
|
|
|
async def get_api_keys(current_user: UserIdentity = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Get all active API keys for the current user.
|
|
|
|
|
|
|
|
- `current_user`: The current authenticated user.
|
|
|
|
- Returns a list of active API keys with their IDs and creation times.
|
|
|
|
|
|
|
|
This endpoint retrieves all the active API keys associated with the current user. It returns a list of API key objects
|
|
|
|
containing the key ID and creation time for each API key.
|
|
|
|
"""
|
2023-12-01 00:29:28 +03:00
|
|
|
response = api_keys_repository.get_user_api_keys(current_user.id)
|
2023-06-23 11:36:55 +03:00
|
|
|
return response.data
|