Compare commits
50 Commits
0a69f5d3c1
...
v0.2.1
Author | SHA1 | Date | |
---|---|---|---|
495e9a9785 | |||
cbbd2248f0 | |||
2028cb1362 | |||
7e71115844 | |||
491da61a79 | |||
bbaebbc80d | |||
0dfdf70c45 | |||
0432561b21 | |||
3709cb4d66 | |||
9d0d0b2686 | |||
8c98d35934 | |||
7db38c7eae | |||
9616fb3ddc | |||
f473db29a8 | |||
b39e9b1321 | |||
f3b525d715 | |||
0f3694ba05 | |||
2425d99492 | |||
be163d35fb | |||
2aaaa9f47f | |||
cb3e313e21 | |||
6f49a180e3 | |||
af8c3a484c | |||
65c3322ecc | |||
cb5cfaf7d4 | |||
dda8472a76 | |||
177f549786 | |||
ff8ada129d | |||
ccec1365bf | |||
78514a8f17 | |||
3dcc409bef | |||
2156aa710f | |||
a43c6aea89 | |||
8e29c91f92 | |||
8ec1e6fd02 | |||
b37861eccc | |||
f2e51b46cb | |||
387f86ef8c | |||
1e43206ee7 | |||
fa943b4831 | |||
c748fcdb16 | |||
5e5d929676 | |||
a7e2f3296f | |||
515098c32a | |||
ca22b9731c | |||
af7af3943a | |||
2770e1cc12 | |||
292d0aaf09 | |||
bf15bcf1f1 | |||
606059a547 |
9
.gitignore
vendored
9
.gitignore
vendored
@ -23,10 +23,13 @@ dist
|
||||
.pytest_cache
|
||||
venv
|
||||
|
||||
flask_session
|
||||
instance
|
||||
|
||||
.DS_Store
|
||||
.idea
|
||||
|
||||
instance
|
||||
flask_session
|
||||
|
||||
.scannerwork
|
||||
sonar-project.properties
|
||||
|
||||
excludes
|
||||
|
416
README.rst
416
README.rst
@ -6,16 +6,423 @@ Flask HTTP Digest Authentication
|
||||
Description
|
||||
===========
|
||||
|
||||
*Flask-Digest-Auth* is an HTTP Digest Authentication implementation
|
||||
*Flask-Digest-Auth* is an `HTTP Digest Authentication`_ implementation
|
||||
for Flask_ applications. It authenticates the user for the protected
|
||||
views. It works with Flask-Login_, so that log in protection can be
|
||||
separated with the log in mechanism. You can write Flask modules that
|
||||
work with different log in mechanisms.
|
||||
views.
|
||||
|
||||
HTTP Digest Authentication is specified in `RFC 2617`_.
|
||||
|
||||
|
||||
Why HTTP Digest Authentication?
|
||||
-------------------------------
|
||||
|
||||
HTTP Digest Authentication has the advantage that it does not send the
|
||||
actual password to the server, which greatly enhances the security.
|
||||
It uses the challenge-response authentication scheme. The client
|
||||
returns the response calculated from the challenge and the password,
|
||||
but not the original password.
|
||||
|
||||
Log in forms has the advantage of freedom, in the senses of both the
|
||||
visual design and the actual implementation. You may implement your
|
||||
own challenge-response log in form, but then you are reinventing the
|
||||
wheels. If a pretty log in form is not critical to your project, HTTP
|
||||
Digest Authentication should be a good choice.
|
||||
|
||||
Flask-Digest-Auth works with Flask-Login_. Log in protection can be
|
||||
separated with the authentication mechanism. You can create protected
|
||||
Flask modules without knowing the actual authentication mechanisms.
|
||||
|
||||
|
||||
Features
|
||||
--------
|
||||
|
||||
There are a couple of Flask HTTP digest authentication
|
||||
implementations. Flask-Digest-Auth has the following features:
|
||||
|
||||
|
||||
Flask-Login Integration
|
||||
#######################
|
||||
|
||||
Flask-Digest-Auth features Flask-Login integration. The views
|
||||
can be totally independent with the actual authentication mechanism.
|
||||
You can write a Flask module that requires log in, without specify
|
||||
the actual authentication mechanism. The application can specify
|
||||
either HTTP Digest Authentication, or the log in forms, as needed.
|
||||
|
||||
|
||||
Session Integration
|
||||
###################
|
||||
|
||||
Flask-Digest-Auth features session integration. The user log in
|
||||
is remembered in the session. The authentication information is not
|
||||
requested again. This is different to the practice of the HTTP Digest
|
||||
Authentication, but is convenient for the log in accounting.
|
||||
|
||||
|
||||
Log Out Support
|
||||
###############
|
||||
|
||||
Flask-Digest-Auth supports log out. The user will be prompted for
|
||||
new username and password.
|
||||
|
||||
|
||||
Log In Bookkeeping
|
||||
##################
|
||||
|
||||
You can register a callback to run when the user logs in.
|
||||
|
||||
|
||||
.. _HTTP Digest Authentication: https://en.wikipedia.org/wiki/Digest_access_authentication
|
||||
.. _RFC 2617: https://www.rfc-editor.org/rfc/rfc2617
|
||||
.. _Flask: https://flask.palletsprojects.com
|
||||
.. _Flask-Login: https://flask-login.readthedocs.io
|
||||
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
You can install Flask-Digest-Auth with ``pip``:
|
||||
|
||||
::
|
||||
|
||||
pip install Flask-Digest-Auth
|
||||
|
||||
You may also install the latest source from the
|
||||
`Flask-Digest-Auth GitHub repository`_.
|
||||
|
||||
::
|
||||
|
||||
git clone git@github.com:imacat/flask-digest-auth.git
|
||||
cd flask-digest-auth
|
||||
pip install .
|
||||
|
||||
.. _Flask-Digest-Auth GitHub repository: https://github.com/imacat/flask-digest-auth
|
||||
|
||||
|
||||
Flask-Digest-Auth Alone
|
||||
=======================
|
||||
|
||||
Flask-Digest-Auth can authenticate the users alone.
|
||||
|
||||
The currently logged-in user can be retrieved at ``g.user``, if any.
|
||||
|
||||
|
||||
Example for Simple Applications with Flask-Digest-Auth Alone
|
||||
------------------------------------------------------------
|
||||
|
||||
In your ``my_app.py``:
|
||||
|
||||
::
|
||||
|
||||
from flask import Flask, request, redirect
|
||||
from flask_digest_auth import DigestAuth
|
||||
|
||||
app: flask = Flask(__name__)
|
||||
... (Configure the Flask application) ...
|
||||
|
||||
auth: DigestAuth = DigestAuth(realm="Admin")
|
||||
auth.init_app(app)
|
||||
|
||||
@auth.register_get_password
|
||||
def get_password_hash(username: str) -> t.Optional[str]:
|
||||
... (Load the password hash) ...
|
||||
|
||||
@auth.register_get_user
|
||||
def get_user(username: str) -> t.Optional[t.Any]:
|
||||
... (Load the user) ...
|
||||
|
||||
@app.get("/admin")
|
||||
@auth.login_required
|
||||
def admin():
|
||||
return f"Hello, {g.user.username}!"
|
||||
|
||||
@app.post("/logout")
|
||||
@auth.login_required
|
||||
def logout():
|
||||
auth.logout()
|
||||
return redirect(request.form.get("next"))
|
||||
|
||||
|
||||
Example for Larger Applications with ``create_app()`` with Flask-Digest-Auth Alone
|
||||
----------------------------------------------------------------------------------
|
||||
|
||||
In your ``my_app/__init__.py``:
|
||||
|
||||
::
|
||||
|
||||
from flask import Flask
|
||||
from flask_digest_auth import DigestAuth
|
||||
|
||||
auth: DigestAuth = DigestAuth()
|
||||
|
||||
def create_app(test_config = None) -> Flask:
|
||||
app: flask = Flask(__name__)
|
||||
... (Configure the Flask application) ...
|
||||
|
||||
auth.realm = app.config["REALM"]
|
||||
auth.init_app(app)
|
||||
|
||||
@auth.register_get_password
|
||||
def get_password_hash(username: str) -> t.Optional[str]:
|
||||
... (Load the password hash) ...
|
||||
|
||||
@auth.register_get_user
|
||||
def get_user(username: str) -> t.Optional[t.Any]:
|
||||
... (Load the user) ...
|
||||
|
||||
return app
|
||||
|
||||
In your ``my_app/views.py``:
|
||||
|
||||
::
|
||||
|
||||
from my_app import auth
|
||||
from flask import Flask, Blueprint, request, redirect
|
||||
|
||||
bp = Blueprint("admin", __name__, url_prefix="/admin")
|
||||
|
||||
@bp.get("/admin")
|
||||
@auth.login_required
|
||||
def admin():
|
||||
return f"Hello, {g.user.username}!"
|
||||
|
||||
@app.post("/logout")
|
||||
@auth.login_required
|
||||
def logout():
|
||||
auth.logout()
|
||||
return redirect(request.form.get("next"))
|
||||
|
||||
def init_app(app: Flask) -> None:
|
||||
app.register_blueprint(bp)
|
||||
|
||||
|
||||
Flask-Login Integration
|
||||
=======================
|
||||
|
||||
Flask-Digest-Auth can work with Flask-Login. You can write a Flask
|
||||
module that requires log in, without specifying the authentication
|
||||
mechanism. The Flask application can specify the actual
|
||||
authentication mechanism as it sees fit.
|
||||
|
||||
``login_manager.init_app(app)`` must be called before
|
||||
``auth.init_app(app)``.
|
||||
|
||||
The currently logged-in user can be retrieved at
|
||||
``flask_login.current_user``, if any.
|
||||
|
||||
|
||||
Example for Simple Applications with Flask-Login Integration
|
||||
------------------------------------------------------------
|
||||
|
||||
In your ``my_app.py``:
|
||||
|
||||
::
|
||||
|
||||
import flask_login
|
||||
from flask import Flask, request, redirect
|
||||
from flask_digest_auth import DigestAuth
|
||||
|
||||
app: flask = Flask(__name__)
|
||||
... (Configure the Flask application) ...
|
||||
|
||||
login_manager: flask_login.LoginManager = flask_login.LoginManager()
|
||||
login_manager.init_app(app)
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id: str) -> t.Optional[User]:
|
||||
... (Load the user with the username) ...
|
||||
|
||||
auth: DigestAuth = DigestAuth(realm="Admin")
|
||||
auth.init_app(app)
|
||||
|
||||
@auth.register_get_password
|
||||
def get_password_hash(username: str) -> t.Optional[str]:
|
||||
... (Load the password hash) ...
|
||||
|
||||
@app.get("/admin")
|
||||
@flask_login.login_required
|
||||
def admin():
|
||||
return f"Hello, {flask_login.current_user.get_id()}!"
|
||||
|
||||
@app.post("/logout")
|
||||
@flask_login.login_required
|
||||
def logout():
|
||||
auth.logout()
|
||||
# Do not call flask_login.logout_user()
|
||||
return redirect(request.form.get("next"))
|
||||
|
||||
|
||||
Example for Larger Applications with ``create_app()`` with Flask-Login Integration
|
||||
----------------------------------------------------------------------------------
|
||||
|
||||
In your ``my_app/__init__.py``:
|
||||
|
||||
::
|
||||
|
||||
from flask import Flask
|
||||
from flask_digest_auth import DigestAuth
|
||||
from flask_login import LoginManager
|
||||
|
||||
auth: DigestAuth = DigestAuth()
|
||||
|
||||
def create_app(test_config = None) -> Flask:
|
||||
app: flask = Flask(__name__)
|
||||
... (Configure the Flask application) ...
|
||||
|
||||
login_manager: LoginManager = LoginManager()
|
||||
login_manager.init_app(app)
|
||||
|
||||
@login_manager.user_loader
|
||||
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
|
||||
def get_password_hash(username: str) -> t.Optional[str]:
|
||||
... (Load the password hash) ...
|
||||
|
||||
return app
|
||||
|
||||
In your ``my_app/views.py``:
|
||||
|
||||
::
|
||||
|
||||
import flask_login
|
||||
from flask import Flask, Blueprint, request, redirect
|
||||
from my_app import auth
|
||||
|
||||
bp = Blueprint("admin", __name__, url_prefix="/admin")
|
||||
|
||||
@bp.get("/admin")
|
||||
@flask_login.login_required
|
||||
def admin():
|
||||
return f"Hello, {flask_login.current_user.get_id()}!"
|
||||
|
||||
@app.post("/logout")
|
||||
@flask_login.login_required
|
||||
def logout():
|
||||
auth.logout()
|
||||
# Do not call flask_login.logout_user()
|
||||
return redirect(request.form.get("next"))
|
||||
|
||||
def init_app(app: Flask) -> None:
|
||||
app.register_blueprint(bp)
|
||||
|
||||
The views only depend on Flask-Login, but not the actual
|
||||
authentication mechanism. You can change the actual authentication
|
||||
mechanism without changing the views.
|
||||
|
||||
|
||||
Setting the Password Hash
|
||||
=========================
|
||||
|
||||
The password hash of the HTTP Digest Authentication is composed of the
|
||||
realm, the username, and the password. Example for setting the
|
||||
password:
|
||||
|
||||
::
|
||||
|
||||
from flask_digest_auth import make_password_hash
|
||||
|
||||
user.password = make_password_hash(realm, username, password)
|
||||
|
||||
The username is part of the hash. If the user changes their username,
|
||||
you need to ask their password, to generate and store the new password
|
||||
hash.
|
||||
|
||||
|
||||
Log Out
|
||||
=======
|
||||
|
||||
Call ``auth.logout()`` when the user wants to log out.
|
||||
Besides the usual log out routine, ``auth.logout()`` actually causes
|
||||
the next browser automatic authentication to fail, forcing the browser
|
||||
to ask the user for the username and password again.
|
||||
|
||||
|
||||
Log In Bookkeeping
|
||||
==================
|
||||
|
||||
You can register a callback to run when the user logs in, for ex.,
|
||||
logging the log in event, adding the log in counter, etc.
|
||||
|
||||
::
|
||||
|
||||
@auth.register_on_login
|
||||
def on_login(user: User) -> None:
|
||||
user.visits = user.visits + 1
|
||||
|
||||
|
||||
Writing Tests
|
||||
=============
|
||||
|
||||
You can write tests with our test client that handles HTTP Digest
|
||||
Authentication.
|
||||
|
||||
Example for a unittest_ test case:
|
||||
|
||||
::
|
||||
|
||||
from flask import Flask
|
||||
from flask_digest_auth import Client
|
||||
from flask_testing import TestCase
|
||||
from my_app import create_app
|
||||
|
||||
class MyTestCase(TestCase):
|
||||
|
||||
def create_app(self):
|
||||
app: Flask = create_app({
|
||||
"SECRET_KEY": token_urlsafe(32),
|
||||
"TESTING": True
|
||||
})
|
||||
app.test_client_class = Client
|
||||
return app
|
||||
|
||||
def test_admin(self):
|
||||
response = self.client.get("/admin")
|
||||
self.assertEqual(response.status_code, 401)
|
||||
response = self.client.get(
|
||||
"/admin", digest_auth=("my_name", "my_pass"))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
|
||||
Example for a pytest_ test:
|
||||
|
||||
::
|
||||
|
||||
import pytest
|
||||
from flask import Flask
|
||||
from flask_digest_auth import Client
|
||||
from my_app import create_app
|
||||
|
||||
@pytest.fixture()
|
||||
def app():
|
||||
app: Flask = create_app({
|
||||
"SECRET_KEY": token_urlsafe(32),
|
||||
"TESTING": True
|
||||
})
|
||||
app.test_client_class = Client
|
||||
yield app
|
||||
|
||||
@pytest.fixture()
|
||||
def client(app):
|
||||
return app.test_client()
|
||||
|
||||
def test_admin(app: Flask, client: Client):
|
||||
with app.app_context():
|
||||
response = self.client.get("/admin")
|
||||
assert response.status_code == 401
|
||||
response = self.client.get(
|
||||
"/admin", digest_auth=("my_name", "my_pass"))
|
||||
assert response.status_code == 200
|
||||
|
||||
.. _unittest: https://docs.python.org/3/library/unittest.html
|
||||
.. _pytest: https://pytest.org
|
||||
|
||||
|
||||
Copyright
|
||||
=========
|
||||
|
||||
@ -33,6 +440,7 @@ Copyright
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
|
||||
Authors
|
||||
=======
|
||||
|
||||
|
17
setup.cfg
17
setup.cfg
@ -17,32 +17,37 @@
|
||||
|
||||
[metadata]
|
||||
name = flask-digest-auth
|
||||
version = 0.0.0
|
||||
version = 0.2.1
|
||||
author = imacat
|
||||
author_email = imacat@mail.imacat.idv.tw
|
||||
description = The Flask HTTP Digest Authentication project.
|
||||
long_description = file: README.rst
|
||||
long_description_content_type = text/x-rst
|
||||
url = https://gitea.imacat.idv.tw/imacat/flask-digest-auth
|
||||
url = https://github.com/imacat/flask-digest-auth
|
||||
project_urls =
|
||||
Bug Tracker = https://gitea.imacat.idv.tw/imacat/flask-digest-auth/issues
|
||||
Bug Tracker = https://github.com/imacat/flask-digest-auth/issues
|
||||
classifiers =
|
||||
Programming Language :: Python :: 3
|
||||
License :: OSI Approved :: Apache Software License
|
||||
Operating System :: OS Independent
|
||||
Framework :: Flask
|
||||
Topic :: Office/Business :: Financial :: Accounting
|
||||
Topic :: System :: Systems Administration :: Authentication/Directory
|
||||
Intended Audience :: Developers
|
||||
|
||||
[options]
|
||||
package_dir =
|
||||
= src
|
||||
packages = find:
|
||||
python_requires = >=3.10
|
||||
python_requires = >=3.7
|
||||
install_requires =
|
||||
flask-login
|
||||
flask
|
||||
tests_require =
|
||||
unittest
|
||||
flask-testing
|
||||
|
||||
[options.packages.find]
|
||||
where = src
|
||||
|
||||
[options.extras_require]
|
||||
flask_login =
|
||||
flask-login
|
||||
|
@ -20,5 +20,4 @@
|
||||
"""
|
||||
from flask_digest_auth.algo import make_password_hash, calc_response
|
||||
from flask_digest_auth.auth import DigestAuth
|
||||
from flask_digest_auth.flask_login import init_login_manager
|
||||
from flask_digest_auth.test import Client
|
||||
|
@ -60,68 +60,53 @@ def calc_response(
|
||||
algorithm, when the body is missing with the auth-int qop, or when the
|
||||
cnonce or nc is missing with the auth or auth-int qop.
|
||||
"""
|
||||
ha1: str = __calc_ha1(password_hash, nonce, algorithm, cnonce)
|
||||
ha2: str = __calc_ha2(method, uri, qop, body)
|
||||
if qop is None:
|
||||
return md5(f"{ha1}:{nonce}:{ha2}".encode("utf8")).hexdigest()
|
||||
if qop == "auth" or qop == "auth-int":
|
||||
if cnonce is None:
|
||||
raise UnauthorizedException(
|
||||
f"Missing \"cnonce\" with the qop=\"{qop}\"")
|
||||
if nc is None:
|
||||
raise UnauthorizedException(
|
||||
f"Missing \"nc\" with the qop=\"{qop}\"")
|
||||
return md5(f"{ha1}:{nonce}:{nc}:{cnonce}:{qop}:{ha2}".encode("utf8"))\
|
||||
.hexdigest()
|
||||
if cnonce is None:
|
||||
raise UnauthorizedException(
|
||||
f"Unsupported qop=\"{qop}\"")
|
||||
|
||||
def validate_required(field: t.Optional[str], error: str) -> None:
|
||||
"""Validates a required field.
|
||||
|
||||
def __calc_ha1(password_hash: str, nonce: str,
|
||||
algorithm: t.Optional[t.Literal["MD5", "MD5-sess"]] = None,
|
||||
cnonce: t.Optional[str] = None) -> str:
|
||||
:param field: The field that is required.
|
||||
:param error: The error message.
|
||||
:return: None.
|
||||
"""
|
||||
if field is None:
|
||||
raise UnauthorizedException(error)
|
||||
|
||||
def calc_ha1() -> str:
|
||||
"""Calculates and returns the first hash.
|
||||
|
||||
:param password_hash: The password hash for the HTTP digest authentication.
|
||||
:param nonce: The nonce.
|
||||
:param algorithm: The algorithm, either "MD5", "MD5-sess", or None.
|
||||
:param cnonce: The client nonce. It must be provided when the algorithm is
|
||||
"MD5-sess".
|
||||
:return: The first hash.
|
||||
:raise UnauthorizedException: When the cnonce is missing with the MD5-sess
|
||||
algorithm.
|
||||
"""
|
||||
if algorithm is None or algorithm == "MD5":
|
||||
return password_hash
|
||||
if algorithm == "MD5-sess":
|
||||
if cnonce is None:
|
||||
raise UnauthorizedException(
|
||||
f"Missing \"cnonce\" with algorithm=\"{algorithm}\"")
|
||||
return md5(f"{password_hash}:{nonce}:{cnonce}".encode("utf8"))\
|
||||
validate_required(
|
||||
cnonce, f"Missing \"cnonce\" with algorithm=\"{algorithm}\"")
|
||||
return md5(f"{password_hash}:{nonce}:{cnonce}".encode("utf8")) \
|
||||
.hexdigest()
|
||||
raise UnauthorizedException(
|
||||
f"Unsupported algorithm=\"{algorithm}\"")
|
||||
# algorithm is None or algorithm == "MD5"
|
||||
return password_hash
|
||||
|
||||
|
||||
def __calc_ha2(method: str, uri: str,
|
||||
qop: t.Optional[t.Literal["auth", "auth-int"]] = None,
|
||||
body: t.Optional[bytes] = None) -> str:
|
||||
def calc_ha2() -> str:
|
||||
"""Calculates the second hash.
|
||||
|
||||
:param method: The request method.
|
||||
:param uri: The request URI.
|
||||
:param qop: The quality of protection, either "auth", "auth-int" or None.
|
||||
:param body: The request body. It must be provided when the quality of
|
||||
protection is "auth-int".
|
||||
:return: The second hash.
|
||||
:raise UnauthorizedException: When the body is missing with qop="auth-int".
|
||||
:raise UnauthorizedException: When the body is missing with
|
||||
qop="auth-int".
|
||||
"""
|
||||
if qop is None or qop == "auth":
|
||||
return md5(f"{method}:{uri}".encode("utf8")).hexdigest()
|
||||
if qop == "auth-int":
|
||||
if body is None:
|
||||
raise UnauthorizedException(f"Missing \"body\" with qop=\"{qop}\"")
|
||||
return md5(f"{method}:{uri}:{md5(body).hexdigest()}".encode("utf8"))\
|
||||
validate_required(body, f"Missing \"body\" with qop=\"{qop}\"")
|
||||
return md5(
|
||||
f"{method}:{uri}:{md5(body).hexdigest()}".encode("utf8")) \
|
||||
.hexdigest()
|
||||
raise UnauthorizedException(f"Unsupported qop=\"{qop}\"")
|
||||
# qop is None or qop == "auth"
|
||||
return md5(f"{method}:{uri}".encode("utf8")).hexdigest()
|
||||
|
||||
ha1: str = calc_ha1()
|
||||
ha2: str = calc_ha2()
|
||||
if qop == "auth" or qop == "auth-int":
|
||||
validate_required(cnonce, f"Missing \"cnonce\" with the qop=\"{qop}\"")
|
||||
validate_required(nc, f"Missing \"nc\" with the qop=\"{qop}\"")
|
||||
return md5(f"{ha1}:{nonce}:{nc}:{cnonce}:{qop}:{ha2}".encode("utf8"))\
|
||||
.hexdigest()
|
||||
# qop is None
|
||||
return md5(f"{ha1}:{nonce}:{ha2}".encode("utf8")).hexdigest()
|
||||
|
@ -24,10 +24,9 @@ from __future__ import annotations
|
||||
import sys
|
||||
import typing as t
|
||||
from functools import wraps
|
||||
from random import random
|
||||
from secrets import token_urlsafe
|
||||
from secrets import token_urlsafe, randbits
|
||||
|
||||
from flask import g, request, Response, session, abort
|
||||
from flask import g, request, Response, session, abort, Flask, Request
|
||||
from itsdangerous import URLSafeTimedSerializer, BadData
|
||||
from werkzeug.datastructures import Authorization
|
||||
|
||||
@ -35,6 +34,50 @@ from flask_digest_auth.algo import calc_response
|
||||
from flask_digest_auth.exception import UnauthorizedException
|
||||
|
||||
|
||||
class BasePasswordHashGetter:
|
||||
"""The base password hash getter."""
|
||||
|
||||
@staticmethod
|
||||
def __call__(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.
|
||||
:raise UnboundLocalError: When the password hash getter function is
|
||||
not registered yet.
|
||||
"""
|
||||
raise UnboundLocalError("The function to return the password hash"
|
||||
" was not registered yet.")
|
||||
|
||||
|
||||
class BaseUserGetter:
|
||||
"""The base user getter."""
|
||||
|
||||
@staticmethod
|
||||
def __call__(username: str) -> t.Optional[t.Any]:
|
||||
"""Returns a user.
|
||||
|
||||
:param username: The username.
|
||||
:return: The user, or None if the user does not exist.
|
||||
:raise UnboundLocalError: When the user getter function is not
|
||||
registered yet.
|
||||
"""
|
||||
raise UnboundLocalError("The function to return the user"
|
||||
" was not registered yet.")
|
||||
|
||||
|
||||
class BaseOnLogInCallback:
|
||||
"""The base callback when the user logs in."""
|
||||
|
||||
@staticmethod
|
||||
def __call__(user: t.Any) -> None:
|
||||
"""Runs the callback when the user logs in.
|
||||
|
||||
:param user: The logged-in user.
|
||||
:return: None.
|
||||
"""
|
||||
|
||||
|
||||
class DigestAuth:
|
||||
"""The HTTP digest authentication."""
|
||||
|
||||
@ -51,9 +94,11 @@ class DigestAuth:
|
||||
self.use_opaque: bool = True
|
||||
self.domain: t.List[str] = []
|
||||
self.qop: t.List[str] = ["auth", "auth-int"]
|
||||
self.__get_password_hash: t.Callable[[str], t.Optional[str]] \
|
||||
= lambda x: None
|
||||
self.__get_user: t.Callable[[str], t.Optional] = lambda x: None
|
||||
self.app: t.Optional[Flask] = None
|
||||
self.__get_password_hash: BasePasswordHashGetter \
|
||||
= BasePasswordHashGetter()
|
||||
self.__get_user: BaseUserGetter = BaseUserGetter()
|
||||
self.__on_login: BaseOnLogInCallback = BaseOnLogInCallback()
|
||||
|
||||
def login_required(self, view) -> t.Callable:
|
||||
"""The view decorator for HTTP digest authentication.
|
||||
@ -65,6 +110,37 @@ class DigestAuth:
|
||||
class NoLogInException(Exception):
|
||||
"""The exception thrown when the user is not authorized."""
|
||||
|
||||
def get_logged_in_user() -> t.Any:
|
||||
"""Returns the currently logged-in user.
|
||||
|
||||
:return: The currently logged-in user.
|
||||
:raise NoLogInException: When the user is not logged in.
|
||||
"""
|
||||
if "user" not in session:
|
||||
raise NoLogInException
|
||||
user: t.Optional[t.Any] = self.__get_user(session["user"])
|
||||
if user is None:
|
||||
del session["user"]
|
||||
raise NoLogInException
|
||||
return user
|
||||
|
||||
def auth_user(state: AuthState) -> t.Any:
|
||||
"""Authenticates a user.
|
||||
|
||||
:param state: The authentication state.
|
||||
:return: The user.
|
||||
:raise UnauthorizedException: When the authentication fails.
|
||||
"""
|
||||
authorization: Authorization = request.authorization
|
||||
if authorization is None:
|
||||
raise UnauthorizedException
|
||||
if authorization.type != "digest":
|
||||
raise UnauthorizedException(
|
||||
"Not an HTTP digest authorization")
|
||||
self.authenticate(state)
|
||||
session["user"] = authorization.username
|
||||
return self.__get_user(authorization.username)
|
||||
|
||||
@wraps(view)
|
||||
def login_required_view(*args, **kwargs) -> t.Any:
|
||||
"""The login-protected view.
|
||||
@ -74,25 +150,15 @@ class DigestAuth:
|
||||
:return: The response.
|
||||
"""
|
||||
try:
|
||||
if "user" not in session:
|
||||
raise NoLogInException
|
||||
user: t.Optional[t.Any] = self.__get_user(session["user"])
|
||||
if user is None:
|
||||
raise NoLogInException
|
||||
g.user = user
|
||||
g.user = get_logged_in_user()
|
||||
return view(*args, **kwargs)
|
||||
except NoLogInException:
|
||||
pass
|
||||
|
||||
state: AuthState = AuthState()
|
||||
authorization: Authorization = request.authorization
|
||||
try:
|
||||
if authorization is None:
|
||||
raise UnauthorizedException
|
||||
if authorization.type != "digest":
|
||||
raise UnauthorizedException(
|
||||
"Not an HTTP digest authorization")
|
||||
self.authenticate(state)
|
||||
session["user"] = authorization.username
|
||||
g.user = self.__get_user(authorization.username)
|
||||
g.user = auth_user(state)
|
||||
self.__on_login(g.user)
|
||||
return view(*args, **kwargs)
|
||||
except UnauthorizedException as e:
|
||||
if len(e.args) > 0:
|
||||
@ -112,6 +178,9 @@ class DigestAuth:
|
||||
:return: None.
|
||||
:raise UnauthorizedException: When the authentication failed.
|
||||
"""
|
||||
if "digest_auth_logout" in session:
|
||||
del session["digest_auth_logout"]
|
||||
raise UnauthorizedException("Logging out")
|
||||
authorization: Authorization = request.authorization
|
||||
if self.use_opaque:
|
||||
if authorization.opaque is None:
|
||||
@ -123,8 +192,8 @@ class DigestAuth:
|
||||
except BadData:
|
||||
raise UnauthorizedException("Invalid opaque")
|
||||
state.opaque = authorization.opaque
|
||||
password_hash: t.Optional[str] = self.__get_password_hash(
|
||||
authorization.username)
|
||||
password_hash: t.Optional[str] \
|
||||
= self.__get_password_hash(authorization.username)
|
||||
if password_hash is None:
|
||||
raise UnauthorizedException(
|
||||
f"No such user \"{authorization.username}\"")
|
||||
@ -153,11 +222,22 @@ class DigestAuth:
|
||||
:param state: The authorization state.
|
||||
:return: The WWW-Authenticate response header.
|
||||
"""
|
||||
opaque: t.Optional[str] = None if not self.use_opaque else \
|
||||
(state.opaque if state.opaque is not None
|
||||
else self.serializer.dumps(random(), salt="opaque"))
|
||||
|
||||
def get_opaque() -> t.Optional[str]:
|
||||
"""Returns the opaque value.
|
||||
|
||||
:return: The opaque value.
|
||||
"""
|
||||
if not self.use_opaque:
|
||||
return None
|
||||
if state.opaque is not None:
|
||||
return state.opaque
|
||||
return self.serializer.dumps(randbits(32), salt="opaque")
|
||||
|
||||
opaque: t.Optional[str] = get_opaque()
|
||||
nonce: str = self.serializer.dumps(
|
||||
random(), salt="nonce" if opaque is None else f"nonce-{opaque}")
|
||||
randbits(32),
|
||||
salt="nonce" if opaque is None else f"nonce-{opaque}")
|
||||
|
||||
header: str = f"Digest realm=\"{self.realm}\""
|
||||
if len(self.domain) > 0:
|
||||
@ -183,7 +263,20 @@ class DigestAuth:
|
||||
hash, or None if the user does not exist.
|
||||
:return: None.
|
||||
"""
|
||||
self.__get_password_hash = func
|
||||
|
||||
class PasswordHashGetter(BasePasswordHashGetter):
|
||||
"""The base password hash getter."""
|
||||
|
||||
@staticmethod
|
||||
def __call__(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 func(username)
|
||||
|
||||
self.__get_password_hash = PasswordHashGetter()
|
||||
|
||||
def register_get_user(self, func: t.Callable[[str], t.Optional[t.Any]])\
|
||||
-> None:
|
||||
@ -193,7 +286,111 @@ class DigestAuth:
|
||||
or None if the user does not exist.
|
||||
:return: None.
|
||||
"""
|
||||
self.__get_user = func
|
||||
|
||||
class UserGetter(BaseUserGetter):
|
||||
"""The user getter."""
|
||||
|
||||
@staticmethod
|
||||
def __call__(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 func(username)
|
||||
|
||||
self.__get_user = UserGetter()
|
||||
|
||||
def register_on_login(self, func: t.Callable[[t.Any], None]) -> None:
|
||||
"""Registers the callback when the user logs in.
|
||||
|
||||
:param func: The callback given the logged-in user.
|
||||
:return: None.
|
||||
"""
|
||||
|
||||
class OnLogInCallback:
|
||||
"""The callback when the user logs in."""
|
||||
|
||||
@staticmethod
|
||||
def __call__(user: t.Any) -> None:
|
||||
"""Runs the callback when the user logs in.
|
||||
|
||||
:param user: The logged-in user.
|
||||
:return: None.
|
||||
"""
|
||||
func(user)
|
||||
|
||||
self.__on_login = OnLogInCallback()
|
||||
|
||||
def init_app(self, app: Flask) -> None:
|
||||
"""Initializes the Flask application.
|
||||
|
||||
:param app: The Flask application.
|
||||
:return: None.
|
||||
"""
|
||||
app.digest_auth = self
|
||||
self.app = app
|
||||
|
||||
if hasattr(app, "login_manager"):
|
||||
from flask_login import LoginManager, login_user
|
||||
|
||||
login_manager: LoginManager = getattr(app, "login_manager")
|
||||
|
||||
@login_manager.unauthorized_handler
|
||||
def unauthorized() -> None:
|
||||
"""Handles when the user is unauthorized.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
response: Response = Response()
|
||||
response.status = 401
|
||||
response.headers["WWW-Authenticate"] \
|
||||
= self.make_response_header(g.digest_auth_state)
|
||||
abort(response)
|
||||
|
||||
@login_manager.request_loader
|
||||
def load_user_from_request(req: Request) -> t.Optional[t.Any]:
|
||||
"""Loads the user from the request header.
|
||||
|
||||
:param req: The request.
|
||||
:return: The authenticated user, or None if the
|
||||
authentication fails
|
||||
"""
|
||||
g.digest_auth_state = AuthState()
|
||||
authorization: Authorization = req.authorization
|
||||
try:
|
||||
if authorization is None:
|
||||
raise UnauthorizedException
|
||||
if authorization.type != "digest":
|
||||
raise UnauthorizedException(
|
||||
"Not an HTTP digest authorization")
|
||||
self.authenticate(g.digest_auth_state)
|
||||
user = login_manager.user_callback(
|
||||
authorization.username)
|
||||
login_user(user)
|
||||
self.__on_login(user)
|
||||
return user
|
||||
except UnauthorizedException as e:
|
||||
if str(e) != "":
|
||||
app.logger.warning(str(e))
|
||||
return None
|
||||
|
||||
def logout(self) -> None:
|
||||
"""Logs out the user.
|
||||
This actually causes the next authentication to fail, which forces
|
||||
the browser to ask the user for the username and password again.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
if "user" in session:
|
||||
del session["user"]
|
||||
try:
|
||||
if hasattr(self.app, "login_manager"):
|
||||
from flask_login import logout_user
|
||||
logout_user()
|
||||
except ModuleNotFoundError:
|
||||
pass
|
||||
session["digest_auth_logout"] = True
|
||||
|
||||
|
||||
class AuthState:
|
||||
|
@ -1,74 +0,0 @@
|
||||
# The Flask HTTP Digest Authentication Project.
|
||||
# Author: imacat@mail.imacat.idv.tw (imacat), 2022/11/14
|
||||
|
||||
# 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 Flask-Login integration.
|
||||
|
||||
"""
|
||||
|
||||
import typing as t
|
||||
|
||||
from flask import Response, abort, current_app, Request, g
|
||||
from flask_login import LoginManager, login_user
|
||||
from werkzeug.datastructures import Authorization
|
||||
|
||||
from flask_digest_auth.auth import DigestAuth, AuthState
|
||||
from flask_digest_auth.exception import UnauthorizedException
|
||||
|
||||
|
||||
def init_login_manager(auth: DigestAuth, login_manager: LoginManager) -> None:
|
||||
"""Initialize the login manager.
|
||||
|
||||
:param auth: The HTTP digest authentication.
|
||||
:param login_manager: The login manager from FlaskLogin.
|
||||
:return: None.
|
||||
"""
|
||||
|
||||
@login_manager.unauthorized_handler
|
||||
def unauthorized() -> None:
|
||||
"""Handles when the user is unauthorized.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
response: Response = Response()
|
||||
response.status = 401
|
||||
response.headers["WWW-Authenticate"] = auth.make_response_header(
|
||||
g.digest_auth_state)
|
||||
abort(response)
|
||||
|
||||
@login_manager.request_loader
|
||||
def load_user_from_request(request: Request) -> t.Optional[t.Any]:
|
||||
"""Loads the user from the request header.
|
||||
|
||||
:param request: The request.
|
||||
:return: The authenticated user, or None if the authentication fails
|
||||
"""
|
||||
g.digest_auth_state = AuthState()
|
||||
authorization: Authorization = request.authorization
|
||||
try:
|
||||
if authorization is None:
|
||||
raise UnauthorizedException
|
||||
if authorization.type != "digest":
|
||||
raise UnauthorizedException(
|
||||
"Not an HTTP digest authorization")
|
||||
auth.authenticate(g.digest_auth_state)
|
||||
user = login_manager.user_callback(authorization.username)
|
||||
login_user(user)
|
||||
return user
|
||||
except UnauthorizedException as e:
|
||||
if str(e) != "":
|
||||
current_app.logger.warning(str(e))
|
||||
return None
|
@ -49,15 +49,15 @@ class Client(WerkzeugClient):
|
||||
return response
|
||||
if hasattr(g, "_login_user"):
|
||||
delattr(g, "_login_user")
|
||||
auth_data: Authorization = _get_req_auth(
|
||||
auth_data: Authorization = self.__class__.make_authorization(
|
||||
www_authenticate, args[0], digest_auth[0], digest_auth[1])
|
||||
response = super(Client, self).open(*args, auth=auth_data, **kwargs)
|
||||
return response
|
||||
|
||||
|
||||
def _get_req_auth(www_authenticate: WWWAuthenticate, uri: str,
|
||||
@staticmethod
|
||||
def make_authorization(www_authenticate: WWWAuthenticate, uri: str,
|
||||
username: str, password: str) -> Authorization:
|
||||
"""Returns the request authorization from the response header.
|
||||
"""Composes and returns the request authorization.
|
||||
|
||||
:param www_authenticate: The WWW-Authenticate response.
|
||||
:param uri: The request URI.
|
||||
@ -66,8 +66,7 @@ def _get_req_auth(www_authenticate: WWWAuthenticate, uri: str,
|
||||
:return: The request authorization.
|
||||
"""
|
||||
qop: t.Optional[t.Literal["auth", "auth-int"]] = None
|
||||
if www_authenticate.qop is not None:
|
||||
if "auth" in www_authenticate.qop:
|
||||
if www_authenticate.qop is not None and "auth" in www_authenticate.qop:
|
||||
qop = "auth"
|
||||
|
||||
cnonce: t.Optional[str] = None
|
||||
@ -83,7 +82,8 @@ def _get_req_auth(www_authenticate: WWWAuthenticate, uri: str,
|
||||
password_hash=make_password_hash(www_authenticate.realm,
|
||||
username, password),
|
||||
nonce=www_authenticate.nonce, qop=qop,
|
||||
algorithm=www_authenticate.algorithm, cnonce=cnonce, nc=nc, body=None)
|
||||
algorithm=www_authenticate.algorithm, cnonce=cnonce, nc=nc,
|
||||
body=None)
|
||||
|
||||
data: t.Dict[str, str] = {
|
||||
"username": username, "realm": www_authenticate.realm,
|
||||
|
@ -20,10 +20,10 @@
|
||||
"""
|
||||
import typing as t
|
||||
from secrets import token_urlsafe
|
||||
from types import SimpleNamespace
|
||||
|
||||
from flask import Response, Flask, g
|
||||
from flask import Response, Flask, g, redirect, request
|
||||
from flask_testing import TestCase
|
||||
from werkzeug.datastructures import WWWAuthenticate, Authorization
|
||||
|
||||
from flask_digest_auth import DigestAuth, make_password_hash, Client
|
||||
|
||||
@ -32,6 +32,21 @@ _USERNAME: str = "Mufasa"
|
||||
_PASSWORD: str = "Circle Of Life"
|
||||
|
||||
|
||||
class User:
|
||||
"""A dummy user"""
|
||||
|
||||
def __init__(self, username: str, password: str):
|
||||
"""Constructs a dummy user.
|
||||
|
||||
:param username: The username.
|
||||
:param password: The clear-text password.
|
||||
"""
|
||||
self.username: str = username
|
||||
self.password_hash: str = make_password_hash(
|
||||
_REALM, username, password)
|
||||
self.visits: int = 0
|
||||
|
||||
|
||||
class AuthenticationTestCase(TestCase):
|
||||
"""The test case for the HTTP digest authentication."""
|
||||
|
||||
@ -40,9 +55,17 @@ class AuthenticationTestCase(TestCase):
|
||||
|
||||
:return: The Flask application.
|
||||
"""
|
||||
app: Flask = Flask(__name__)
|
||||
app.config.from_mapping({
|
||||
"SECRET_KEY": token_urlsafe(32),
|
||||
"TESTING": True
|
||||
})
|
||||
app.test_client_class = Client
|
||||
|
||||
auth: DigestAuth = DigestAuth(realm=_REALM)
|
||||
user_db: t.Dict[str, str] \
|
||||
= {_USERNAME: make_password_hash(_REALM, _USERNAME, _PASSWORD)}
|
||||
auth.init_app(app)
|
||||
self.user: User = User(_USERNAME, _PASSWORD)
|
||||
user_db: t.Dict[str, User] = {_USERNAME: self.user}
|
||||
|
||||
@auth.register_get_password
|
||||
def get_password_hash(username: str) -> t.Optional[str]:
|
||||
@ -51,7 +74,8 @@ class AuthenticationTestCase(TestCase):
|
||||
: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
|
||||
return user_db[username].password_hash if username in user_db \
|
||||
else None
|
||||
|
||||
@auth.register_get_user
|
||||
def get_user(username: str) -> t.Optional[t.Any]:
|
||||
@ -60,34 +84,45 @@ class AuthenticationTestCase(TestCase):
|
||||
: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
|
||||
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
|
||||
@auth.register_on_login
|
||||
def on_login(user: User):
|
||||
"""The callback when the user logs in.
|
||||
|
||||
@app.route("/login-required-1/auth", endpoint="auth-1")
|
||||
:param user: The logged-in user.
|
||||
:return: None.
|
||||
"""
|
||||
user.visits = user.visits + 1
|
||||
|
||||
@app.get("/admin-1/auth", endpoint="admin-1")
|
||||
@auth.login_required
|
||||
def login_required_1() -> str:
|
||||
"""The first dummy view.
|
||||
def admin_1() -> str:
|
||||
"""The first administration section.
|
||||
|
||||
:return: The response.
|
||||
"""
|
||||
return f"Hello, {g.user.username}! #1"
|
||||
|
||||
@app.route("/login-required-2/auth", endpoint="auth-2")
|
||||
@app.get("/admin-2/auth", endpoint="admin-2")
|
||||
@auth.login_required
|
||||
def login_required_2() -> str:
|
||||
"""The second dummy view.
|
||||
def admin_2() -> str:
|
||||
"""The second administration section.
|
||||
|
||||
:return: The response.
|
||||
"""
|
||||
return f"Hello, {g.user.username}! #2"
|
||||
|
||||
@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"))
|
||||
|
||||
return app
|
||||
|
||||
def test_auth(self) -> None:
|
||||
@ -95,14 +130,92 @@ class AuthenticationTestCase(TestCase):
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
response: Response = self.client.get(self.app.url_for("auth-1"))
|
||||
response: Response = self.client.get(self.app.url_for("admin-1"))
|
||||
self.assertEqual(response.status_code, 401)
|
||||
response = self.client.get(
|
||||
self.app.url_for("auth-1"), digest_auth=(_USERNAME, _PASSWORD))
|
||||
self.app.url_for("admin-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"))
|
||||
response: Response = self.client.get(self.app.url_for("admin-2"))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data.decode("UTF-8"),
|
||||
f"Hello, {_USERNAME}! #2")
|
||||
self.assertEqual(self.user.visits, 1)
|
||||
|
||||
def test_stale_opaque(self) -> None:
|
||||
"""Tests the stale and opaque value.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
admin_uri: str = self.app.url_for("admin-1")
|
||||
response: Response
|
||||
www_authenticate: WWWAuthenticate
|
||||
auth_data: Authorization
|
||||
|
||||
response = super(Client, self.client).get(admin_uri)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
www_authenticate = response.www_authenticate
|
||||
self.assertEqual(www_authenticate.type, "digest")
|
||||
self.assertEqual(www_authenticate.stale, None)
|
||||
opaque: str = www_authenticate.opaque
|
||||
|
||||
www_authenticate.nonce = "bad"
|
||||
auth_data = Client.make_authorization(
|
||||
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
||||
response = super(Client, self.client).get(admin_uri, auth=auth_data)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
www_authenticate = response.www_authenticate
|
||||
self.assertEqual(www_authenticate.stale, True)
|
||||
self.assertEqual(www_authenticate.opaque, opaque)
|
||||
|
||||
auth_data = Client.make_authorization(
|
||||
www_authenticate, admin_uri, _USERNAME, _PASSWORD + "2")
|
||||
response = super(Client, self.client).get(admin_uri, auth=auth_data)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
www_authenticate = response.www_authenticate
|
||||
self.assertEqual(www_authenticate.stale, False)
|
||||
self.assertEqual(www_authenticate.opaque, opaque)
|
||||
|
||||
auth_data = Client.make_authorization(
|
||||
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
||||
response = super(Client, self.client).get(admin_uri, auth=auth_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
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)
|
||||
self.assertEqual(self.user.visits, 2)
|
||||
|
@ -21,13 +21,11 @@
|
||||
import typing as t
|
||||
from secrets import token_urlsafe
|
||||
|
||||
import flask_login
|
||||
from flask import Response, Flask
|
||||
from flask_login import LoginManager
|
||||
from flask import Response, Flask, g, redirect, request
|
||||
from flask_testing import TestCase
|
||||
from werkzeug.datastructures import WWWAuthenticate, Authorization
|
||||
|
||||
from flask_digest_auth import DigestAuth, make_password_hash, Client, \
|
||||
init_login_manager
|
||||
from flask_digest_auth import DigestAuth, make_password_hash, Client
|
||||
|
||||
_REALM: str = "testrealm@host.com"
|
||||
_USERNAME: str = "Mufasa"
|
||||
@ -35,8 +33,18 @@ _PASSWORD: str = "Circle Of Life"
|
||||
|
||||
|
||||
class User:
|
||||
def __init__(self, username: str):
|
||||
"""A dummy user."""
|
||||
|
||||
def __init__(self, username: str, password: str):
|
||||
"""Constructs a dummy user.
|
||||
|
||||
:param username: The username.
|
||||
:param password: The clear-text password.
|
||||
"""
|
||||
self.username: str = username
|
||||
self.password_hash: str = make_password_hash(
|
||||
_REALM, username, password)
|
||||
self.visits: int = 0
|
||||
self.is_authenticated: bool = True
|
||||
self.is_active: bool = True
|
||||
self.is_anonymous: bool = False
|
||||
@ -53,15 +61,33 @@ class User:
|
||||
class FlaskLoginTestCase(TestCase):
|
||||
"""The test case with the Flask-Login integration."""
|
||||
|
||||
def create_app(self):
|
||||
def create_app(self) -> Flask:
|
||||
"""Creates the Flask application.
|
||||
|
||||
:return: The Flask application.
|
||||
"""
|
||||
app: Flask = Flask(__name__)
|
||||
app.config.from_mapping({
|
||||
"SECRET_KEY": token_urlsafe(32),
|
||||
"TESTING": True
|
||||
})
|
||||
app.test_client_class = Client
|
||||
|
||||
self.has_flask_login: bool = True
|
||||
try:
|
||||
import flask_login
|
||||
except ModuleNotFoundError:
|
||||
self.has_flask_login = False
|
||||
return app
|
||||
|
||||
login_manager: flask_login.LoginManager = flask_login.LoginManager()
|
||||
login_manager.init_app(app)
|
||||
|
||||
auth: DigestAuth = DigestAuth(realm=_REALM)
|
||||
login_manager: LoginManager = LoginManager()
|
||||
user_db: t.Dict[str, str] \
|
||||
= {_USERNAME: make_password_hash(_REALM, _USERNAME, _PASSWORD)}
|
||||
auth.init_app(app)
|
||||
|
||||
self.user: User = User(_USERNAME, _PASSWORD)
|
||||
user_db: t.Dict[str, User] = {_USERNAME: self.user}
|
||||
|
||||
@auth.register_get_password
|
||||
def get_password_hash(username: str) -> t.Optional[str]:
|
||||
@ -70,17 +96,17 @@ class FlaskLoginTestCase(TestCase):
|
||||
: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
|
||||
return user_db[username].password_hash 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
|
||||
@auth.register_on_login
|
||||
def on_login(user: User):
|
||||
"""The callback when the user logs in.
|
||||
|
||||
login_manager.init_app(app)
|
||||
init_login_manager(auth, login_manager)
|
||||
:param user: The logged-in user.
|
||||
:return: None.
|
||||
"""
|
||||
user.visits = user.visits + 1
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id: str) -> t.Optional[User]:
|
||||
@ -89,25 +115,35 @@ class FlaskLoginTestCase(TestCase):
|
||||
: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
|
||||
return user_db[user_id] if user_id in user_db else None
|
||||
|
||||
@app.route("/login-required-1/auth", endpoint="auth-1")
|
||||
@app.get("/admin-1/auth", endpoint="admin-1")
|
||||
@flask_login.login_required
|
||||
def login_required_1() -> str:
|
||||
"""The first dummy view.
|
||||
def admin_1() -> str:
|
||||
"""The first administration section.
|
||||
|
||||
:return: The response.
|
||||
"""
|
||||
return f"Hello, {flask_login.current_user.username}! #1"
|
||||
return f"Hello, {flask_login.current_user.get_id()}! #1"
|
||||
|
||||
@app.route("/login-required-2/auth", endpoint="auth-2")
|
||||
@app.get("/admin-2/auth", endpoint="admin-2")
|
||||
@flask_login.login_required
|
||||
def login_required_2() -> str:
|
||||
"""The second dummy view.
|
||||
def admin_2() -> str:
|
||||
"""The second administration section.
|
||||
|
||||
:return: The response.
|
||||
"""
|
||||
return f"Hello, {flask_login.current_user.username}! #2"
|
||||
return f"Hello, {flask_login.current_user.get_id()}! #2"
|
||||
|
||||
@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"))
|
||||
|
||||
return app
|
||||
|
||||
@ -116,14 +152,107 @@ class FlaskLoginTestCase(TestCase):
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
response: Response = self.client.get(self.app.url_for("auth-1"))
|
||||
if not self.has_flask_login:
|
||||
self.skipTest("Skipped without Flask-Login.")
|
||||
|
||||
response: Response = self.client.get(self.app.url_for("admin-1"))
|
||||
self.assertEqual(response.status_code, 401)
|
||||
response = self.client.get(
|
||||
self.app.url_for("auth-1"), digest_auth=(_USERNAME, _PASSWORD))
|
||||
self.app.url_for("admin-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"))
|
||||
response: Response = self.client.get(self.app.url_for("admin-2"))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data.decode("UTF-8"),
|
||||
f"Hello, {_USERNAME}! #2")
|
||||
self.assertEqual(self.user.visits, 1)
|
||||
|
||||
def test_stale_opaque(self) -> None:
|
||||
"""Tests the stale and opaque value.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
if not self.has_flask_login:
|
||||
self.skipTest("Skipped without Flask-Login.")
|
||||
|
||||
admin_uri: str = self.app.url_for("admin-1")
|
||||
response: Response
|
||||
www_authenticate: WWWAuthenticate
|
||||
auth_data: Authorization
|
||||
|
||||
response = super(Client, self.client).get(admin_uri)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
www_authenticate = response.www_authenticate
|
||||
self.assertEqual(www_authenticate.type, "digest")
|
||||
self.assertEqual(www_authenticate.stale, None)
|
||||
opaque: str = www_authenticate.opaque
|
||||
|
||||
if hasattr(g, "_login_user"):
|
||||
delattr(g, "_login_user")
|
||||
www_authenticate.nonce = "bad"
|
||||
auth_data = Client.make_authorization(
|
||||
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
||||
response = super(Client, self.client).get(admin_uri, auth=auth_data)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
www_authenticate = response.www_authenticate
|
||||
self.assertEqual(www_authenticate.stale, True)
|
||||
self.assertEqual(www_authenticate.opaque, opaque)
|
||||
|
||||
if hasattr(g, "_login_user"):
|
||||
delattr(g, "_login_user")
|
||||
auth_data = Client.make_authorization(
|
||||
www_authenticate, admin_uri, _USERNAME, _PASSWORD + "2")
|
||||
response = super(Client, self.client).get(admin_uri, auth=auth_data)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
www_authenticate = response.www_authenticate
|
||||
self.assertEqual(www_authenticate.stale, False)
|
||||
self.assertEqual(www_authenticate.opaque, opaque)
|
||||
|
||||
if hasattr(g, "_login_user"):
|
||||
delattr(g, "_login_user")
|
||||
auth_data = Client.make_authorization(
|
||||
www_authenticate, admin_uri, _USERNAME, _PASSWORD)
|
||||
response = super(Client, self.client).get(admin_uri, auth=auth_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_logout(self) -> None:
|
||||
"""Tests the logging out.
|
||||
|
||||
:return: None.
|
||||
"""
|
||||
if not self.has_flask_login:
|
||||
self.skipTest("Skipped without Flask-Login.")
|
||||
|
||||
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)
|
||||
self.assertEqual(self.user.visits, 2)
|
||||
|
Reference in New Issue
Block a user