mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Add API_KEY checking
This commit is contained in:
9
fastapi_book_server/app/depends.py
Normal file
9
fastapi_book_server/app/depends.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from fastapi import Security, HTTPException, status
|
||||||
|
|
||||||
|
from core.auth import default_security
|
||||||
|
from core.config import env_config
|
||||||
|
|
||||||
|
|
||||||
|
async def check_token(api_key: str = Security(default_security)):
|
||||||
|
if api_key != env_config.API_KEY:
|
||||||
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Wrong api key!")
|
||||||
@@ -8,11 +8,13 @@ from app.models import Author as AuthorDB, AuthorAnnotation as AuthorAnnotationD
|
|||||||
from app.serializers.author import Author, CreateAuthor, UpdateAuthor, AuthorBook, TranslatedBook
|
from app.serializers.author import Author, CreateAuthor, UpdateAuthor, AuthorBook, TranslatedBook
|
||||||
from app.serializers.author_annotation import AuthorAnnotation
|
from app.serializers.author_annotation import AuthorAnnotation
|
||||||
from app.services.author import AuthorTGRMSearchService
|
from app.services.author import AuthorTGRMSearchService
|
||||||
|
from app.depends import check_token
|
||||||
|
|
||||||
|
|
||||||
author_router = APIRouter(
|
author_router = APIRouter(
|
||||||
prefix="/api/v1/authors",
|
prefix="/api/v1/authors",
|
||||||
tags=["author"],
|
tags=["author"],
|
||||||
|
dependencies=[Depends(check_token)],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -23,7 +25,7 @@ async def get_authors():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@author_router.post("/", response_model=Author)
|
@author_router.post("/", response_model=Author, dependencies=[Depends(Params)])
|
||||||
async def create_author(data: CreateAuthor):
|
async def create_author(data: CreateAuthor):
|
||||||
author = await AuthorDB.objects.create(
|
author = await AuthorDB.objects.create(
|
||||||
**data.dict()
|
**data.dict()
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ from fastapi_pagination.ext.ormar import paginate
|
|||||||
|
|
||||||
from app.models import AuthorAnnotation as AuthorAnnotationDB
|
from app.models import AuthorAnnotation as AuthorAnnotationDB
|
||||||
from app.serializers.author_annotation import AuthorAnnotation, CreateAuthorAnnotation, UpdateAuthorAnnotation
|
from app.serializers.author_annotation import AuthorAnnotation, CreateAuthorAnnotation, UpdateAuthorAnnotation
|
||||||
|
from app.depends import check_token
|
||||||
|
|
||||||
|
|
||||||
author_annotation_router = APIRouter(
|
author_annotation_router = APIRouter(
|
||||||
prefix="/api/v1/author_annotations",
|
prefix="/api/v1/author_annotations",
|
||||||
tags=["author_annotation"]
|
tags=["author_annotation"],
|
||||||
|
dependencies=[Depends(check_token)],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,13 @@ from app.utils.pagination import CustomPage
|
|||||||
from app.models import Book as BookDB, Author as AuthorDB, AuthorAnnotation as AuthorAnnotationDB
|
from app.models import Book as BookDB, Author as AuthorDB, AuthorAnnotation as AuthorAnnotationDB
|
||||||
from app.serializers.book import Book, CreateBook, UpdateBook, CreateRemoteBook
|
from app.serializers.book import Book, CreateBook, UpdateBook, CreateRemoteBook
|
||||||
from app.services.book import BookTGRMSearchService, BookCreator
|
from app.services.book import BookTGRMSearchService, BookCreator
|
||||||
|
from app.depends import check_token
|
||||||
|
|
||||||
|
|
||||||
book_router = APIRouter(
|
book_router = APIRouter(
|
||||||
prefix="/api/v1/books",
|
prefix="/api/v1/books",
|
||||||
tags=["book"],
|
tags=["book"],
|
||||||
|
dependencies=[Depends(check_token)],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ from fastapi_pagination.ext.ormar import paginate
|
|||||||
|
|
||||||
from app.models import BookAnnotation as BookAnnotationDB
|
from app.models import BookAnnotation as BookAnnotationDB
|
||||||
from app.serializers.book_annotation import BookAnnotation, CreateBookAnnotation, UpdateBookAnnotation
|
from app.serializers.book_annotation import BookAnnotation, CreateBookAnnotation, UpdateBookAnnotation
|
||||||
|
from app.depends import check_token
|
||||||
|
|
||||||
|
|
||||||
book_annotation_router = APIRouter(
|
book_annotation_router = APIRouter(
|
||||||
prefix="/api/v1/book_annotations",
|
prefix="/api/v1/book_annotations",
|
||||||
tags=["book_annotation"]
|
tags=["book_annotation"],
|
||||||
|
dependencies=[Depends(check_token)]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,11 +7,13 @@ from app.utils.pagination import CustomPage
|
|||||||
from app.models import Sequence as SequenceDB
|
from app.models import Sequence as SequenceDB
|
||||||
from app.serializers.sequence import Sequence, CreateSequence
|
from app.serializers.sequence import Sequence, CreateSequence
|
||||||
from app.services.sequence import SequenceTGRMSearchService
|
from app.services.sequence import SequenceTGRMSearchService
|
||||||
|
from app.depends import check_token
|
||||||
|
|
||||||
|
|
||||||
sequence_router = APIRouter(
|
sequence_router = APIRouter(
|
||||||
prefix="/api/v1/sequences",
|
prefix="/api/v1/sequences",
|
||||||
tags=["sequence"]
|
tags=["sequence"],
|
||||||
|
dependencies=[Depends(check_token)],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,13 @@ from app.utils.pagination import CustomPage
|
|||||||
from app.models import SequenceInfo as SequenceInfoDB
|
from app.models import SequenceInfo as SequenceInfoDB
|
||||||
from app.serializers.sequence_info import SequenceInfo, CreateSequenceInfo, CreateRemoteSequenceInfo
|
from app.serializers.sequence_info import SequenceInfo, CreateSequenceInfo, CreateRemoteSequenceInfo
|
||||||
from app.services.sequence_info import SequenceInfoCreator
|
from app.services.sequence_info import SequenceInfoCreator
|
||||||
|
from app.depends import check_token
|
||||||
|
|
||||||
|
|
||||||
sequence_info_router = APIRouter(
|
sequence_info_router = APIRouter(
|
||||||
prefix="/api/v1/sequence_info",
|
prefix="/api/v1/sequence_info",
|
||||||
tags=["sequence_info"]
|
tags=["sequence_info"],
|
||||||
|
dependencies=[Depends(check_token)],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ from fastapi_pagination.ext.ormar import paginate
|
|||||||
|
|
||||||
from app.models import Source as SourceDB
|
from app.models import Source as SourceDB
|
||||||
from app.serializers.source import Source, CreateSource
|
from app.serializers.source import Source, CreateSource
|
||||||
|
from app.depends import check_token
|
||||||
|
|
||||||
|
|
||||||
source_router = APIRouter(
|
source_router = APIRouter(
|
||||||
prefix="/api/v1/sources",
|
prefix="/api/v1/sources",
|
||||||
tags=["source"],
|
tags=["source"],
|
||||||
|
dependencies=[Depends(check_token)],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,13 @@ from app.utils.pagination import CustomPage
|
|||||||
from app.models import Translation as TranslationDB
|
from app.models import Translation as TranslationDB
|
||||||
from app.serializers.translation import Translation, CreateTranslation, CreateRemoteTranslation
|
from app.serializers.translation import Translation, CreateTranslation, CreateRemoteTranslation
|
||||||
from app.services.translation import TranslationCreator
|
from app.services.translation import TranslationCreator
|
||||||
|
from app.depends import check_token
|
||||||
|
|
||||||
|
|
||||||
translation_router = APIRouter(
|
translation_router = APIRouter(
|
||||||
prefix="/api/v1/translation",
|
prefix="/api/v1/translation",
|
||||||
tags=["translation"]
|
tags=["translation"],
|
||||||
|
dependencies=[Depends(check_token)],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
4
fastapi_book_server/core/auth.py
Normal file
4
fastapi_book_server/core/auth.py
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
from fastapi.security import APIKeyHeader
|
||||||
|
|
||||||
|
|
||||||
|
default_security = APIKeyHeader(name="Authorization")
|
||||||
@@ -2,6 +2,8 @@ from pydantic import BaseSettings
|
|||||||
|
|
||||||
|
|
||||||
class EnvConfig(BaseSettings):
|
class EnvConfig(BaseSettings):
|
||||||
|
API_KEY: str
|
||||||
|
|
||||||
POSTGRES_USER: str
|
POSTGRES_USER: str
|
||||||
POSTGRES_PASSWORD: str
|
POSTGRES_PASSWORD: str
|
||||||
POSTGRES_HOST: str
|
POSTGRES_HOST: str
|
||||||
|
|||||||
Reference in New Issue
Block a user