2022-11-23 15:08:30 +08:00
|
|
|
# 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
|
|
|
|
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2022-11-23 20:36:41 +08:00
|
|
|
app: Flask = Flask(__name__)
|
|
|
|
app.config.from_mapping({
|
|
|
|
"SECRET_KEY": token_urlsafe(32),
|
|
|
|
"TESTING": True
|
|
|
|
})
|
|
|
|
app.test_client_class = Client
|
|
|
|
|
2022-11-23 15:08:30 +08:00
|
|
|
auth: DigestAuth = DigestAuth(realm=_REALM)
|
2022-11-25 06:14:57 +08:00
|
|
|
auth.init_app(app)
|
2022-11-23 15:08:30 +08:00
|
|
|
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
|
|
|
|
|
2022-11-24 20:46:45 +08:00
|
|
|
@app.get("/admin-1/auth", endpoint="admin-1")
|
2022-11-23 15:08:30 +08:00
|
|
|
@auth.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.
|
|
|
|
"""
|
|
|
|
return f"Hello, {g.user.username}! #1"
|
|
|
|
|
2022-11-24 20:46:45 +08:00
|
|
|
@app.get("/admin-2/auth", endpoint="admin-2")
|
2022-11-23 15:08:30 +08:00
|
|
|
@auth.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.
|
|
|
|
"""
|
|
|
|
return f"Hello, {g.user.username}! #2"
|
|
|
|
|
2022-11-25 05:40:27 +08:00
|
|
|
@app.post("/logout", endpoint="logout")
|
|
|
|
@auth.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.
|
|
|
|
"""
|
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")
|
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.
|
|
|
|
"""
|
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")
|
|
|
|
self.assertEqual(www_authenticate.stale, None)
|
2022-11-24 20:55:49 +08:00
|
|
|
opaque: str = www_authenticate.opaque
|
2022-11-24 18:53:21 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
self.assertEqual(www_authenticate.stale, True)
|
2022-11-24 20:55:49 +08:00
|
|
|
self.assertEqual(www_authenticate.opaque, opaque)
|
2022-11-24 18:53:21 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
self.assertEqual(www_authenticate.stale, False)
|
2022-11-24 20:55:49 +08:00
|
|
|
self.assertEqual(www_authenticate.opaque, opaque)
|
2022-11-24 18:53:21 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
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)
|