Compare commits
7 Commits
0387abb4f6
...
v0.3.1
Author | SHA1 | Date | |
---|---|---|---|
2de770aed0 | |||
9ab413d583 | |||
aeb93a60e5 | |||
a07118ef9c | |||
514e9255aa | |||
79abdc9cde | |||
038e7a8352 |
@ -350,7 +350,7 @@ A unittest Test Case
|
|||||||
response = self.client.get("/admin")
|
response = self.client.get("/admin")
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, 401)
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
"/admin", digest_auth=("my_name", "my_pass"))
|
"/admin", digest_auth=(USERNAME, PASSWORD))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
@ -382,7 +382,7 @@ A pytest Test
|
|||||||
response = client.get("/admin")
|
response = client.get("/admin")
|
||||||
assert response.status_code == 401
|
assert response.status_code == 401
|
||||||
response = client.get(
|
response = client.get(
|
||||||
"/admin", digest_auth=("my_name", "my_pass"))
|
"/admin", digest_auth=(USERNAME, PASSWORD))
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -229,7 +229,7 @@ A unittest Test Case
|
|||||||
response = self.client.get("/admin")
|
response = self.client.get("/admin")
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, 401)
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
"/admin", digest_auth=("my_name", "my_pass"))
|
"/admin", digest_auth=(USERNAME, PASSWORD))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
|
||||||
@ -264,5 +264,5 @@ A pytest Test
|
|||||||
response = client.get("/admin")
|
response = client.get("/admin")
|
||||||
assert response.status_code == 401
|
assert response.status_code == 401
|
||||||
response = client.get(
|
response = client.get(
|
||||||
"/admin", digest_auth=("my_name", "my_pass"))
|
"/admin", digest_auth=(USERNAME, PASSWORD))
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
@ -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.
|
||||||
|
@ -70,7 +70,7 @@ class DigestAuth:
|
|||||||
"""The callback to run when the user logs in."""
|
"""The callback to run when the user logs in."""
|
||||||
|
|
||||||
def login_required(self, view) -> t.Callable:
|
def login_required(self, view) -> t.Callable:
|
||||||
"""The view decorator for HTTP digest authentication.
|
"""The view decorator for the HTTP digest authentication.
|
||||||
|
|
||||||
:Example:
|
:Example:
|
||||||
|
|
||||||
@ -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)
|
||||||
|
@ -51,7 +51,7 @@ class Client(WerkzeugClient):
|
|||||||
response = self.client.get("/admin")
|
response = self.client.get("/admin")
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, 401)
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
"/admin", digest_auth=("my_name", "my_pass"))
|
"/admin", digest_auth=(USERNAME, PASSWORD))
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
For pytest_:
|
For pytest_:
|
||||||
@ -76,7 +76,7 @@ class Client(WerkzeugClient):
|
|||||||
response = client.get("/admin")
|
response = client.get("/admin")
|
||||||
assert response.status_code == 401
|
assert response.status_code == 401
|
||||||
response = client.get(
|
response = client.get(
|
||||||
"/admin", digest_auth=("my_name", "my_pass"))
|
"/admin", digest_auth=(USERNAME, PASSWORD))
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
.. _unittest: https://docs.python.org/3/library/unittest.html
|
.. _unittest: https://docs.python.org/3/library/unittest.html
|
||||||
@ -87,12 +87,8 @@ class Client(WerkzeugClient):
|
|||||||
**kwargs) -> TestResponse:
|
**kwargs) -> TestResponse:
|
||||||
"""Opens a request.
|
"""Opens a request.
|
||||||
|
|
||||||
.. warning::
|
|
||||||
This is to override the parent ``open`` method. You should call
|
|
||||||
the ``get``, ``post``, ``put``, and ``delete`` methods instead.
|
|
||||||
|
|
||||||
:param args: The arguments.
|
:param args: The arguments.
|
||||||
:param digest_auth: A tuple of the username and password for the HTTP
|
:param digest_auth: The (*username*, *password*) tuple for the HTTP
|
||||||
digest authentication.
|
digest authentication.
|
||||||
:param kwargs: The keyword arguments.
|
:param kwargs: The keyword arguments.
|
||||||
:return: The response.
|
:return: The response.
|
||||||
@ -115,9 +111,6 @@ class Client(WerkzeugClient):
|
|||||||
username: str, password: str) -> Authorization:
|
username: str, password: str) -> Authorization:
|
||||||
"""Composes and returns the request authorization.
|
"""Composes and returns the request authorization.
|
||||||
|
|
||||||
.. warning::
|
|
||||||
This method is not for public.
|
|
||||||
|
|
||||||
:param www_authenticate: The ``WWW-Authenticate`` response.
|
:param www_authenticate: The ``WWW-Authenticate`` response.
|
||||||
:param uri: The request URI.
|
:param uri: The request URI.
|
||||||
:param username: The username.
|
:param username: The username.
|
||||||
|
@ -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)
|
||||||
|
Reference in New Issue
Block a user