✨ Update all for Postgres and new techniques
This commit is contained in:
@@ -21,7 +21,7 @@ SERVER_NAME = os.getenv("SERVER_NAME")
|
||||
SERVER_HOST = os.getenv("SERVER_HOST")
|
||||
BACKEND_CORS_ORIGINS = os.getenv(
|
||||
"BACKEND_CORS_ORIGINS"
|
||||
) # a string of origins separated by commas, e.g: "http://localhost, http://localhost:4200, http://localhost:3000, http://localhost:8080, http://dev.couchbase-project.com, https://stag.couchbase-project.com, https://couchbase-project.com, http://local.dockertoolbox.tiangolo.com"
|
||||
) # a string of origins separated by commas, e.g: "http://localhost, http://localhost:4200, http://localhost:3000, http://localhost:8080, http://local.dockertoolbox.tiangolo.com"
|
||||
PROJECT_NAME = os.getenv("PROJECT_NAME")
|
||||
SENTRY_DSN = os.getenv("SENTRY_DSN")
|
||||
|
||||
@@ -47,8 +47,6 @@ EMAIL_RESET_TOKEN_EXPIRE_HOURS = 48
|
||||
EMAIL_TEMPLATES_DIR = "/app/app/email-templates/build"
|
||||
EMAILS_ENABLED = SMTP_HOST and SMTP_PORT and EMAILS_FROM_EMAIL
|
||||
|
||||
ROLE_SUPERUSER = "superuser"
|
||||
|
||||
FIRST_SUPERUSER = os.getenv("FIRST_SUPERUSER")
|
||||
FIRST_SUPERUSER_PASSWORD = os.getenv("FIRST_SUPERUSER_PASSWORD")
|
||||
|
||||
|
||||
@@ -1,37 +1,12 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import jwt
|
||||
from fastapi import Security
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from jwt import PyJWTError
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.status import HTTP_403_FORBIDDEN
|
||||
|
||||
from app.core.config import SECRET_KEY
|
||||
from app.crud.user import get_user
|
||||
from app.db.database import get_default_bucket
|
||||
from app.models.token import TokenPayload
|
||||
from app.core import config
|
||||
|
||||
ALGORITHM = "HS256"
|
||||
access_token_jwt_subject = "access"
|
||||
|
||||
reusable_oauth2 = OAuth2PasswordBearer(tokenUrl="/api/v1/login/access-token")
|
||||
|
||||
|
||||
def get_current_user(token: str = Security(reusable_oauth2)):
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
token_data = TokenPayload(**payload)
|
||||
except PyJWTError:
|
||||
raise HTTPException(
|
||||
status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials"
|
||||
)
|
||||
bucket = get_default_bucket()
|
||||
user = get_user(bucket, username=token_data.username)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
return user
|
||||
|
||||
|
||||
def create_access_token(*, data: dict, expires_delta: timedelta = None):
|
||||
to_encode = data.copy()
|
||||
@@ -40,5 +15,5 @@ def create_access_token(*, data: dict, expires_delta: timedelta = None):
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(minutes=15)
|
||||
to_encode.update({"exp": expire, "sub": access_token_jwt_subject})
|
||||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
||||
encoded_jwt = jwt.encode(to_encode, config.SECRET_KEY, algorithm=ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
@@ -3,9 +3,9 @@ from passlib.context import CryptContext
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
|
||||
def verify_password(plain_password, hashed_password):
|
||||
def verify_password(plain_password: str, hashed_password: str):
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def get_password_hash(password):
|
||||
def get_password_hash(password: str):
|
||||
return pwd_context.hash(password)
|
||||
|
||||
Reference in New Issue
Block a user