mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-04-23 14:25:14 +02:00
working on getting the config
This commit is contained in:
@@ -7,9 +7,9 @@ from fastapi.security import OAuth2PasswordBearer
|
||||
from jwt.exceptions import InvalidTokenError
|
||||
from pydantic import BaseModel
|
||||
|
||||
import config
|
||||
import database
|
||||
import database.users
|
||||
from config import AuthConfig
|
||||
from database.users import UserInternal
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/v1/token")
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
async def get_current_user(token: str = Depends(oauth2_scheme)) -> UserInternal:
|
||||
async def get_current_user(token: str = Depends(oauth2_scheme), config = Depends(AuthConfig)) -> UserInternal:
|
||||
credentials_exception = HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate credentials",
|
||||
@@ -37,7 +37,7 @@ async def get_current_user(token: str = Depends(oauth2_scheme)) -> UserInternal:
|
||||
)
|
||||
log.debug("token: " + token)
|
||||
try:
|
||||
payload = jwt.decode(token, config.auth.jwt_signing_key, algorithms=[config.auth.jwt_signing_algorithm])
|
||||
payload = jwt.decode(token, config.jwt_signing_key, algorithms=[config.jwt_signing_algorithm])
|
||||
log.debug("jwt payload: " + payload.__str__())
|
||||
user_uid: str = payload.get("sub")
|
||||
log.debug("jwt payload sub (user uid): " + user_uid)
|
||||
@@ -55,12 +55,12 @@ async def get_current_user(token: str = Depends(oauth2_scheme)) -> UserInternal:
|
||||
return user
|
||||
|
||||
|
||||
def create_access_token(data: dict, expires_delta: timedelta | None = None):
|
||||
def create_access_token(data: dict, expires_delta: timedelta | None = None, config = Depends(AuthConfig)):
|
||||
to_encode = data.copy()
|
||||
if expires_delta:
|
||||
expire = datetime.now(timezone.utc) + expires_delta
|
||||
else:
|
||||
expire = datetime.now(timezone.utc) + timedelta(minutes=config.auth.jwt_access_token_lifetime)
|
||||
expire = datetime.now(timezone.utc) + timedelta(minutes=config.jwt_access_token_lifetime)
|
||||
to_encode.update({"exp": expire})
|
||||
encoded_jwt = jwt.encode(to_encode, config.auth.jwt_signing_key, algorithm=config.auth.jwt_signing_algorithm)
|
||||
encoded_jwt = jwt.encode(to_encode, config.jwt_signing_key, algorithm=config.jwt_signing_algorithm)
|
||||
return encoded_jwt
|
||||
|
||||
Reference in New Issue
Block a user