Spaces:
Sleeping
Sleeping
File size: 1,748 Bytes
d0e0a14 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import os
import uuid
from fastapi import Request
from sqladmin.authentication import AuthenticationBackend as AuthBackendAdmin
from fastapi_users import FastAPIUsers
from fastapi_users.authentication import CookieTransport, AuthenticationBackend
from fastapi_users.authentication import JWTStrategy
from fastapi_users.password import PasswordHelper
from project.users.manager import get_user_manager
from project.users.models import User
from sqlalchemy import select
cookie_transport = CookieTransport(
cookie_name="real-estate",
cookie_max_age=3600,
cookie_path="/",
cookie_domain=None,
cookie_secure=False,
cookie_httponly=False,
cookie_samesite="lax",
)
def get_jwt_strategy() -> JWTStrategy:
return JWTStrategy(secret=os.getenv('SECRET'), lifetime_seconds=3600)
auth_backend = AuthenticationBackend(
name='jwt',
transport=cookie_transport,
get_strategy=get_jwt_strategy
)
fastapi_users = FastAPIUsers[User, int](
get_user_manager,
[auth_backend]
)
class AdminAuth(AuthBackendAdmin):
async def login(self, request: Request) -> bool:
form = await request.form()
username, password = form["username"], form["password"]
if username == 'hectool24' and password == 'hectoolshopify2024@':
request.session.update({"session": str(uuid.uuid4())})
return True
return False
async def logout(self, request: Request) -> bool:
request.session.clear()
return True
async def authenticate(self, request: Request) -> bool:
token = request.session.get("session")
if not token:
return False
return True
authentication_backend_admin = AdminAuth(secret_key=os.getenv('SECRET'))
|