Revised the login_required method of the DigestAuth class for readability.
This commit is contained in:
parent
8c98d35934
commit
9d0d0b2686
@ -110,6 +110,36 @@ class DigestAuth:
|
|||||||
class NoLogInException(Exception):
|
class NoLogInException(Exception):
|
||||||
"""The exception thrown when the user is not authorized."""
|
"""The exception thrown when the user is not authorized."""
|
||||||
|
|
||||||
|
def get_logged_in_user() -> t.Optional[t.Any]:
|
||||||
|
"""Returns the currently logged-in user.
|
||||||
|
|
||||||
|
:return: The currently logged-in user.
|
||||||
|
:raise NoLogInException: When the user is not logged in.
|
||||||
|
"""
|
||||||
|
if "user" not in session:
|
||||||
|
raise NoLogInException
|
||||||
|
user: t.Optional[t.Any] = self.__get_user(session["user"])
|
||||||
|
if user is None:
|
||||||
|
raise NoLogInException
|
||||||
|
return user
|
||||||
|
|
||||||
|
def auth_user(state: AuthState) -> t.Any:
|
||||||
|
"""Authenticates a user.
|
||||||
|
|
||||||
|
:param state: The authentication state.
|
||||||
|
:return: The user.
|
||||||
|
:raise UnauthorizedException: When the authentication fails.
|
||||||
|
"""
|
||||||
|
authorization: Authorization = request.authorization
|
||||||
|
if authorization is None:
|
||||||
|
raise UnauthorizedException
|
||||||
|
if authorization.type != "digest":
|
||||||
|
raise UnauthorizedException(
|
||||||
|
"Not an HTTP digest authorization")
|
||||||
|
self.authenticate(state)
|
||||||
|
session["user"] = authorization.username
|
||||||
|
return self.__get_user(authorization.username)
|
||||||
|
|
||||||
@wraps(view)
|
@wraps(view)
|
||||||
def login_required_view(*args, **kwargs) -> t.Any:
|
def login_required_view(*args, **kwargs) -> t.Any:
|
||||||
"""The login-protected view.
|
"""The login-protected view.
|
||||||
@ -119,27 +149,15 @@ class DigestAuth:
|
|||||||
:return: The response.
|
:return: The response.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
if "user" not in session:
|
g.user = get_logged_in_user()
|
||||||
raise NoLogInException
|
|
||||||
user: t.Optional[t.Any] = self.__get_user(session["user"])
|
|
||||||
if user is None:
|
|
||||||
raise NoLogInException
|
|
||||||
g.user = user
|
|
||||||
return view(*args, **kwargs)
|
return view(*args, **kwargs)
|
||||||
except NoLogInException:
|
except NoLogInException:
|
||||||
|
pass
|
||||||
|
|
||||||
state: AuthState = AuthState()
|
state: AuthState = AuthState()
|
||||||
authorization: Authorization = request.authorization
|
|
||||||
try:
|
try:
|
||||||
if authorization is None:
|
g.user = auth_user(state)
|
||||||
raise UnauthorizedException
|
self.__on_login(g.user)
|
||||||
if authorization.type != "digest":
|
|
||||||
raise UnauthorizedException(
|
|
||||||
"Not an HTTP digest authorization")
|
|
||||||
self.authenticate(state)
|
|
||||||
session["user"] = authorization.username
|
|
||||||
user = self.__get_user(authorization.username)
|
|
||||||
g.user = user
|
|
||||||
self.__on_login(user)
|
|
||||||
return view(*args, **kwargs)
|
return view(*args, **kwargs)
|
||||||
except UnauthorizedException as e:
|
except UnauthorizedException as e:
|
||||||
if len(e.args) > 0:
|
if len(e.args) > 0:
|
||||||
|
Loading…
Reference in New Issue
Block a user