2022-11-23 15:08:30 +08:00
|
|
|
# The Flask HTTP Digest Authentication Project.
|
|
|
|
# Author: imacat@mail.imacat.idv.tw (imacat), 2022/11/23
|
|
|
|
|
2023-01-06 00:19:24 +08:00
|
|
|
# Copyright (c) 2022-2023 imacat.
|
2022-11-23 15:08:30 +08:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
"""
|
2023-05-03 08:08:51 +08:00
|
|
|
import logging
|
2022-11-23 15:08:30 +08:00
|
|
|
from secrets import token_urlsafe
|
2023-04-26 23:15:13 +08:00
|
|
|
from typing import Optional, Dict
|
2022-11-23 15:08:30 +08:00
|
|
|
|
2022-11-25 05:40:27 +08:00
|
|
|
from flask import Response, Flask, g, redirect, request
|
2022-11-23 15:08:30 +08:00
|
|
|
from flask_testing import TestCase
|
2022-11-24 18:53:21 +08:00
|
|
|
from werkzeug.datastructures import WWWAuthenticate, Authorization
|
2022-11-23 15:08:30 +08:00
|
|
|
|
2022-11-23 22:02:51 +08:00
|
|
|
from flask_digest_auth import DigestAuth, make_password_hash, Client
|
2022-11-23 15:08:30 +08:00
|
|
|
|
|
|
|
_REALM: str = "testrealm@host.com"
|
2023-06-08 17:20:24 +08:00
|
|
|
"""The realm."""
|
2022-11-23 15:08:30 +08:00
|
|
|
_USERNAME: str = "Mufasa"
|
2023-06-08 17:20:24 +08:00
|
|
|
"""The username."""
|
2022-11-23 15:08:30 +08:00
|
|
|
_PASSWORD: str = "Circle Of Life"
|
2023-06-08 17:20:24 +08:00
|
|
|
"""The password."""
|
2023-06-10 16:45:19 +08:00
|
|
|
SKIPPED_NO_FLASK_LOGIN: str = "Skipped without Flask-Login."
|
|
|
|
"""The message that a test is skipped when Flask-Login is not installed."""
|
2022-11-23 15:08:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
class User:
|
2022-11-24 20:48:09 +08:00
|
|
|
"""A dummy user."""
|
|
|
|
|
2022-12-03 14:30:22 +08:00
|
|
|
def __init__(self, username: str, password: str):
|
2022-11-24 20:48:09 +08:00
|
|
|
"""Constructs a dummy user.
|
|
|
|
|
|
|
|
:param username: The username.
|
2022-12-03 14:30:22 +08:00
|
|
|
:param password: The clear-text password.
|
2022-11-24 20:48:09 +08:00
|
|
|
"""
|
2022-11-23 15:08:30 +08:00
|
|
|
self.username: str = username
|
2023-06-08 17:20:24 +08:00
|
|
|
"""The username."""
|
|
|
|
self.password_hash: str = make_password_hash(_REALM, username, password)
|
|
|
|
"""The password hash."""
|
2022-11-27 03:58:29 +08:00
|
|
|
self.visits: int = 0
|
2023-06-08 17:20:24 +08:00
|
|
|
"""The number of visits."""
|
2022-11-23 15:08:30 +08:00
|
|
|
self.is_active: bool = True
|
2023-06-08 17:20:24 +08:00
|
|
|
"""True if the account is active, or False otherwise."""
|
2022-11-23 15:08:30 +08:00
|
|
|
self.is_anonymous: bool = False
|
2023-06-08 17:20:24 +08:00
|
|
|
"""True if the account is anonymous, or False otherwise."""
|
2022-11-23 15:08:30 +08:00
|
|
|
|
|
|
|
def get_id(self) -> str:
|
|
|
|
"""Returns the username.
|
|
|
|
This is required by Flask-Login.
|
|
|
|
|
|
|
|
:return: The username.
|
|
|
|
"""
|
|
|
|
return self.username
|
|
|
|
|
2022-12-29 23:44:02 +08:00
|
|
|
@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
|
|
|
|
|
2022-11-23 15:08:30 +08:00
|
|
|
|
|
|
|
class FlaskLoginTestCase(TestCase):
|
|
|
|
"""The test case with the Flask-Login integration."""
|
|
|
|
|
2022-11-23 22:02:51 +08:00
|
|
|
def create_app(self) -> Flask:
|
2022-11-23 15:08:30 +08:00
|
|
|
"""Creates the Flask application.
|
|
|
|
|
|
|
|
:return: The Flask application.
|
|
|
|
"""
|
2023-05-03 08:08:51 +08:00
|
|
|
logging.getLogger("test_flask_login").addHandler(logging.NullHandler())
|
2022-11-23 20:36:41 +08:00
|
|
|
app: Flask = Flask(__name__)
|
|
|
|
app.config.from_mapping({
|
2023-01-05 22:50:59 +08:00
|
|
|
"TESTING": True,
|
2022-11-23 20:36:41 +08:00
|
|
|
"SECRET_KEY": token_urlsafe(32),
|
2023-01-06 00:19:24 +08:00
|
|
|
"DIGEST_AUTH_REALM": _REALM,
|
2022-11-23 20:36:41 +08:00
|
|
|
})
|
|
|
|
app.test_client_class = Client
|
|
|
|
|
2023-06-08 17:28:35 +08:00
|
|
|
self.__has_flask_login: bool = True
|
2023-06-08 17:20:24 +08:00
|
|
|
"""Whether the Flask-Login package is installed."""
|
2022-11-23 22:02:51 +08:00
|
|
|
try:
|
|
|
|
import flask_login
|
|
|
|
except ModuleNotFoundError:
|
2023-06-08 17:28:35 +08:00
|
|
|
self.__has_flask_login = False
|
2022-11-23 22:02:51 +08:00
|
|
|
return app
|
|
|
|
|
|
|
|
login_manager: flask_login.LoginManager = flask_login.LoginManager()
|
2022-11-23 20:36:41 +08:00
|
|
|
login_manager.init_app(app)
|
|
|
|
|
2023-01-06 00:19:24 +08:00
|
|
|
auth: DigestAuth = DigestAuth()
|
2022-11-23 22:02:51 +08:00
|
|
|
auth.init_app(app)
|
2022-11-23 20:36:41 +08:00
|
|
|
|
2023-06-08 17:28:35 +08:00
|
|
|
self.__user: User = User(_USERNAME, _PASSWORD)
|
2023-06-08 17:20:24 +08:00
|
|
|
"""The user account."""
|
2023-06-08 17:28:35 +08:00
|
|
|
user_db: Dict[str, User] = {_USERNAME: self.__user}
|
2022-11-23 15:08:30 +08:00
|
|
|
|
|
|
|
@auth.register_get_password
|
2023-04-26 23:15:13 +08:00
|
|
|
def get_password_hash(username: str) -> Optional[str]:
|
2022-11-23 15:08:30 +08:00
|
|
|
"""Returns the password hash of a user.
|
|
|
|
|
|
|
|
:param username: The username.
|
|
|
|
:return: The password hash, or None if the user does not exist.
|
|
|
|
"""
|
2022-11-27 03:58:29 +08:00
|
|
|
return user_db[username].password_hash if username in user_db \
|
|
|
|
else None
|
|
|
|
|
|
|
|
@auth.register_on_login
|
|
|
|
def on_login(user: User):
|
|
|
|
"""The callback when the user logs in.
|
|
|
|
|
|
|
|
:param user: The logged-in user.
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
user.visits = user.visits + 1
|
2022-11-23 15:08:30 +08:00
|
|
|
|
|
|
|
@login_manager.user_loader
|
2023-04-26 23:15:13 +08:00
|
|
|
def load_user(user_id: str) -> Optional[User]:
|
2022-11-23 15:08:30 +08:00
|
|
|
"""Loads a user.
|
|
|
|
|
|
|
|
:param user_id: The username.
|
|
|
|
:return: The user, or None if the user does not exist.
|
|
|
|
"""
|
2022-11-27 03:58:29 +08:00
|
|
|
return user_db[user_id] if user_id in user_db else None
|
2022-11-23 15:08:30 +08:00
|
|
|
|
2022-11-24 20:46:45 +08:00
|
|
|
@app.get("/admin-1/auth", endpoint="admin-1")
|
2022-11-23 15:08:30 +08:00
|
|
|
@flask_login.login_required
|
2022-11-24 20:46:45 +08:00
|
|
|
def admin_1() -> str:
|
|
|
|
"""The first administration section.
|
2022-11-23 15:08:30 +08:00
|
|
|
|
|
|
|
:return: The response.
|
|
|
|
"""
|
2022-11-26 15:59:17 +08:00
|
|
|
return f"Hello, {flask_login.current_user.get_id()}! #1"
|
2022-11-23 15:08:30 +08:00
|
|
|
|
2022-11-24 20:46:45 +08:00
|
|
|
@app.get("/admin-2/auth", endpoint="admin-2")
|
2022-11-23 15:08:30 +08:00
|
|
|
@flask_login.login_required
|
2022-11-24 20:46:45 +08:00
|
|
|
def admin_2() -> str:
|
|
|
|
"""The second administration section.
|
2022-11-23 15:08:30 +08:00
|
|
|
|
|
|
|
:return: The response.
|
|
|
|
"""
|
2022-11-26 15:59:17 +08:00
|
|
|
return f"Hello, {flask_login.current_user.get_id()}! #2"
|
2022-11-23 15:08:30 +08:00
|
|
|
|
2022-11-25 05:40:27 +08:00
|
|
|
@app.post("/logout", endpoint="logout")
|
|
|
|
@flask_login.login_required
|
|
|
|
def logout() -> redirect:
|
|
|
|
"""Logs out the user.
|
|
|
|
|
|
|
|
:return: The response.
|
|
|
|
"""
|
|
|
|
auth.logout()
|
|
|
|
return redirect(request.form.get("next"))
|
|
|
|
|
2022-11-23 15:08:30 +08:00
|
|
|
return app
|
|
|
|
|
|
|
|
def test_auth(self) -> None:
|
|
|
|
"""Tests the authentication.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-06-08 17:28:35 +08:00
|
|
|
if not self.__has_flask_login:
|
2023-06-10 16:45:19 +08:00
|
|
|
self.skipTest(SKIPPED_NO_FLASK_LOGIN)
|
2022-11-23 22:02:51 +08:00
|
|
|
|
2022-11-24 20:46:45 +08:00
|
|
|
response: Response = self.client.get(self.app.url_for("admin-1"))
|
2022-11-23 15:08:30 +08:00
|
|
|
self.assertEqual(response.status_code, 401)
|
|
|
|
response = self.client.get(
|
2022-11-24 20:46:45 +08:00
|
|
|
self.app.url_for("admin-1"), digest_auth=(_USERNAME, _PASSWORD))
|
2022-11-23 15:08:30 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.data.decode("UTF-8"),
|
|
|
|
f"Hello, {_USERNAME}! #1")
|
2022-11-24 20:46:45 +08:00
|
|
|
response: Response = self.client.get(self.app.url_for("admin-2"))
|
2022-11-23 15:08:30 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.data.decode("UTF-8"),
|
|
|
|
f"Hello, {_USERNAME}! #2")
|
2023-06-08 17:28:35 +08:00
|
|
|
self.assertEqual(self.__user.visits, 1)
|
2022-11-24 18:53:21 +08:00
|
|
|
|
2022-11-24 20:55:49 +08:00
|
|
|
def test_stale_opaque(self) -> None:
|
|
|
|
"""Tests the stale and opaque value.
|
2022-11-24 18:53:21 +08:00
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-06-08 17:28:35 +08:00
|
|
|
if not self.__has_flask_login:
|
2023-06-10 16:45:19 +08:00
|
|
|
self.skipTest(SKIPPED_NO_FLASK_LOGIN)
|
2022-11-24 20:46:45 +08:00
|
|
|
|
|
|
|
admin_uri: str = self.app.url_for("admin-1")
|
|
|
|
response: Response
|
|
|
|
www_authenticate: WWWAuthenticate
|
|
|
|
auth_data: Authorization
|
|
|
|
|
2022-11-24 21:18:43 +08:00
|
|
|
response = super(Client, self.client).get(admin_uri)
|
2022-11-24 18:53:21 +08:00
|
|
|
self.assertEqual(response.status_code, 401)
|
2022-11-24 20:46:45 +08:00
|
|
|
www_authenticate = response.www_authenticate
|
2022-11-24 18:53:21 +08:00
|
|
|
self.assertEqual(www_authenticate.type, "digest")
|
2023-05-03 06:56:31 +08:00
|
|
|
self.assertIsNone(www_authenticate.get("stale"))
|
2022-11-24 20:55:49 +08:00
|
|
|
opaque: str = www_authenticate.opaque
|
2022-11-24 18:53:21 +08:00
|
|
|
|
|
|
|
if hasattr(g, "_login_user"):
|
|
|
|
delattr(g, "_login_user")
|
|
|
|
www_authenticate.nonce = "bad"
|
2022-11-24 20:46:45 +08:00
|
|
|
auth_data = Client.make_authorization(
|
|
|
|
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
2022-11-24 21:18:43 +08:00
|
|
|
response = super(Client, self.client).get(admin_uri, auth=auth_data)
|
2022-11-24 18:53:21 +08:00
|
|
|
self.assertEqual(response.status_code, 401)
|
|
|
|
www_authenticate = response.www_authenticate
|
2023-05-03 06:56:31 +08:00
|
|
|
self.assertEqual(www_authenticate.get("stale"), "TRUE")
|
2022-11-24 20:55:49 +08:00
|
|
|
self.assertEqual(www_authenticate.opaque, opaque)
|
2022-11-24 18:53:21 +08:00
|
|
|
|
|
|
|
if hasattr(g, "_login_user"):
|
|
|
|
delattr(g, "_login_user")
|
|
|
|
auth_data = Client.make_authorization(
|
2022-11-24 20:46:45 +08:00
|
|
|
www_authenticate, admin_uri, _USERNAME, _PASSWORD + "2")
|
2022-11-24 21:18:43 +08:00
|
|
|
response = super(Client, self.client).get(admin_uri, auth=auth_data)
|
2022-11-24 18:53:21 +08:00
|
|
|
self.assertEqual(response.status_code, 401)
|
|
|
|
www_authenticate = response.www_authenticate
|
2023-05-03 06:56:31 +08:00
|
|
|
self.assertEqual(www_authenticate.get("stale"), "FALSE")
|
2022-11-24 20:55:49 +08:00
|
|
|
self.assertEqual(www_authenticate.opaque, opaque)
|
2022-11-24 18:53:21 +08:00
|
|
|
|
|
|
|
if hasattr(g, "_login_user"):
|
|
|
|
delattr(g, "_login_user")
|
|
|
|
auth_data = Client.make_authorization(
|
2022-11-24 20:46:45 +08:00
|
|
|
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
2022-11-24 21:18:43 +08:00
|
|
|
response = super(Client, self.client).get(admin_uri, auth=auth_data)
|
2022-11-24 18:53:21 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2022-11-25 05:40:27 +08:00
|
|
|
|
|
|
|
def test_logout(self) -> None:
|
|
|
|
"""Tests the logging out.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-06-08 17:28:35 +08:00
|
|
|
if not self.__has_flask_login:
|
2023-06-10 16:45:19 +08:00
|
|
|
self.skipTest(SKIPPED_NO_FLASK_LOGIN)
|
2022-12-03 11:56:40 +08:00
|
|
|
|
2022-11-25 05:40:27 +08:00
|
|
|
admin_uri: str = self.app.url_for("admin-1")
|
|
|
|
logout_uri: str = self.app.url_for("logout")
|
|
|
|
response: Response
|
|
|
|
|
|
|
|
response = self.client.get(admin_uri)
|
|
|
|
self.assertEqual(response.status_code, 401)
|
|
|
|
|
|
|
|
response = self.client.get(admin_uri,
|
|
|
|
digest_auth=(_USERNAME, _PASSWORD))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
response = self.client.get(admin_uri)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
response = self.client.post(logout_uri, data={"next": admin_uri})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.location, admin_uri)
|
|
|
|
|
|
|
|
response = self.client.get(admin_uri)
|
|
|
|
self.assertEqual(response.status_code, 401)
|
|
|
|
|
|
|
|
response = self.client.get(admin_uri,
|
|
|
|
digest_auth=(_USERNAME, _PASSWORD))
|
|
|
|
self.assertEqual(response.status_code, 401)
|
|
|
|
|
|
|
|
response = self.client.get(admin_uri,
|
|
|
|
digest_auth=(_USERNAME, _PASSWORD))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
response = self.client.get(admin_uri)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2023-06-08 17:28:35 +08:00
|
|
|
self.assertEqual(self.__user.visits, 2)
|
2022-12-29 23:44:02 +08:00
|
|
|
|
|
|
|
def test_disabled(self) -> None:
|
|
|
|
"""Tests the disabled user.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-06-08 17:28:35 +08:00
|
|
|
if not self.__has_flask_login:
|
2023-06-10 16:45:19 +08:00
|
|
|
self.skipTest(SKIPPED_NO_FLASK_LOGIN)
|
2022-12-29 23:44:02 +08:00
|
|
|
|
|
|
|
response: Response
|
|
|
|
|
2023-06-08 17:28:35 +08:00
|
|
|
self.__user.is_active = False
|
2022-12-29 23:44:02 +08:00
|
|
|
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)
|
|
|
|
|
2023-06-08 17:28:35 +08:00
|
|
|
self.__user.is_active = True
|
2022-12-29 23:44:02 +08:00
|
|
|
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)
|
|
|
|
|
2023-06-08 17:28:35 +08:00
|
|
|
self.__user.is_active = False
|
2022-12-29 23:44:02 +08:00
|
|
|
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)
|