added get token

parent 4d5c5424
from uuid import UUID from uuid import UUID
from starlette import status from starlette import status
import fastapi as fa import fastapi as fa
from fastapi.security import OAuth2PasswordRequestForm
from api import schemas from api import schemas
from db.repository import ArticleRepository from db.repository import ArticleRepository
from exceptions import common as common_exc, http as http_exc from exceptions import common as common_exc, http as http_exc
from .validate import is_valid_user from .validate import is_valid_user
from ..services import auth_service
router = fa.APIRouter(prefix='/article', tags=['article']) router = fa.APIRouter(prefix='/article', tags=['article'])
auth_router = fa.APIRouter(prefix='/auth', tags=['Auth'])
repo = ArticleRepository() repo = ArticleRepository()
...@@ -65,3 +68,10 @@ async def delete_article(id: UUID): ...@@ -65,3 +68,10 @@ async def delete_article(id: UUID):
except common_exc.NotFoundExcepton as e: except common_exc.NotFoundExcepton as e:
raise http_exc.HTTPNotFoundException(detail=str(e)) raise http_exc.HTTPNotFoundException(detail=str(e))
@auth_router.post('/login')
async def login(
form_data: OAuth2PasswordRequestForm = fa.Depends(),
):
return await auth_service.login(form_data)
...@@ -3,13 +3,14 @@ import fastapi as fa ...@@ -3,13 +3,14 @@ import fastapi as fa
from starlette import status from starlette import status
from api.article.routes import router as article_router from api.article.routes import router as article_router, auth_router as auth_route
from adapters import MinIOClient from adapters import MinIOClient
import settings import settings
router = fa.APIRouter(prefix='/api') router = fa.APIRouter(prefix='/api')
router.include_router(article_router) router.include_router(article_router)
router.include_router(auth_route)
@router.get('/greetings') @router.get('/greetings')
def get_greetings(): def get_greetings():
......
from uuid import UUID from uuid import UUID
from dataclasses import dataclass
import pydantic import pydantic
...@@ -18,3 +19,9 @@ class ArticleGetSchema(pydantic.BaseModel): ...@@ -18,3 +19,9 @@ class ArticleGetSchema(pydantic.BaseModel):
class ArticleUpdateSchema(pydantic.BaseModel): class ArticleUpdateSchema(pydantic.BaseModel):
name: str | None = pydantic.Field(None) name: str | None = pydantic.Field(None)
description: str | None = pydantic.Field(None) description: str | None = pydantic.Field(None)
@dataclass
class LoginRequestModel:
username: str
password: str
import httpx
from .schemas import LoginRequestModel
from exceptions import http as http_exc
class AuthService:
auth_service_url = "http://auth-core-1:8000/api/auth/login"
async def login(self, data: LoginRequestModel):
async with httpx.AsyncClient() as client:
response = await client.post(
self.auth_service_url,
data={"username": data.username, "password": data.password},
)
if response.status_code == 200:
return response.json()
else:
raise http_exc.HTTPNotFoundException(detail='Incorrect username or password')
auth_service = AuthService()
...@@ -17,6 +17,8 @@ ecdsa==0.18.0 ...@@ -17,6 +17,8 @@ ecdsa==0.18.0
exceptiongroup==1.2.0 exceptiongroup==1.2.0
fastapi==0.106.0 fastapi==0.106.0
h11==0.14.0 h11==0.14.0
httpcore==1.0.2
httpx==0.26.0
idna==3.6 idna==3.6
iso8601==1.1.0 iso8601==1.1.0
minio==7.2.0 minio==7.2.0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment