Skip to content

Commit

Permalink
Merge pull request #93 from QActf/feature/BCK-41_cookie_auth
Browse files Browse the repository at this point in the history
Добавлена авторизация по куки
  • Loading branch information
kokhlo authored Jul 29, 2024
2 parents 4cd5171 + 68a50d4 commit 7742384
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ jobs:
cd backend/
git pull
cd backend/infra
rm .env
touch .env
echo DATABASE_URL=${{ secrets.DATABASE_URL }} >> .env
echo EMAIL_FROM=${{ secrets.EMAIL_FROM }} >> .env
Expand Down
10 changes: 8 additions & 2 deletions app/api/endpoints/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@
)
from app.api_docs_responses.utils_docs import USER_VALUE
from app.core.db import get_async_session
from app.core.user import auth_backend, fastapi_users
from app.core.user import auth_backend_cookie, auth_backend_jwt, fastapi_users
from app.crud.user import user_crud
from app.schemas.user import UserCreate, UserRead, UserReadRegister, UserUpdate
from app.services.token_generator.tokens import token_generator

router = APIRouter()

router.include_router(
fastapi_users.get_auth_router(auth_backend),
fastapi_users.get_auth_router(auth_backend_jwt),
prefix='/auth/jwt',
tags=['auth'],
)

router.include_router(
fastapi_users.get_auth_router(auth_backend_cookie),
prefix='/auth/cookie',
tags=['auth'],
)

router.include_router(
register.get_register_router(
UserReadRegister,
Expand Down
14 changes: 11 additions & 3 deletions app/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
BaseUserManager, FastAPIUsers, IntegerIDMixin, InvalidPasswordException,
)
from fastapi_users.authentication import (
AuthenticationBackend, BearerTransport, JWTStrategy,
AuthenticationBackend, BearerTransport, CookieTransport, JWTStrategy,
)
from fastapi_users_db_sqlalchemy import SQLAlchemyUserDatabase
from sqlalchemy.ext.asyncio import AsyncSession
Expand All @@ -27,19 +27,27 @@ async def get_user_db(session: AsyncSession = Depends(get_async_session)):

bearer_transport = BearerTransport(tokenUrl='auth/jwt/login')

cookie_transport = CookieTransport()


def get_jwt_strategy() -> JWTStrategy:
return JWTStrategy(
secret=settings.secret, lifetime_seconds=settings.lifetime_seconds
)


auth_backend = AuthenticationBackend(
auth_backend_jwt = AuthenticationBackend(
name='jwt_auth',
transport=bearer_transport,
get_strategy=get_jwt_strategy,
)

auth_backend_cookie = AuthenticationBackend(
name='cookie_auth',
transport=cookie_transport,
get_strategy=get_jwt_strategy
)


class UserManager(IntegerIDMixin, BaseUserManager[User, int]):
async def validate_password(
Expand Down Expand Up @@ -76,7 +84,7 @@ async def get_user_manager(user_db=Depends(get_user_db)):

fastapi_users = FastAPIUsers[User, int](
get_user_manager,
[auth_backend],
[auth_backend_jwt, auth_backend_cookie],
)

current_user = fastapi_users.current_user(active=True)
Expand Down
4 changes: 2 additions & 2 deletions app/schemas/profile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import datetime
from typing import Optional
from typing_extensions import Annotated

from pydantic import (
BaseModel, Field, WithJsonSchema, field_serializer, field_validator
BaseModel, Field, WithJsonSchema, field_serializer, field_validator,
)
from sqlalchemy_utils import Choice
from typing_extensions import Annotated

from app.core.constants import Gender

Expand Down
2 changes: 1 addition & 1 deletion infra/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ services:

volumes:
db_volume_qactf:
backend_static:
backend_static:

0 comments on commit 7742384

Please sign in to comment.