2022-12-06 18:54:40 +08:00
|
|
|
Introduction
|
|
|
|
============
|
|
|
|
|
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
*Flask-DigestAuth* is an `HTTP Digest Authentication`_ implementation
|
2022-12-06 18:54:40 +08:00
|
|
|
for Flask_ applications. It authenticates the user for the protected
|
|
|
|
views.
|
|
|
|
|
|
|
|
HTTP Digest Authentication is specified in `RFC 2617`_.
|
|
|
|
|
|
|
|
|
|
|
|
Why HTTP Digest Authentication?
|
|
|
|
-------------------------------
|
|
|
|
|
2022-12-06 21:54:10 +08:00
|
|
|
*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.
|
2022-12-06 18:54:40 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
Flask-DigestAuth works with Flask-Login_. Log in protection can be
|
2022-12-06 18:54:40 +08:00
|
|
|
separated with the authentication mechanism. You can create protected
|
|
|
|
Flask modules without knowing the actual authentication mechanisms.
|
|
|
|
|
|
|
|
|
2022-12-06 20:58:13 +08:00
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
You can install Flask-DigestAuth with ``pip``:
|
2022-12-06 20:58:13 +08:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
pip install Flask-DigestAuth
|
2022-12-06 20:58:13 +08:00
|
|
|
|
|
|
|
You may also install the latest source from the
|
2023-01-04 21:29:12 +08:00
|
|
|
`Flask-DigestAuth GitHub repository`_.
|
2022-12-06 20:58:13 +08:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
pip install git+https://github.com/imacat/flask-digestauth.git
|
2022-12-06 20:58:13 +08:00
|
|
|
|
|
|
|
|
2023-01-06 00:19:24 +08:00
|
|
|
Configuration
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Flask-DigestAuth takes the configuration ``DIGEST_AUTH_REALM`` as the
|
|
|
|
realm. The default realm is ``Login Required``.
|
|
|
|
|
|
|
|
|
2022-12-06 20:58:13 +08:00
|
|
|
Setting the Password
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2022-12-07 18:11:35 +08:00
|
|
|
See :func:`flask_digest_auth.algo.make_password_hash`.
|
2022-12-06 20:58:13 +08:00
|
|
|
|
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
Flask-DigestAuth Alone
|
|
|
|
----------------------
|
2022-12-06 18:54:40 +08:00
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
Flask-DigestAuth can authenticate the users alone.
|
2022-12-06 20:58:13 +08:00
|
|
|
|
|
|
|
See :ref:`example-alone-simple` and :ref:`example-alone-large`.
|
2022-12-06 18:54:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
Flask-Login Integration
|
2022-12-06 20:58:13 +08:00
|
|
|
-----------------------
|
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
Flask-DigestAuth works with Flask-Login_. You can write a Flask
|
2022-12-06 20:58:13 +08:00
|
|
|
module that requires log in, without specifying how to log in. The
|
|
|
|
application can use either HTTP Digest Authentication, or the log in
|
|
|
|
forms, as needed.
|
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
To use Flask-Login with Flask-DigestAuth,
|
2022-12-06 20:58:13 +08:00
|
|
|
``login_manager.init_app(app)`` must be called before
|
|
|
|
``auth.init_app(app)``.
|
2022-12-06 18:54:40 +08:00
|
|
|
|
2022-12-06 20:58:13 +08:00
|
|
|
The currently logged-in user can be retrieved at
|
|
|
|
``flask_login.current_user``, if any.
|
2022-12-06 18:54:40 +08:00
|
|
|
|
|
|
|
See :ref:`example-flask-login-simple` and
|
|
|
|
:ref:`example-flask-login-large`.
|
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
The views only depend on Flask-Login, but not the Flask-DigestAuth.
|
2022-12-06 20:58:13 +08:00
|
|
|
You can change the actual authentication mechanism without changing
|
|
|
|
the views.
|
|
|
|
|
2022-12-06 18:54:40 +08:00
|
|
|
|
|
|
|
Session Integration
|
2022-12-06 20:58:13 +08:00
|
|
|
-------------------
|
2022-12-06 18:54:40 +08:00
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
Flask-DigestAuth features session integration. The user log in
|
2022-12-06 18:54:40 +08:00
|
|
|
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 In Bookkeeping
|
2022-12-06 20:58:13 +08:00
|
|
|
------------------
|
|
|
|
|
|
|
|
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
|
2022-12-06 18:54:40 +08:00
|
|
|
|
2022-12-07 18:11:35 +08:00
|
|
|
See :meth:`flask_digest_auth.auth.DigestAuth.register_on_login`.
|
2022-12-06 18:54:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
Log Out
|
2022-12-06 20:58:13 +08:00
|
|
|
-------
|
2022-12-06 18:54:40 +08:00
|
|
|
|
2023-01-04 21:29:12 +08:00
|
|
|
Flask-DigestAuth supports log out. The user will be prompted for the
|
2022-12-06 18:54:40 +08:00
|
|
|
new username and password.
|
2022-12-06 20:58:13 +08:00
|
|
|
|
2022-12-07 18:11:35 +08:00
|
|
|
See :meth:`flask_digest_auth.auth.DigestAuth.logout`.
|
2022-12-06 18:54:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
.. _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
|
2023-01-04 21:29:12 +08:00
|
|
|
.. _Flask-DigestAuth GitHub repository: https://github.com/imacat/flask-digestauth
|