Added the initial version that works.
This commit is contained in:
		
							
								
								
									
										51
									
								
								tests/test_algo.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								tests/test_algo.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
# The Flask HTTP Digest Authentication Project.
 | 
			
		||||
# Author: imacat@mail.imacat.idv.tw (imacat), 2022/10/30
 | 
			
		||||
 | 
			
		||||
#  Copyright (c) 2022 imacat.
 | 
			
		||||
#
 | 
			
		||||
#  Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
#  you may not use this file except in compliance with the License.
 | 
			
		||||
#  You may obtain a copy of the License at
 | 
			
		||||
#
 | 
			
		||||
#      http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
#
 | 
			
		||||
#  Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
#  distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
#  See the License for the specific language governing permissions and
 | 
			
		||||
#  limitations under the License.
 | 
			
		||||
 | 
			
		||||
"""The test case for the HTTP digest authentication algorithm.
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
import typing as t
 | 
			
		||||
import unittest
 | 
			
		||||
 | 
			
		||||
from flask_digest_auth import make_password_hash, calc_response
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AlgorithmTestCase(unittest.TestCase):
 | 
			
		||||
    """The test case for the HTTP digest authentication algorithm."""
 | 
			
		||||
 | 
			
		||||
    def test_response_value(self) -> None:
 | 
			
		||||
        """Tests the response value.
 | 
			
		||||
        See https://en.wikipedia.org/wiki/Digest_access_authentication.
 | 
			
		||||
 | 
			
		||||
        :return: None.
 | 
			
		||||
        """
 | 
			
		||||
        realm: str = "testrealm@host.com"
 | 
			
		||||
        username: str = "Mufasa"
 | 
			
		||||
        password: str = "Circle Of Life"
 | 
			
		||||
        method: str = "GET"
 | 
			
		||||
        uri: str = "/dir/index.html"
 | 
			
		||||
        nonce: str = "dcd98b7102dd2f0e8b11d0f600bfb0c093"
 | 
			
		||||
        qop: t.Optional[t.Literal["auth", "auth-int"]] = "auth"
 | 
			
		||||
        algorithm: t.Optional[t.Literal["MD5", "MD5-sess"]] = None
 | 
			
		||||
        cnonce: t.Optional[str] = "0a4f113b"
 | 
			
		||||
        nc: t.Optional[str] = "00000001"
 | 
			
		||||
        body: t.Optional[bytes] = None
 | 
			
		||||
 | 
			
		||||
        password_hash: str = make_password_hash(realm, username, password)
 | 
			
		||||
        response: str = calc_response(method, uri, password_hash, nonce, qop,
 | 
			
		||||
                                      algorithm, cnonce, nc, body)
 | 
			
		||||
        self.assertEqual(response, "6629fae49393a05397450978507c4ef1")
 | 
			
		||||
							
								
								
									
										108
									
								
								tests/test_auth.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								tests/test_auth.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,108 @@
 | 
			
		||||
# The Flask HTTP Digest Authentication Project.
 | 
			
		||||
# Author: imacat@mail.imacat.idv.tw (imacat), 2022/10/22
 | 
			
		||||
 | 
			
		||||
#  Copyright (c) 2022 imacat.
 | 
			
		||||
#
 | 
			
		||||
#  Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
#  you may not use this file except in compliance with the License.
 | 
			
		||||
#  You may obtain a copy of the License at
 | 
			
		||||
#
 | 
			
		||||
#      http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
#
 | 
			
		||||
#  Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
#  distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
#  See the License for the specific language governing permissions and
 | 
			
		||||
#  limitations under the License.
 | 
			
		||||
 | 
			
		||||
"""The test case for the HTTP digest authentication.
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
import typing as t
 | 
			
		||||
from secrets import token_urlsafe
 | 
			
		||||
from types import SimpleNamespace
 | 
			
		||||
 | 
			
		||||
from flask import Response, Flask, g
 | 
			
		||||
from flask_testing import TestCase
 | 
			
		||||
 | 
			
		||||
from flask_digest_auth import DigestAuth, make_password_hash, Client
 | 
			
		||||
 | 
			
		||||
_REALM: str = "testrealm@host.com"
 | 
			
		||||
_USERNAME: str = "Mufasa"
 | 
			
		||||
