working on new system for importing
This commit is contained in:
@@ -20,6 +20,7 @@ pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
# JWT Security
|
||||
security = HTTPBearer()
|
||||
optional_security = HTTPBearer(auto_error=False)
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
@@ -190,6 +191,31 @@ def get_current_user(
|
||||
return user
|
||||
|
||||
|
||||
def get_optional_current_user(
|
||||
credentials: Optional[HTTPAuthorizationCredentials] = Depends(optional_security),
|
||||
db: Session = Depends(get_db)
|
||||
) -> Optional[User]:
|
||||
"""Get current authenticated user, but allow None if not authenticated"""
|
||||
if not credentials:
|
||||
return None
|
||||
|
||||
try:
|
||||
token = credentials.credentials
|
||||
username = verify_token(token)
|
||||
|
||||
if username is None:
|
||||
return None
|
||||
|
||||
user = db.query(User).filter(User.username == username).first()
|
||||
if user is None or not user.is_active:
|
||||
return None
|
||||
|
||||
return user
|
||||
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def get_admin_user(current_user: User = Depends(get_current_user)) -> User:
|
||||
"""Require admin privileges"""
|
||||
if not current_user.is_admin:
|
||||
|
||||
Reference in New Issue
Block a user