fix: strict optional errors (#1759)

* fix strict optional errors

* fix typing in repository

* fix backup db files location

* update workspace settings
This commit is contained in:
Hayden
2022-10-23 13:04:04 -08:00
committed by GitHub
parent 97d9e2a109
commit 84c23765cd
31 changed files with 253 additions and 139 deletions

View File

@@ -117,35 +117,58 @@ def validate_long_live_token(session: Session, client_token: str, user_id: str)
def validate_file_token(token: Optional[str] = None) -> Path:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="could not validate file token",
)
"""
Args:
token (Optional[str], optional): _description_. Defaults to None.
Raises:
HTTPException: 400 Bad Request when no token or the file doesn't exist
HTTPException: 401 Unauthorized when the token is invalid
"""
if not token:
return None
raise HTTPException(status.HTTP_400_BAD_REQUEST)
try:
payload = jwt.decode(token, settings.SECRET, algorithms=[ALGORITHM])
file_path = Path(payload.get("file"))
except JWTError as e:
raise credentials_exception from e
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="could not validate file token",
) from e
if not file_path.exists():
raise HTTPException(status.HTTP_400_BAD_REQUEST)
return file_path
def validate_recipe_token(token: Optional[str] = None) -> str:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="could not validate file token",
)
"""
Args:
token (Optional[str], optional): _description_. Defaults to None.
Raises:
HTTPException: 400 Bad Request when no token or the recipe doesn't exist
HTTPException: 401 JWTError when token is invalid
Returns:
str: token data
"""
if not token:
return None
raise HTTPException(status.HTTP_400_BAD_REQUEST)
try:
payload = jwt.decode(token, settings.SECRET, algorithms=[ALGORITHM])
slug = payload.get("slug")
slug: str | None = payload.get("slug")
except JWTError as e:
raise credentials_exception from e
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="could not validate file token",
) from e
if slug is None:
raise HTTPException(status.HTTP_400_BAD_REQUEST)
return slug

View File

@@ -3,6 +3,17 @@ from sqlite3 import IntegrityError
from mealie.lang.providers import Translator
class UnexpectedNone(Exception):
"""Exception raised when a value is None when it should not be."""
def __init__(self, message: str = "Unexpected None Value"):
self.message = message
super().__init__(self.message)
def __str__(self):
return f"{self.message}"
class PermissionDenied(Exception):
"""
This exception is raised when a user tries to access a resource that they do not have permission to access.

View File

@@ -62,11 +62,16 @@ def user_from_ldap(db: AllRepositories, username: str, password: str) -> Private
conn.set_option(ldap.OPT_X_TLS_CACERTFILE, settings.LDAP_TLS_CACERTFILE)
conn.set_option(ldap.OPT_X_TLS_NEWCTX, 0)
user = db.users.get_one(username, "email", any_case=True)
if not settings.LDAP_BIND_TEMPLATE:
return False
if not user:
user_bind = settings.LDAP_BIND_TEMPLATE.format(username)
user = db.users.get_one(username, "username", any_case=True)
else:
user_bind = settings.LDAP_BIND_TEMPLATE.format(user.username)
try:
conn.simple_bind_s(user_bind, password)
except (ldap.INVALID_CREDENTIALS, ldap.NO_SUCH_OBJECT):
@@ -86,7 +91,7 @@ def user_from_ldap(db: AllRepositories, username: str, password: str) -> Private
else:
return False
if not user:
if user is None:
user = db.users.create(
{
"username": username,
@@ -96,6 +101,7 @@ def user_from_ldap(db: AllRepositories, username: str, password: str) -> Private
"admin": False,
},
)
if settings.LDAP_ADMIN_FILTER:
user.admin = len(conn.search_s(user_dn, ldap.SCOPE_BASE, settings.LDAP_ADMIN_FILTER, [])) > 0
db.users.update(user.id, user)

View File

@@ -93,18 +93,18 @@ class AppSettings(BaseSettings):
@staticmethod
def validate_smtp(
host: str,
port: str,
from_name: str,
from_email: str,
strategy: str,
host: str | None,
port: str | None,
from_name: str | None,
from_email: str | None,
strategy: str | None,
user: str | None = None,
password: str | None = None,
) -> bool:
"""Validates all SMTP variables are set"""
required = {host, port, from_name, from_email, strategy}
if strategy.upper() in {"TLS", "SSL"}:
if strategy and strategy.upper() in {"TLS", "SSL"}:
required.add(user)
required.add(password)