_PASSWORD: str = "Circle Of Life"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AuthenticationTestCase(TestCase):
 | 
			
		||||
    """The test case for the HTTP digest authentication."""
 | 
			
		||||
 | 
			
		||||
    def create_app(self):
 | 
			
		||||
        """Creates the Flask application.
 | 
			
		||||
 | 
			
		||||
        :return: The Flask application.
 | 
			
		||||
        """
 | 
			
		||||
        auth: DigestAuth = DigestAuth(realm=_REALM)
 | 
			
		||||
        user_db: t.Dict[str, str] \
 | 
			
		||||
            = {_USERNAME: make_password_hash(_REALM, _USERNAME, _PASSWORD)}
 | 
			
		||||
 | 
			
		||||
        @auth.register_get_password
 | 
			
		||||
        def get_password_hash(username: str) -> t.Optional[str]:
 | 
			
		||||
            """Returns the password hash of a user.
 | 
			
		||||
 | 
			
		||||
            :param username: The username.
 | 
			
		||||
            :return: The password hash, or None if the user does not exist.
 | 
			
		||||
            """
 | 
			
		||||
            return user_db[username] if username in user_db else None
 | 
			
		||||
 | 
			
		||||
        @auth.register_get_user
 | 
			
		||||
        def get_user(username: str) -> t.Optional[t.Any]:
 | 
			
		||||
            """Returns a user.
 | 
			
		||||
 | 
			
		||||
            :param username: The username.
 | 
			
		||||
            :return: The user, or None if the user does not exist.
 | 
			
		||||
            """
 | 
			
		||||
            return SimpleNamespace(username=username) if username in user_db \
 | 
			
		||||
                else None
 | 
			
		||||
 | 
			
		||||
        app: Flask = Flask(__name__)
 | 
			
		||||
        app.config.from_mapping({
 | 
			
		||||
            "SECRET_KEY": token_urlsafe(32),
 | 
			
		||||
            "TESTING": True
 | 
			
		||||
        })
 | 
			
		||||
        app.test_client_class = Client
 | 
			
		||||
 | 
			
		||||
        @app.route("/login-required-1/auth", endpoint="auth-1")
 | 
			
		||||
        @auth.login_required
 | 
			
		||||
        def login_required_1() -> str:
 | 
			
		||||
            """The first dummy view.
 | 
			
		||||
 | 
			
		||||
            :return: The response.
 | 
			
		||||
            """
 | 
			
		||||
            return f"Hello, {g.user.username}! #1"
 | 
			
		||||
 | 
			
		||||
        @app.route("/login-required-2/auth", endpoint="auth-2")
 | 
			
		||||
        @auth.login_required
 | 
			
		||||
        def login_required_2() -> str:
 | 
			
		||||
            """The second dummy view.
 | 
			
		||||
 | 
			
		||||
            :return: The response.
 | 
			
		||||
            """
 | 
			
		||||
            return f"Hello, {g.user.username}! #2"
 | 
			
		||||
 | 
			
		||||
        return app
 | 
			
		||||
 | 
			
		||||
    def test_auth(self) -> None:
 | 
			
		||||
        """Tests the authentication.
 | 
			
		||||
 | 
			
		||||
        :return: None.
 | 
			
		||||
        """
 | 
			
		||||
        response: Response = self.client.get(self.app.url_for("auth-1"))
 | 
			
		||||
        self.assertEqual(response.status_code, 401)
 | 
			
		||||
        response = self.client.get(
 | 
			
		||||
            self.app.url_for("auth-1"), digest_auth=(_USERNAME, _PASSWORD))
 | 
			
		||||
        self.assertEqual(response.status_code, 200)
 | 
			
		||||
        self.assertEqual(response.data.decode("UTF-8"),
 | 
			
		||||
                         f"Hello, {_USERNAME}! #1")
 | 
			
		||||
        response: Response = self.client.get(self.app.url_for("auth-2"))
 | 
			
		||||
        self.assertEqual(response.status_code, 200)
 | 
			
		||||
        self.assertEqual(response.data.decode("UTF-8"),
 | 
			
		||||
                         f"Hello, {_USERNAME}! #2")
 | 
			
		||||
							
								
								
									
										129
									
								
								tests/test_flask_login.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										129
									
								
								tests/test_flask_login.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,129 @@
 | 
			
		||||
# The Flask HTTP Digest Authentication Project.
 | 
			
		||||
# Author: imacat@mail.imacat.idv.tw (imacat), 2022/11/23
 | 
			
		||||
 | 
			
		||||
#  Copyright (c) 2022 imacat.
 | 
			
		||||
#
 | 
			
		||||
#  Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
#  you may not use this file except in compliance with the License.
 | 
			
		||||
#  You may obtain a copy of the License at
 | 
			
		||||
#
 | 
			
		||||
#      http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
#
 | 
			
		||||
#  Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
#  distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
#  See the License for the specific language governing permissions and
 | 
			
		||||
#  limitations under the License.
 | 
			
		||||
 | 
			
		||||
