Compare commits

..

No commits in common. "5b255b65046077d000255fa261f50bc4225e9126" and "604ed0be2758dfc8940a04dd249fad7952f8270e" have entirely different histories.

View File

@ -225,7 +225,7 @@ class DigestAuth:
if opaque is not None: if opaque is not None:
header += f", opaque=\"{opaque}\"" header += f", opaque=\"{opaque}\""
if state.stale is not None: if state.stale is not None:
header += ", stale=TRUE" if state.stale else ", stale=FALSE" header += f", stale=TRUE" if state.stale else f", stale=FALSE"
if self.algorithm is not None: if self.algorithm is not None:
header += f", algorithm=\"{self.algorithm}\"" header += f", algorithm=\"{self.algorithm}\""
if len(self.__qop) > 0: if len(self.__qop) > 0:
@ -344,57 +344,50 @@ class DigestAuth:
self.realm = app.config["DIGEST_AUTH_REALM"] self.realm = app.config["DIGEST_AUTH_REALM"]
if hasattr(app, "login_manager"): if hasattr(app, "login_manager"):
self.__init_login_manager(app) from flask_login import LoginManager, login_user
def __init_login_manager(self, app: Flask) -> None: login_manager: LoginManager = getattr(app, "login_manager")
"""Initializes the Flask-Login login manager.
:param app: The Flask application. @login_manager.unauthorized_handler
:return: None. def unauthorized() -> None:
""" """Handles when the user is unauthorized.
from flask_login import LoginManager, login_user
login_manager: LoginManager = getattr(app, "login_manager")
@login_manager.unauthorized_handler :return: None.
def unauthorized() -> None: """
"""Handles when the user is unauthorized. state: AuthState = getattr(request, "_digest_auth_state") \
if hasattr(request, "_digest_auth_state") \
else AuthState()
response: Response = Response()
response.status = 401
response.headers["WWW-Authenticate"] \
= self.__make_response_header(state)
abort(response)
:return: None. @login_manager.request_loader
""" def load_user_from_request(req: Request) -> Optional[Any]:
state: AuthState = getattr(request, "_digest_auth_state") \ """Loads the user from the request header.
if hasattr(request, "_digest_auth_state") \
else AuthState()
response: Response = Response()
response.status = 401
response.headers["WWW-Authenticate"] \
= self.__make_response_header(state)
abort(response)
@login_manager.request_loader :param req: The request.
def load_user_from_request(req: Request) -> Optional[Any]: :return: The authenticated user, or None if the
"""Loads the user from the request header. authentication fails
"""
:param req: The request. request._digest_auth_state = AuthState()
:return: The authenticated user, or None if the authorization: Authorization = req.authorization
authentication fails try:
""" if authorization is None:
request._digest_auth_state = AuthState() raise UnauthorizedException
authorization: Authorization = req.authorization if authorization.type != "digest":
try: raise UnauthorizedException(
if authorization is None: "Not an HTTP digest authorization")
raise UnauthorizedException self.__authenticate(request._digest_auth_state)
if authorization.type != "digest": user = login_manager.user_callback(authorization.username)
raise UnauthorizedException( login_user(user)
"Not an HTTP digest authorization") self.__on_login(user)
self.__authenticate(request._digest_auth_state) return user
user = login_manager.user_callback(authorization.username) except UnauthorizedException as e:
login_user(user) if str(e) != "":
self.__on_login(user) app.logger.warning(str(e))
return user return None
except UnauthorizedException as e:
if str(e) != "":
app.logger.warning(str(e))
return None
def logout(self) -> None: def logout(self) -> None:
"""Logs out the user. """Logs out the user.