| 1 |
Features |
| 2 |
======== |
| 3 |
|
| 4 |
Flask-Security allows you to quickly add common security mechanisms to your |
| 5 |
Flask application. They include: |
| 6 |
|
| 7 |
|
| 8 |
Session Based Authentication |
| 9 |
---------------------------- |
| 10 |
|
| 11 |
Session based authentication is fulfilled entirely by the `Flask-Login`_ |
| 12 |
extension. Flask-Security handles the configuration of Flask-Login automatically |
| 13 |
based on a few of its own configuration values and uses Flask-Login's |
| 14 |
`alternative token`_ feature to associate the value of ``fs_uniquifier`` with the user. |
| 15 |
(This enables easily invalidating all existing sessions for a given user without |
| 16 |
having to change their user id). `Flask-WTF`_ |
| 17 |
integrates with the session as well to provide out of the box CSRF support. |
| 18 |
Flask-Security extends that to support configurations that would require CSRF for requests that are |
| 19 |
authenticated via session cookies, but not for requests authenticated using tokens. |
| 20 |
|
| 21 |
|
| 22 |
Role/Identity Based Access |
| 23 |
-------------------------- |
| 24 |
|
| 25 |
Flask-Security implements very basic role management out of the box. This means |
| 26 |
that you can associate a high level role or multiple roles to any user. For |
| 27 |
instance, you may assign roles such as `Admin`, `Editor`, `SuperUser`, or a |
| 28 |
combination of said roles to a user. Access control is based on the role name and/or |
| 29 |
permissions contained within the role; |
| 30 |
and all roles should be uniquely named. This feature is implemented using the |
| 31 |
`Flask-Principal`_ extension. As with basic RBAC, permissions can be assigned to roles |
| 32 |
to provide more granular access control. Permissions can be associated with one or |
| 33 |
more roles (the RoleModel contains a list of permissions). The values of |
| 34 |
permissions are completely up to the developer - Flask-Security simply treats them |
| 35 |
as strings. |
| 36 |
If you'd like to implement even more granular access |
| 37 |
control (such as per-object), you can refer to the Flask-Principal `documentation on this topic`_. |
| 38 |
|
| 39 |
|
| 40 |
Password Hashing |
| 41 |
---------------- |
| 42 |
|
| 43 |
Password hashing is implemented using `passlib`_. Passwords are hashed with the |
| 44 |
`argon2`_ function by default but you can easily configure other hashing |
| 45 |
algorithms. |
| 46 |
For any given hashing algorithm, consult its documentation on what |
| 47 |
options/values are recommended. For argon2, the `argon2_cffi`_ package |
| 48 |
keeps its default options up to date with `RFC9106`_, and should be suitable for most |
| 49 |
applications. The `OWASP Password Storage Cheat Sheet`_ also |
| 50 |
has a lot of useful suggestions. |
| 51 |
|
| 52 |
You should **always use a hashing algorithm** in your production |
| 53 |
environment. Hash algorithms not listed in :data:`SECURITY_PASSWORD_SINGLE_HASH` |
| 54 |
will be double hashed - first an HMAC will be computed, then the selected hash |
| 55 |
function will be used. In this case - you must provide a :data:`SECURITY_PASSWORD_SALT`. |
| 56 |
A good way to generate this is:: |
| 57 |
|
| 58 |
secrets.SystemRandom().getrandbits(128) |
| 59 |
|
| 60 |
Bear in mind passlib does not assume which |
| 61 |
algorithm you will choose and may require additional libraries to be installed. |
| 62 |
|
| 63 |
Password Validation and Complexity |
| 64 |
----------------------------------- |
| 65 |
Consult :ref:`pass_validation_topic`. |
| 66 |
|
| 67 |
|
| 68 |
Basic HTTP Authentication |
| 69 |
------------------------- |
| 70 |
|
| 71 |
Basic HTTP authentication is achievable using a simple view method decorator. |
| 72 |
This feature expects the incoming authentication information to identify a user |
| 73 |
in the system. This means that the username must be equal to their email address. |
| 74 |
|
| 75 |
|
| 76 |
Token Authentication |
| 77 |
-------------------- |
| 78 |
|
| 79 |
Token based authentication can be used by retrieving the user auth token from an |
| 80 |
authentication endpoint (e.g. ``/login``, ``/us-signin``, ``/wan-signin``, ``/verify``, |
| 81 |
``/us-verify``, ``/wan-verify``). |
| 82 |
Perform an HTTP POST with a query param of ``include_auth_token`` and the authentication details |
| 83 |
as JSON data. |
| 84 |
A successful call will return the authentication token. This token can be used in subsequent |
| 85 |
requests to protected resources. The auth token should be supplied in the request |
| 86 |
through an HTTP header or query string parameter. By default the HTTP header |
| 87 |
name is `Authentication-Token` and the default query string parameter name is |
| 88 |
`auth_token`. |
| 89 |
|
| 90 |
If :ref:`configured<configuration:Refresh Tokens>`, a robust refresh token feature is available. |
| 91 |
See :ref:`token_topic`. |
| 92 |
|
| 93 |
User Registration |
| 94 |
----------------- |
| 95 |
If :ref:`configured<configuration:Registerable>`, Flask-Security provides a basic user registration view. This view is |
| 96 |
very simple and new users need only supply an email address and their password. |
| 97 |
This view can be overridden if your registration process requires more fields. |
| 98 |
User email is validated and normalized using the |
| 99 |
`email_validator <https://pypi.org/project/email-validator/>`_ package. |
| 100 |
|
| 101 |
Username Support |
| 102 |
----------------- |
| 103 |
Flask-Security supports configuring and using a ``username`` in addition to or instead of an email for |
| 104 |
authentication. |
| 105 |
|
| 106 |
If the :py:data:`SECURITY_USERNAME_ENABLE` configuration option is set to ``True``, ``username`` |
| 107 |
will be added to the register and login forms. |
| 108 |
By default, the user will be |
| 109 |
able to authenticate with EITHER email or username - however that can be changed via the |
| 110 |
:py:data:`SECURITY_USER_IDENTITY_ATTRIBUTES`. |
| 111 |
|
| 112 |
The :py:data:`SECURITY_USERNAME_RECOVERY` option adds an endpoint that allows users |
| 113 |
to recover a forgotten username (via email). |
| 114 |
|
| 115 |
The :py:data:`SECURITY_CHANGE_USERNAME` option adds an endpoint that can be |
| 116 |
used by authenticated users to change their username. |
| 117 |
|
| 118 |
Email Confirmation |
| 119 |
------------------ |
| 120 |
If :ref:`configured<configuration:Confirmable>`, your application |
| 121 |
can require that new users confirm their email address prior to allowing them to authenticate. |
| 122 |
Flask-Security will send an email message to any new users with a confirmation |
| 123 |
link. Upon navigating to the confirmation link, the user's account will be set to |
| 124 |
'confirmed'. The user can then sign in usually the normal mechanisms. |
| 125 |
There is also view for resending a confirmation link to a given email |
| 126 |
if the user happens to try to use an expired token or has lost the previous |
| 127 |
email. Confirmation links can be configured to expire after a specified amount |
| 128 |
of time (default 2 days). |
| 129 |
|
| 130 |
Password Reset/Recovery |
| 131 |
----------------------- |
| 132 |
If :ref:`configured<configuration:Recoverable>`, |
| 133 |
password reset and recovery is available for when a user forgets their |
| 134 |
password. Flask-Security sends an email to the user with a link to a view which |
| 135 |
allows them to reset their password. Once the password is reset they are redirected to |
| 136 |
the login page where they need to authenticate using the new password. |
| 137 |
Password reset links can be configured to expire after a specified amount of time. |
| 138 |
|
| 139 |
As with password change - this will update the the user's ``fs_uniquifier`` attribute |
| 140 |
which will invalidate all existing sessions AND (by default) all authentication tokens. |
| 141 |
|
| 142 |
Password Change |
| 143 |
--------------- |
| 144 |
If :ref:`configured<configuration:Changeable>` users can change their password. Unlike password |
| 145 |
recovery, this endpoint is used when the user is already authenticated. The result |
| 146 |
of a successful password change is not only a new password, but a new value for ``fs_uniquifier``. |
| 147 |
This has the effect of immediately invalidating all existing sessions. The change request |
| 148 |
itself effectively re-signs in in the user so a new session is created. Note that |
| 149 |
the same signals are sent as when the user normally authenticates. |
| 150 |
|
| 151 |
*NOTE*: The ``fs_uniquifier`` by default, controls both sessions and authenticated tokens. |
| 152 |
Thus changing the password also invalidates all authentication tokens. This may not be desirable |
| 153 |
behavior, so if the UserModel contains an attribute ``fs_token_uniquifier``, then that will be used |
| 154 |
when generating authentication tokens and so won't be affected by password changes. |
| 155 |
|
| 156 |
Two-factor Authentication |
| 157 |
---------------------------------------- |
| 158 |
If :ref:`configured<configuration:Two-Factor>`, |
| 159 |
the two-factor authentication feature generates time-based one time passwords |
| 160 |
(Tokens). The tokens are generated using the users `totp secret`_, which is unique |
| 161 |
per user, and is generated both on first login, and when changing the two-factor |
| 162 |
method (doing this causes the previous totp secret to become invalid). The token |
| 163 |
is provided by one of 3 methods - email, sms (service is not provided), or |
| 164 |
an authenticator app such as Google Authenticator, LastPass Authenticator, or Authy. |
| 165 |
By default, tokens provided by the authenticator app are |
| 166 |
valid for 2 minutes, tokens sent by mail for up to 5 minute and tokens sent by |
| 167 |
sms for up to 2 minutes. The QR code used to supply the authenticator app with |
| 168 |
the secret is generated using the `qrcode <https://pypi.org/project/qrcode/>`_ library. |
| 169 |
Please read :ref:`2fa_theory_of_operation` for more details. |
| 170 |
|
| 171 |
The Two-factor feature offers the ability for a user to 'rescue' themselves if |
| 172 |
they lose track of their secondary factor device. Rescue options include sending |
| 173 |
a one time code via email, send an email to the application admin, and using a previously |
| 174 |
generated and downloaded one-time code (see :py:data:`SECURITY_MULTI_FACTOR_RECOVERY_CODES`). |
| 175 |
|
| 176 |
Unified Sign In |
| 177 |
--------------- |
| 178 |
If :ref:`configured<configuration:Unified Signin>`, |
| 179 |
a generalized login endpoint is provided that takes an `identity` |
| 180 |
and a `passcode`; where (based on configuration): |
| 181 |
|
| 182 |
* `identity` is any of :py:data:`SECURITY_USER_IDENTITY_ATTRIBUTES` (e.g. email, username, phone) |
| 183 |
* `passcode` is a password or a one-time code (delivered via email, SMS, or authenticator app) |
| 184 |
|
| 185 |
Please see this `Wikipedia`_ article about multi-factor authentication. |
| 186 |
|
| 187 |
Using this feature, it is possible to not require the user to have a stored password |
| 188 |
at all, and just require the use of a one-time code. The mechanisms for generating |
| 189 |
and delivering the one-time code are similar to common two-factor mechanisms. |
| 190 |
|
| 191 |
This one-time code can be configured to be delivered via email, SMS or authenticator app - |
| 192 |
however be aware that NIST does not recommend email for this purpose (though many web sites do so) |
| 193 |
due to the fact that a) email may travel through |
| 194 |
many different servers as part of being delivered - and b) is available from any device. |
| 195 |
|
| 196 |
Using SMS or an authenticator app means you are providing "something you have" (the mobile device) |
| 197 |
and either "something you know" (passcode to unlock your device) |
| 198 |
or "something you are" (biometric quality to unlock your device). |
| 199 |
This effectively means that using a one-time code to sign in, is in fact already two-factor (if using |
| 200 |
SMS or authenticator app). Many large authentication providers already offer this - here is |
| 201 |
`Microsoft's`_ version. |
| 202 |
|
| 203 |
Note that by configuring :py:data:`SECURITY_US_ENABLED_METHODS` an application can |
| 204 |
use this endpoint JUST with identity/password or in fact disallow passwords altogether. |
| 205 |
|
| 206 |
Unified sign in is integrated with two-factor authentication. Since in general |
| 207 |
there is no need for a second factor if the initial authentication was with SMS or |
| 208 |
an authenticator application, the :py:data:`SECURITY_US_MFA_REQUIRED` configuration |
| 209 |
determines which primary authentication mechanisms require a second factor. By default |
| 210 |
limited to ``email`` and ``password`` (if two-factor is enabled). |
| 211 |
|
| 212 |
Be aware that by default, the :py:data:`SECURITY_US_SETUP_URL` endpoint is protected |
| 213 |
with a freshness check (see :meth:`flask_security.auth_required`) which means it requires a session |
| 214 |
cookie to function properly. This is true even if using JSON payload or token authentication. |
| 215 |
If you disable the freshness check then sessions aren't required. |
| 216 |
|
| 217 |
`Current Limited Functionality`: |
| 218 |
|
| 219 |
* Change password does not work if a user registers without a password. However |
| 220 |
forgot-password will allow the user to set a new password. |
| 221 |
* Registration and Confirmation only work with email - so while you can enable multiple |
| 222 |
authentication methods, you still have to register with email. |
| 223 |
|
| 224 |
WebAuthn/Passkeys |
| 225 |
----------------- |
| 226 |
**This feature is in Beta - mostly due to it being brand new and little to no production soak time** |
| 227 |
|
| 228 |
WebAuthn is a standardized protocol that connects authenticators (such as YubiKey and mobile biometrics) |
| 229 |
with websites. If :ref:`configured<configuration:WebAuthn/Passkeys>`, Flask-Security supports using WebAuthn credentials (aka passkeys) as either 'first' or 'secondary' |
| 230 |
authenticators. Please read :ref:`webauthn_topic` for more details. |
| 231 |
|
| 232 |
Email Change |
| 233 |
------------ |
| 234 |
If :ref:`configured<configuration:Change-Email>`, users can change the email they registered with. This will send a new confirmation email to the new email address. |
| 235 |
|
| 236 |
Login Tracking |
| 237 |
-------------- |
| 238 |
Flask-Security can, if :ref:`configured<configuration:Trackable>`, keep track of basic login events and |
| 239 |
statistics. They include: |
| 240 |
|
| 241 |
* Last login date |
| 242 |
* Current login date |
| 243 |
* Last login IP address |
| 244 |
* Current login IP address |
| 245 |
* Total login count |
| 246 |
|
| 247 |
JSON/Ajax Support |
| 248 |
----------------- |
| 249 |
Flask-Security supports JSON/Ajax requests where appropriate. Please |
| 250 |
look at :ref:`csrf_topic` for details on how to work with JSON and |
| 251 |
Single Page Applications. |
| 252 |
|
| 253 |
In addition, :ref:`spa:Working With Single Page Applications` |
| 254 |
(like those built with Vue, Angular, and React) are supported via customizable redirect links. |
| 255 |
|
| 256 |
Command Line Interface |
| 257 |
---------------------- |
| 258 |
Basic `Click`_ commands for managing users and roles are automatically |
| 259 |
registered. They can be completely disabled or their names can be changed. |
| 260 |
Run ``flask --help`` and look for users and roles. |
| 261 |
|
| 262 |
|
| 263 |
Social Login (OAuth) Authentication |
| 264 |
------------------------------------ |
| 265 |
Flask-Security provides a thin layer which integrates `authlib`_ with Flask-Security |
| 266 |
views and features (such as two-factor authentication and verify). Flask-Security is shipped |
| 267 |
with support for github and google - others can be added by the application (see `loginpass`_ |
| 268 |
for many examples). |
| 269 |
|
| 270 |
See :py:class:`flask_security.OAuthGlue` and :py:class:`flask_security.FsOAuthProvider` |
| 271 |
|
| 272 |
Please note - this is for authentication only, and the authenticating user must |
| 273 |
already be a registered user in your application. Once authenticated, all further |
| 274 |
authorization uses Flask-Security role/permission mechanisms. |
| 275 |
|
| 276 |
See `Flask OAuth Client <https://docs.authlib.org/en/latest/client/flask.html>`_ |
| 277 |
for details. Note in particular, that you must setup and provide provider specific |
| 278 |
information - and most importantly - XX_CLIENT_ID and XX_CLIENT_SECRET should be |
| 279 |
specified as environment variables. |
| 280 |
|
| 281 |
We have seen issues with some providers when `SESSION_COOKIE_SAMESITE` = "strict". |
| 282 |
The handshake (sometimes just the first time when the user is being asked to accept your application) |
| 283 |
fails due to the session cookie not getting sent as part of the redirect. |
| 284 |
|
| 285 |
A very simple example of configuring social login with Flask-Security is available |
| 286 |
in the `examples` directory. |
| 287 |
|
| 288 |
Account Management |
| 289 |
------------------- |
| 290 |
Flask-Security includes support for managing user accounts. A combination of forms, |
| 291 |
views, database models, and CLI provide application support for: |
| 292 |
|
| 293 |
Adding and Deleting Users |
| 294 |
+++++++++++++++++++++++++ |
| 295 |
Users can be added either by enabling the Register feature - which allows users to self-register |
| 296 |
and/or via CLI - where an administrator can control precisely who to add to the system. |
| 297 |
Users can be allowed or required to have a username in addition to an email address. |
| 298 |
|
| 299 |
Self-Management of Accounts |
| 300 |
+++++++++++++++++++++++++++ |
| 301 |
Flask-Security can be configured to allow users to perform many normal administrative |
| 302 |
tasks pertaining to security: |
| 303 |
|
| 304 |
* :ref:`Change Password<features:Password Change>` |
| 305 |
* :ref:`Forgot Password<features:Password Reset/Recovery>` |
| 306 |
* :ref:`Change Username<features:Username Support>` |
| 307 |
* :ref:`Forgot Username<features:Username Support>` |
| 308 |
* :ref:`Change Email<features:Email Change>` |
| 309 |
* Add/modify alternative primary authentication mechanisms - |
| 310 |
SMS, Passkeys, Authenticator, magic link, social login (OAuth) |
| 311 |
* Add/modify second factor authentication mechanisms - |
| 312 |
SMS, Passkeys, Authenticator, magic link |
| 313 |
|
| 314 |
Administrative Management of Accounts |
| 315 |
++++++++++++++++++++++++++++++++++++++ |
| 316 |
The CLI and API provides administrators and the application control over accounts. |
| 317 |
|
| 318 |
* Deactivate immediately stops any access by the user to any |
| 319 |
protected endpoints |
| 320 |
|
| 321 |
* CLI: ``user deactivate`` |
| 322 |
* API: :py:meth:`.UserDatastore.deactivate_user` |
| 323 |
* Reset user access - this disables all authentication mechanisms except password |
| 324 |
(e.g. one-time password, passkeys, tokens) |
| 325 |
|
| 326 |
* CLI: ``user reset_access`` |
| 327 |
* API: :py:meth:`.UserDatastore.reset_user_access` |
| 328 |
* Create or delete roles - including adding/removing permissions from roles |
| 329 |
|
| 330 |
* CLI: ``roles create``, ``roles add_permissions``, ``roles remove_permissions`` |
| 331 |
* API: :py:meth:`.UserDatastore.create_role`, :py:meth:`.UserDatastore.add_permissions_to_role`, :py:meth:`.UserDatastore.remove_permissions_from_role` |
| 332 |
* Add or remove roles from a user |
| 333 |
|
| 334 |
* CLI: ``roles add``, ``roles remove`` |
| 335 |
* API: :py:meth:`.UserDatastore.add_role_to_user`, :py:meth:`.UserDatastore.remove_role_from_user` |
| 336 |
* API - :ref:`Signals<api:Signals>` are sent for many operations that allow the application to track |
| 337 |
important account changes (such as track_failed_authn) |
| 338 |
* Configuration - allow or require two factor authentication |
| 339 |
* API - :py:meth:`.UserMixin.check_tf_required` allows applications to fine-tune which users require MFA. |
| 340 |
For example could require all users with a role of 'admin' to setup MFA. |
| 341 |
* API - :py:meth:`.UserMixin.is_locked` can be used to support account lockout |
| 342 |
|
| 343 |
|
| 344 |
.. _Click: https://palletsprojects.com/p/click/ |
| 345 |
.. _Flask-Login: https://flask-login.readthedocs.org/en/latest/ |
| 346 |
.. _Flask-WTF: https://flask-wtf.readthedocs.io/en/1.2.x/csrf/ |
| 347 |
.. _alternative token: https://flask-login.readthedocs.io/en/latest/#alternative-tokens |
| 348 |
.. _Flask-Principal: https://pypi.org/project/Flask-Principal/ |
| 349 |
.. _documentation on this topic: http://packages.python.org/Flask-Principal/#granular-resource-protection |
| 350 |
.. _passlib: https://passlib.readthedocs.io/en/stable/ |
| 351 |
.. _totp secret: https://passlib.readthedocs.io/en/stable/narr/totp-tutorial.html#overview |
| 352 |
.. _argon2: https://en.wikipedia.org/wiki/Argon2 |
| 353 |
.. _argon2_cffi: https://pypi.org/project/argon2-cffi/ |
| 354 |
.. _RFC9106: https://www.rfc-editor.org/rfc/rfc9106.html |
| 355 |
.. _OWASP Password Storage Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html |
| 356 |
.. _bcrypt: https://en.wikipedia.org/wiki/Bcrypt |
| 357 |
.. _PyQRCode: https://pypi.python.org/pypi/PyQRCode/ |
| 358 |
.. _Wikipedia: https://en.wikipedia.org/wiki/Multi-factor_authentication |
| 359 |
.. _Microsoft's: https://docs.microsoft.com/en-us/azure/active-directory/user-help/user-help-auth-app-overview |
| 360 |
.. _authlib: https://authlib.org/ |
| 361 |
.. _loginpass: https://github.com/authlib/loginpass |