"""The test case for the Flask-Login integration.
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
import typing as t
 | 
			
		||||
from secrets import token_urlsafe
 | 
			
		||||
 | 
			
		||||
import flask_login
 | 
			
		||||
from flask import Response, Flask
 | 
			
		||||
from flask_login import LoginManager
 | 
			
		||||
from flask_testing import TestCase
 | 
			
		||||
 | 
			
		||||
from flask_digest_auth import DigestAuth, make_password_hash, Client, \
 | 
			
		||||
    init_login_manager
 | 
			
		||||
 | 
			
		||||
_REALM: str = "testrealm@host.com"
 | 
			
		||||
_USERNAME: str = "Mufasa"
 | 
			
		||||
_PASSWORD: str = "Circle Of Life"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class User:
 | 
			
		||||
    def __init__(self, username: str):
 | 
			
		||||
        self.username: str = username
 | 
			
		||||
        self.is_authenticated: bool = True
 | 
			
		||||
        self.is_active: bool = True
 | 
			
		||||
        self.is_anonymous: bool = False
 | 
			
		||||
 | 
			
		||||
    def get_id(self) -> str:
 | 
			
		||||
        """Returns the username.
 | 
			
		||||
        This is required by Flask-Login.
 | 
			
		||||
 | 
			
		||||
        :return: The username.
 | 
			
		||||
        """
 | 
			
		||||
        return self.username
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FlaskLoginTestCase(TestCase):
 | 
			
		||||
    """The test case with the Flask-Login integration."""
 | 
			
		||||
 | 
			
		||||
    def create_app(self):
 | 
			
		||||
        """Creates the Flask application.
 | 
			
		||||
 | 
			
		||||
        :return: The Flask application.
 | 
			
		||||
        """
 | 
			
		||||
        auth: DigestAuth = DigestAuth(realm=_REALM)
 | 
			
		||||
        login_manager: LoginManager = LoginManager()
 | 
			
		||||
        user_db: t.Dict[str, str] \
 | 
			
		||||
            = {_USERNAME: make_password_hash(_REALM, _USERNAME, _PASSWORD)}
 | 
			
		||||
 | 
			
		||||
        @auth.register_get_password
 | 
			
		||||
        def get_password_hash(username: str) -> t.Optional[str]:
 | 
			
		||||
            """Returns the password hash of a user.
 | 
			
		||||
 | 
			
		||||
            :param username: The username.
 | 
			
		||||
            :return: The password hash, or None if the user does not exist.
 | 
			
		||||
            """
 | 
			
		||||
            return user_db[username] if username in user_db else None
 | 
			
		||||
 | 
			
		||||
        app: Flask = Flask(__name__)
 | 
			
		||||
        app.config.from_mapping({
 | 
			
		||||
            "SECRET_KEY": token_urlsafe(32),
 | 
			
		||||
            "TESTING": True
 | 
			
		||||
        })
 | 
			
		||||
        app.test_client_class = Client
 | 
			
		||||
 | 
			
		||||
        login_manager.init_app(app)
 | 
			
		||||
        init_login_manager(auth, login_manager)
 | 
			
		||||
 | 
			
		||||
        @login_manager.user_loader
 | 
			
		||||
        def load_user(user_id: str) -> t.Optional[User]:
 | 
			
		||||
            """Loads a user.
 | 
			
		||||
 | 
			
		||||
            :param user_id: The username.
 | 
			
		||||
            :return: The user, or None if the user does not exist.
 | 
			
		||||
            """
 | 
			
		||||
            return User(user_id) if user_id in user_db else None
 | 
			
		||||
 | 
			
		||||
        @app.route("/login-required-1/auth", endpoint="auth-1")
 | 
			
		||||
        @flask_login.login_required
 | 
			
		||||
        def login_required_1() -> str:
 | 
			
		||||
            """The first dummy view.
 | 
			
		||||
 | 
			
		||||
            :return: The response.
 | 
			
		||||
            """
 | 
			
		||||
            return f"Hello, {flask_login.current_user.username}! #1"
 | 
			
		||||
 | 
			
		||||
        @app.route("/login-required-2/auth", endpoint="auth-2")
 | 
			
		||||
        @flask_login.login_required
 | 
			
		||||
        def login_required_2() -> str:
 | 
			
		||||
            """The second dummy view.
 | 
			
		||||
 | 
			
		||||
            :return: The response.
 | 
			
		||||
            """
 | 
			
		||||
            return f"Hello, {flask_login.current_user.username}! #2"
 | 
			
		||||
 | 
			
		||||
        return app
 | 
			
		||||
 | 
			
		||||
    def test_auth(self) -> None:
 | 
			
		||||
        """Tests the authentication.
 | 
			
		||||
 | 
			
		||||
        :return: None.
 | 
			
		||||
        """
 | 
			
		||||
        response: Response = self.client.get(self.app.url_for("auth-1"))
 | 
			
		||||
        self.assertEqual(response.status_code, 401)
 | 
			
		||||
        response = self.client.get(
 | 
			
		||||
            self.app.url_for("auth-1"), digest_auth=(_USERNAME, _PASSWORD))
 | 
			
		||||
        self.assertEqual(response.status_code, 200)
 | 
			
		||||
        self.assertEqual(response.data.decode("UTF-8"),
 | 
			
		||||
                         f"Hello, {_USERNAME}! #1")
 | 
			
		||||
        response: Response = self.client.get(self.app.url_for("auth-2"))
 | 
			
		||||
        self.assertEqual(response.status_code, 200)
 | 
			
		||||
        self.assertEqual(response.data.decode("UTF-8"),
 | 
			
		||||
                         f"Hello, {_USERNAME}! #2")
 | 
			
		||||
		Reference in New Issue
	
	Block a user