Compare commits

...

3 Commits

4 changed files with 49 additions and 6 deletions

View File

@ -13,7 +13,7 @@ sys.path.insert(0, os.path.abspath('../../src/'))
project = 'Flask-Digest-Auth' project = 'Flask-Digest-Auth'
copyright = '2022, imacat' copyright = '2022, imacat'
author = 'imacat' author = 'imacat'
release = '0.3.0' release = '0.3.1'
# -- General configuration --------------------------------------------------- # -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

View File

@ -17,7 +17,7 @@
[metadata] [metadata]
name = flask-digest-auth name = flask-digest-auth
version = 0.3.0 version = 0.3.1
author = imacat author = imacat
author_email = imacat@mail.imacat.idv.tw author_email = imacat@mail.imacat.idv.tw
description = The Flask HTTP Digest Authentication project. description = The Flask HTTP Digest Authentication project.

View File

@ -356,10 +356,13 @@ class DigestAuth:
:return: None. :return: None.
""" """
state: AuthState = request.digest_auth_state \
if hasattr(request, "digest_auth_state") \
else AuthState()
response: Response = Response() response: Response = Response()
response.status = 401 response.status = 401
response.headers["WWW-Authenticate"] \ response.headers["WWW-Authenticate"] \
= self.__make_response_header(g.digest_auth_state) = self.__make_response_header(state)
abort(response) abort(response)
@login_manager.request_loader @login_manager.request_loader
@ -370,7 +373,7 @@ class DigestAuth:
:return: The authenticated user, or None if the :return: The authenticated user, or None if the
authentication fails authentication fails
""" """
g.digest_auth_state = AuthState() request.digest_auth_state = AuthState()
authorization: Authorization = req.authorization authorization: Authorization = req.authorization
try: try:
if authorization is None: if authorization is None:
@ -378,7 +381,7 @@ class DigestAuth:
if authorization.type != "digest": if authorization.type != "digest":
raise UnauthorizedException( raise UnauthorizedException(
"Not an HTTP digest authorization") "Not an HTTP digest authorization")
self.__authenticate(g.digest_auth_state) self.__authenticate(request.digest_auth_state)
user = login_manager.user_callback( user = login_manager.user_callback(
authorization.username) authorization.username)
login_user(user) login_user(user)

View File

@ -45,7 +45,6 @@ class User:
self.password_hash: str = make_password_hash( self.password_hash: str = make_password_hash(
_REALM, username, password) _REALM, username, password)
self.visits: int = 0 self.visits: int = 0
self.is_authenticated: bool = True
self.is_active: bool = True self.is_active: bool = True
self.is_anonymous: bool = False self.is_anonymous: bool = False
@ -57,6 +56,16 @@ class User:
""" """
return self.username return self.username
@property
def is_authenticated(self) -> bool:
"""Returns whether the user is authenticated.
This is required by Flask-Login.
This should return self.is_active.
:return: True if the user is active, or False otherwise.
"""
return self.is_active
class FlaskLoginTestCase(TestCase): class FlaskLoginTestCase(TestCase):
"""The test case with the Flask-Login integration.""" """The test case with the Flask-Login integration."""
@ -256,3 +265,34 @@ class FlaskLoginTestCase(TestCase):
response = self.client.get(admin_uri) response = self.client.get(admin_uri)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(self.user.visits, 2) self.assertEqual(self.user.visits, 2)
def test_disabled(self) -> None:
"""Tests the disabled user.
:return: None.
"""
if not self.has_flask_login:
self.skipTest("Skipped without Flask-Login.")
response: Response
self.user.is_active = False
response = self.client.get(self.app.url_for("admin-1"))
self.assertEqual(response.status_code, 401)
response = self.client.get(self.app.url_for("admin-1"),
digest_auth=(_USERNAME, _PASSWORD))
self.assertEqual(response.status_code, 401)
self.user.is_active = True
response = self.client.get(self.app.url_for("admin-1"),
digest_auth=(_USERNAME, _PASSWORD))
self.assertEqual(response.status_code, 200)
response = self.client.get(self.app.url_for("admin-1"))
self.assertEqual(response.status_code, 200)
self.user.is_active = False
response = self.client.get(self.app.url_for("admin-1"))
self.assertEqual(response.status_code, 401)
response = self.client.get(self.app.url_for("admin-1"),
digest_auth=(_USERNAME, _PASSWORD))
self.assertEqual(response.status_code, 401)