diff --git a/README.rst b/README.rst index 7c8f9c5..aa960b8 100644 --- a/README.rst +++ b/README.rst @@ -52,6 +52,13 @@ You may also install the latest source from the pip install git+https://github.com/imacat/flask-digestauth.git +Configuration +============= + +Flask-DigestAuth takes the configuration ``DIGEST_AUTH_REALM`` as the +realm. The default realm is ``Login Required``. + + Setting the Password ==================== @@ -89,7 +96,7 @@ In your ``my_app.py``: app: flask = Flask(__name__) ... (Configure the Flask application) ... - auth: DigestAuth = DigestAuth(realm="Admin") + auth: DigestAuth = DigestAuth() auth.init_app(app) @auth.register_get_password @@ -128,7 +135,6 @@ In your ``my_app/__init__.py``: app: flask = Flask(__name__) ... (Configure the Flask application) ... - auth.realm = app.config["REALM"] auth.init_app(app) @auth.register_get_password @@ -207,7 +213,7 @@ In your ``my_app.py``: def load_user(user_id: str) -> t.Optional[User]: ... (Load the user with the username) ... - auth: DigestAuth = DigestAuth(realm="Admin") + auth: DigestAuth = DigestAuth() auth.init_app(app) @auth.register_get_password @@ -251,7 +257,6 @@ In your ``my_app/__init__.py``: def load_user(user_id: str) -> t.Optional[User]: ... (Load the user with the username) ... - auth.realm = app.config["REALM"] auth.init_app(app) @auth.register_get_password @@ -340,8 +345,9 @@ A unittest Test Case def create_app(self): app: Flask = create_app({ + "TESTING": True, "SECRET_KEY": token_urlsafe(32), - "TESTING": True + "DIGEST_AUTH_REALM": "admin", }) app.test_client_class = Client return app @@ -367,8 +373,9 @@ A pytest Test @pytest.fixture() def app(): app: Flask = create_app({ + "TESTING": True, "SECRET_KEY": token_urlsafe(32), - "TESTING": True + "DIGEST_AUTH_REALM": "admin", }) app.test_client_class = Client yield app diff --git a/docs/source/examples.rst b/docs/source/examples.rst index c314537..5a6b1ca 100644 --- a/docs/source/examples.rst +++ b/docs/source/examples.rst @@ -17,7 +17,7 @@ In your ``my_app.py``: app: flask = Flask(__name__) ... (Configure the Flask application) ... - auth: DigestAuth = DigestAuth(realm="Admin") + auth: DigestAuth = DigestAuth() auth.init_app(app) @auth.register_get_password @@ -58,7 +58,6 @@ In your ``my_app/__init__.py``: app: flask = Flask(__name__) ... (Configure the Flask application) ... - auth.realm = app.config["REALM"] auth.init_app(app) @auth.register_get_password @@ -118,7 +117,7 @@ In your ``my_app.py``: def load_user(user_id: str) -> t.Optional[User]: ... (Load the user with the username) ... - auth: DigestAuth = DigestAuth(realm="Admin") + auth: DigestAuth = DigestAuth() auth.init_app(app) @auth.register_get_password @@ -164,7 +163,6 @@ In your ``my_app/__init__.py``: def load_user(user_id: str) -> t.Optional[User]: ... (Load the user with the username) ... - auth.realm = app.config["REALM"] auth.init_app(app) @auth.register_get_password @@ -219,8 +217,9 @@ A unittest Test Case def create_app(self): app: Flask = create_app({ + "TESTING": True, "SECRET_KEY": token_urlsafe(32), - "TESTING": True + "DIGEST_AUTH_REALM": "admin", }) app.test_client_class = Client return app @@ -249,8 +248,9 @@ A pytest Test @pytest.fixture() def app(): app: Flask = create_app({ + "TESTING": True, "SECRET_KEY": token_urlsafe(32), - "TESTING": True + "DIGEST_AUTH_REALM": "admin", }) app.test_client_class = Client yield app diff --git a/docs/source/intro.rst b/docs/source/intro.rst index f5c9207..fdfc7f7 100644 --- a/docs/source/intro.rst +++ b/docs/source/intro.rst @@ -46,6 +46,13 @@ You may also install the latest source from the pip install git+https://github.com/imacat/flask-digestauth.git +Configuration +------------- + +Flask-DigestAuth takes the configuration ``DIGEST_AUTH_REALM`` as the +realm. The default realm is ``Login Required``. + + Setting the Password -------------------- diff --git a/src/flask_digest_auth/auth.py b/src/flask_digest_auth/auth.py index 5b9b70b..417c5c0 100644 --- a/src/flask_digest_auth/auth.py +++ b/src/flask_digest_auth/auth.py @@ -46,8 +46,8 @@ class DigestAuth: self.__serializer: URLSafeTimedSerializer \ = URLSafeTimedSerializer(token_urlsafe(32)) """The serializer to generate and validate the nonce and opaque.""" - self.realm: str = "" if realm is None else realm - """The realm. Default is an empty string.""" + self.realm: str = "Login Required" if realm is None else realm + """The realm. Default is "Login Required".""" self.algorithm: t.Optional[t.Literal["MD5", "MD5-sess"]] = None """The algorithm, either None, ``MD5``, or ``MD5-sess``. Default is None.""" @@ -343,6 +343,8 @@ class DigestAuth: :return: None. """ app.extensions["digest_auth"] = self + if "DIGEST_AUTH_REALM" in app.config: + self.realm = app.config["DIGEST_AUTH_REALM"] if hasattr(app, "login_manager"): from flask_login import LoginManager, login_user diff --git a/tests/test_auth.py b/tests/test_auth.py index 5c7a4ec..02301cc 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -1,7 +1,7 @@ # The Flask HTTP Digest Authentication Project. # Author: imacat@mail.imacat.idv.tw (imacat), 2022/10/22 -# Copyright (c) 2022 imacat. +# Copyright (c) 2022-2023 imacat. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -59,10 +59,11 @@ class AuthenticationTestCase(TestCase): app.config.from_mapping({ "TESTING": True, "SECRET_KEY": token_urlsafe(32), + "DIGEST_AUTH_REALM": _REALM, }) app.test_client_class = Client - auth: DigestAuth = DigestAuth(realm=_REALM) + auth: DigestAuth = DigestAuth() auth.init_app(app) self.user: User = User(_USERNAME, _PASSWORD) user_db: t.Dict[str, User] = {_USERNAME: self.user} diff --git a/tests/test_flask_login.py b/tests/test_flask_login.py index b9a1971..e90c9b3 100644 --- a/tests/test_flask_login.py +++ b/tests/test_flask_login.py @@ -1,7 +1,7 @@ # The Flask HTTP Digest Authentication Project. # Author: imacat@mail.imacat.idv.tw (imacat), 2022/11/23 -# Copyright (c) 2022 imacat. +# Copyright (c) 2022-2023 imacat. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -79,6 +79,7 @@ class FlaskLoginTestCase(TestCase): app.config.from_mapping({ "TESTING": True, "SECRET_KEY": token_urlsafe(32), + "DIGEST_AUTH_REALM": _REALM, }) app.test_client_class = Client @@ -92,7 +93,7 @@ class FlaskLoginTestCase(TestCase): login_manager: flask_login.LoginManager = flask_login.LoginManager() login_manager.init_app(app) - auth: DigestAuth = DigestAuth(realm=_REALM) + auth: DigestAuth = DigestAuth() auth.init_app(app) self.user: User = User(_USERNAME, _PASSWORD)