| 1 |
API |
| 2 |
=== |
| 3 |
|
| 4 |
The external (json/form) API is described `here`_ |
| 5 |
|
| 6 |
.. _here: _static/openapi_view.html |
| 7 |
|
| 8 |
|
| 9 |
Core |
| 10 |
---- |
| 11 |
.. autoclass:: flask_security.Security |
| 12 |
:members: |
| 13 |
|
| 14 |
.. data:: flask_security.current_user |
| 15 |
|
| 16 |
A proxy for the current user. |
| 17 |
|
| 18 |
Protecting Views |
| 19 |
---------------- |
| 20 |
All Flask-Security decorators are compatible with Flask's async implementation. |
| 21 |
This is accomplished by wrapping function calls with flask.ensure_async(). |
| 22 |
Please see `Flask async`_. |
| 23 |
|
| 24 |
.. autofunction:: flask_security.anonymous_user_required |
| 25 |
|
| 26 |
.. autofunction:: flask_security.http_auth_required |
| 27 |
|
| 28 |
.. autofunction:: flask_security.auth_token_required |
| 29 |
|
| 30 |
.. autofunction:: flask_security.auth_required |
| 31 |
|
| 32 |
.. autofunction:: flask_security.roles_required |
| 33 |
|
| 34 |
.. autofunction:: flask_security.roles_accepted |
| 35 |
|
| 36 |
.. autofunction:: flask_security.permissions_required |
| 37 |
|
| 38 |
.. autofunction:: flask_security.permissions_accepted |
| 39 |
|
| 40 |
.. autofunction:: flask_security.unauth_csrf |
| 41 |
|
| 42 |
.. autofunction:: flask_security.handle_csrf |
| 43 |
|
| 44 |
User Object Helpers |
| 45 |
------------------- |
| 46 |
.. autoclass:: flask_security.UserMixin |
| 47 |
:members: |
| 48 |
|
| 49 |
.. autoclass:: flask_security.RoleMixin |
| 50 |
:members: |
| 51 |
|
| 52 |
.. autoclass:: flask_security.WebAuthnMixin |
| 53 |
:members: |
| 54 |
|
| 55 |
.. autoclass:: flask_security.RefreshTrackerMixin |
| 56 |
:members: |
| 57 |
|
| 58 |
|
| 59 |
Datastores |
| 60 |
---------- |
| 61 |
.. autoclass:: flask_security.UserDatastore |
| 62 |
:members: |
| 63 |
:exclude-members: create_webauthn, find_user_from_webauthn, |
| 64 |
mf_set_recovery_codes, mf_delete_recovery_code, |
| 65 |
set_webauthn_user_handle, |
| 66 |
us_get_totp_secrets, us_put_totp_secrets, |
| 67 |
create_refresh_tracker, |
| 68 |
|
| 69 |
.. autoclass:: flask_security.SQLAlchemyUserDatastore |
| 70 |
:show-inheritance: |
| 71 |
|
| 72 |
.. autoclass:: flask_security.FSQLALiteUserDatastore |
| 73 |
:show-inheritance: |
| 74 |
|
| 75 |
.. autoclass:: flask_security.SQLAlchemySessionUserDatastore |
| 76 |
:show-inheritance: |
| 77 |
|
| 78 |
.. autoclass:: flask_security.MongoEngineUserDatastore |
| 79 |
:show-inheritance: |
| 80 |
|
| 81 |
.. autoclass:: flask_security.PeeweeUserDatastore |
| 82 |
:show-inheritance: |
| 83 |
|
| 84 |
.. autoclass:: flask_security.datastore.SQLAlchemyDatastore |
| 85 |
|
| 86 |
Internal class implementing DataStore interface. |
| 87 |
|
| 88 |
.. autoclass:: flask_security.datastore.MongoEngineDatastore |
| 89 |
|
| 90 |
Internal class implementing DataStore interface. |
| 91 |
|
| 92 |
.. autoclass:: flask_security.datastore.PeeweeDatastore |
| 93 |
|
| 94 |
Internal class implementing DataStore interface. |
| 95 |
|
| 96 |
.. class:: User |
| 97 |
|
| 98 |
The User model. This must be provided by the application. |
| 99 |
See :ref:`Models <models_topic>`. |
| 100 |
|
| 101 |
.. class:: Role |
| 102 |
|
| 103 |
The Role model. This must be provided by the application. |
| 104 |
See :ref:`Models <models_topic>`. |
| 105 |
|
| 106 |
.. class:: WebAuthn |
| 107 |
|
| 108 |
The WebAuthn model. This must be provided by the application. |
| 109 |
See :ref:`Models <models_topic>`. |
| 110 |
|
| 111 |
.. class:: FsRefreshTracker |
| 112 |
|
| 113 |
The RefreshTracker model. This must be provided by the application. |
| 114 |
See :ref:`Models <models_topic>`. |
| 115 |
|
| 116 |
Packaged Models |
| 117 |
--------------- |
| 118 |
.. autoclass:: flask_security.models.fsqla.FsModels |
| 119 |
:members: |
| 120 |
.. autoclass:: flask_security.models.sqla.FsModels |
| 121 |
:members: |
| 122 |
|
| 123 |
Utils |
| 124 |
----- |
| 125 |
.. autofunction:: flask_security.input_svn |
| 126 |
|
| 127 |
.. autofunction:: flask_security.lookup_identity |
| 128 |
|
| 129 |
.. autofunction:: flask_security.login_user |
| 130 |
|
| 131 |
.. autofunction:: flask_security.logout_user |
| 132 |
|
| 133 |
.. autofunction:: flask_security.check_and_update_authn_fresh |
| 134 |
|
| 135 |
.. autofunction:: flask_security.get_hmac |
| 136 |
|
| 137 |
.. autofunction:: flask_security.get_request_attr |
| 138 |
|
| 139 |
.. autofunction:: flask_security.verify_password |
| 140 |
|
| 141 |
.. autofunction:: flask_security.verify_and_update_password |
| 142 |
|
| 143 |
.. autofunction:: flask_security.hash_password |
| 144 |
|
| 145 |
.. autofunction:: flask_security.admin_change_password |
| 146 |
|
| 147 |
.. autofunction:: flask_security.uia_phone_mapper |
| 148 |
|
| 149 |
.. autofunction:: flask_security.uia_email_mapper |
| 150 |
|
| 151 |
.. autofunction:: flask_security.uia_username_mapper |
| 152 |
|
| 153 |
.. autofunction:: flask_security.url_for_security |
| 154 |
|
| 155 |
.. autofunction:: flask_security.send_mail |
| 156 |
|
| 157 |
.. autofunction:: flask_security.check_and_get_token_status |
| 158 |
|
| 159 |
.. autofunction:: flask_security.get_url |
| 160 |
|
| 161 |
.. autofunction:: flask_security.password_length_validator |
| 162 |
|
| 163 |
.. autofunction:: flask_security.password_complexity_validator |
| 164 |
|
| 165 |
.. autofunction:: flask_security.password_breached_validator |
| 166 |
|
| 167 |
.. autofunction:: flask_security.pwned |
| 168 |
|
| 169 |
.. autofunction:: flask_security.unique_identity_attribute |
| 170 |
|
| 171 |
.. autofunction:: flask_security.us_send_security_token |
| 172 |
|
| 173 |
.. autofunction:: flask_security.tf_send_security_token |
| 174 |
|
| 175 |
.. autoclass:: flask_security.AsaList |
| 176 |
|
| 177 |
.. autoclass:: flask_security.SmsSenderBaseClass |
| 178 |
:members: send_sms |
| 179 |
|
| 180 |
.. autoclass:: flask_security.SmsSenderFactory |
| 181 |
:members: createSender |
| 182 |
|
| 183 |
.. py:class:: OauthCbType[oauth: OAuth, token: t.Any] |
| 184 |
|
| 185 |
This callback is called when the OAuth |
| 186 |
redirect happens. It must take the response from the provider and return |
| 187 |
a tuple of <user_model_field_name, value> - which will be used |
| 188 |
to look up the user in the datastore. |
| 189 |
|
| 190 |
.. autoclass:: flask_security.OAuthGlue |
| 191 |
:members: register_provider, register_provider_ext |
| 192 |
|
| 193 |
.. autoclass:: flask_security.FsOAuthProvider |
| 194 |
:members: |
| 195 |
|
| 196 |
|
| 197 |
Extendable Classes |
| 198 |
------------------ |
| 199 |
Each of the following classes can be extended and passed in as part of |
| 200 |
Security() instantiation. |
| 201 |
|
| 202 |
.. autoclass:: flask_security.PhoneUtil |
| 203 |
:members: |
| 204 |
:special-members: __init__ |
| 205 |
|
| 206 |
.. autoclass:: flask_security.MailUtil |
| 207 |
:members: |
| 208 |
:special-members: __init__ |
| 209 |
|
| 210 |
.. autoclass:: flask_security.EmailValidateException |
| 211 |
|
| 212 |
.. autoclass:: flask_security.PasswordUtil |
| 213 |
:members: |
| 214 |
:special-members: __init__ |
| 215 |
|
| 216 |
.. autoclass:: flask_security.MfRecoveryCodesUtil |
| 217 |
:members: |
| 218 |
:special-members: __init__ |
| 219 |
|
| 220 |
.. autoclass:: flask_security.UsernameUtil |
| 221 |
:members: |
| 222 |
:special-members: __init__ |
| 223 |
|
| 224 |
.. autoclass:: flask_security.WebauthnUtil |
| 225 |
:members: |
| 226 |
:special-members: __init__ |
| 227 |
|
| 228 |
.. autoclass:: flask_security.Totp |
| 229 |
:members: get_last_counter, set_last_counter, generate_qrcode |
| 230 |
|
| 231 |
|
| 232 |
Forms |
| 233 |
----- |
| 234 |
|
| 235 |
.. autoclass:: flask_security.ChangeEmailForm |
| 236 |
.. autoclass:: flask_security.ChangePasswordForm |
| 237 |
.. autoclass:: flask_security.ChangeUsernameForm |
| 238 |
.. autoclass:: flask_security.ConfirmRegisterForm |
| 239 |
.. autoclass:: flask_security.Form |
| 240 |
.. autoclass:: flask_security.FormInfo |
| 241 |
.. autoclass:: flask_security.ForgotPasswordForm |
| 242 |
.. autoclass:: flask_security.LoginForm |
| 243 |
.. autoclass:: flask_security.LogoutForm |
| 244 |
.. autoclass:: flask_security.MfRecoveryCodesForm |
| 245 |
.. autoclass:: flask_security.MfRecoveryForm |
| 246 |
.. autoclass:: flask_security.PasswordlessLoginForm |
| 247 |
.. autoclass:: flask_security.RefreshTokenForm |
| 248 |
.. autoclass:: flask_security.RegisterForm |
| 249 |
.. autoclass:: flask_security.RegisterFormV2 |
| 250 |
.. autoclass:: flask_security.ResetPasswordForm |
| 251 |
.. autoclass:: flask_security.SendConfirmationForm |
| 252 |
.. autoclass:: flask_security.TwoFactorRescueForm |
| 253 |
.. autoclass:: flask_security.TwoFactorSelectForm |
| 254 |
.. autoclass:: flask_security.TwoFactorSetupForm |
| 255 |
.. autoclass:: flask_security.TwoFactorVerifyCodeForm |
| 256 |
.. autoclass:: flask_security.UnifiedSigninForm |
| 257 |
.. autoclass:: flask_security.UnifiedSigninSetupForm |
| 258 |
.. autoclass:: flask_security.UnifiedSigninSetupValidateForm |
| 259 |
.. autoclass:: flask_security.UnifiedVerifyForm |
| 260 |
.. autoclass:: flask_security.UsernameRecoveryForm |
| 261 |
.. autoclass:: flask_security.VerifyForm |
| 262 |
.. autoclass:: flask_security.WebAuthnDeleteForm |
| 263 |
.. autoclass:: flask_security.WebAuthnRegisterForm |
| 264 |
.. autoclass:: flask_security.WebAuthnRegisterResponseForm |
| 265 |
.. autoclass:: flask_security.WebAuthnSigninForm |
| 266 |
.. autoclass:: flask_security.WebAuthnSigninResponseForm |
| 267 |
.. autoclass:: flask_security.WebAuthnVerifyForm |
| 268 |
|
| 269 |
.. _signals_topic: |
| 270 |
|
| 271 |
Signals |
| 272 |
------- |
| 273 |
See the `Flask documentation on signals`_ for information on how to use these |
| 274 |
signals in your code. |
| 275 |
All Flask-Security signals are compatible with Blinker's async implementation. |
| 276 |
See `Blinker async`_ |
| 277 |
|
| 278 |
.. tip:: |
| 279 |
|
| 280 |
Remember to add ``**extra_args`` to your signature so that if we add |
| 281 |
additional parameters in the future your code doesn't break. |
| 282 |
|
| 283 |
See the documentation for the signals provided by the Flask-Login and |
| 284 |
Flask-Principal extensions. In addition to those signals, Flask-Security |
| 285 |
sends the following signals. |
| 286 |
|
| 287 |
.. data:: user_authenticated |
| 288 |
|
| 289 |
Sent when a user successfully authenticates. In addition to the app (which is the |
| 290 |
sender), it is passed `user`, and `authn_via` arguments. The `authn_via` argument |
| 291 |
specifies how the user authenticated - it will be a list with possible values |
| 292 |
of ``password``, ``sms``, ``authenticator``, ``email``, ``confirm``, ``reset``, |
| 293 |
``register``, ``webauthn``, ``oauth``. |
| 294 |
|
| 295 |
.. versionadded:: 3.4.0 |
| 296 |
|
| 297 |
.. data:: user_unauthenticated |
| 298 |
|
| 299 |
Sent when a user accesses a protected endpoint but isn't properly authenticated. |
| 300 |
It is sent as part of the `default_unauthn_handler` (:meth:`.Security.unauthn_handler`) |
| 301 |
It is passed the app (which is the sender). |
| 302 |
|
| 303 |
.. versionadded:: 5.4.0 |
| 304 |
|
| 305 |
.. data:: user_failed_authn |
| 306 |
|
| 307 |
Sent from :py:meth:`.UserMixin.track_failed_authn` when a user fails authentication. |
| 308 |
In addition to the app (which is the sender) it is passed: |
| 309 |
|
| 310 |
* `user` - the user that failed authentication |
| 311 |
* `endpoint` - the endpoint that was accessed |
| 312 |
* `auth_type` - `password`, `code`, `passkey` |
| 313 |
* `tfa` - True if this was a two factor authentication attempt |
| 314 |
|
| 315 |
.. versionadded:: 5.8.0 |
| 316 |
|
| 317 |
.. data:: user_registered |
| 318 |
|
| 319 |
Sent when a user registers on the site. In addition to the app (which is the |
| 320 |
sender), it is passed `user`, `confirm_token` (deprecated), `confirmation_token` and `form_data` arguments. |
| 321 |
`form_data` is a dictionary representation of registration form's content |
| 322 |
received with the registration request. |
| 323 |
|
| 324 |
.. data:: user_not_registered |
| 325 |
|
| 326 |
Sent when a user attempts to register, but is already registered. This is ONLY sent |
| 327 |
when :py:data:`SECURITY_RETURN_GENERIC_RESPONSES` is enabled. It is passed the |
| 328 |
following arguments: |
| 329 |
|
| 330 |
* `user` - The existing user model |
| 331 |
* `existing_email` - True if attempting to register an existing email |
| 332 |
* `existing_username`- True if attempting to register an existing username |
| 333 |
* `form_data` - the entire contents of the posted request form |
| 334 |
|
| 335 |
.. versionadded:: 5.0.0 |
| 336 |
|
| 337 |
.. data:: user_confirmed |
| 338 |
|
| 339 |
Sent when a user is confirmed. In addition to the app (which is the |
| 340 |
sender), it is passed a `user` argument. |
| 341 |
|
| 342 |
.. data:: confirm_instructions_sent |
| 343 |
|
| 344 |
Sent when a user requests confirmation instructions. In addition to the app |
| 345 |
(which is the sender), it is passed a `user` and `confirmation_token` arguments. |
| 346 |
|
| 347 |
.. data:: login_instructions_sent |
| 348 |
|
| 349 |
Sent when passwordless login is used and user logs in. In addition to the app |
| 350 |
(which is the sender), it is passed `user` and `login_token` arguments. |
| 351 |
|
| 352 |
.. data:: password_reset |
| 353 |
|
| 354 |
Sent when a user completes a password reset. In addition to the app (which is |
| 355 |
the sender), it is passed a `user` argument. |
| 356 |
|
| 357 |
.. data:: password_changed |
| 358 |
|
| 359 |
Sent when a user completes a password change. In addition to the app (which is |
| 360 |
the sender), it is passed a `user` argument. |
| 361 |
|
| 362 |
.. data:: reset_password_instructions_sent |
| 363 |
|
| 364 |
Sent when a user requests a password reset. In addition to the app (which is |
| 365 |
the sender), it is passed `user`, `token` (deprecated), and `reset_token` arguments. |
| 366 |
|
| 367 |
.. data:: change_email_instructions_sent |
| 368 |
|
| 369 |
Sent when a user requests to change their registered email address. In addition to the |
| 370 |
app (which is the sender) it is passed `user`, `token`, and `new_email`. |
| 371 |
|
| 372 |
.. versionadded:: 5.5.0 |
| 373 |
|
| 374 |
.. data:: change_email_confirmed |
| 375 |
|
| 376 |
Sent when a user has confirmed their new email address. In addition to the |
| 377 |
app (which is the sender) it is passed `user`, `old_email`. |
| 378 |
|
| 379 |
.. versionadded:: 5.5.0 |
| 380 |
|
| 381 |
.. data:: refresh_tracker_created |
| 382 |
|
| 383 |
Sent when a user has authenticated, has requested an auth_token, and |
| 384 |
the :py:data:`SECURITY_REFRESH_TOKEN` feature is enabled. |
| 385 |
In addition to the app (which is the sender), it is passed the `user` and |
| 386 |
the `refresh_tracker` objects. |
| 387 |
|
| 388 |
.. versionadded:: 5.9.0 |
| 389 |
|
| 390 |
.. data:: refresh_tracker_revoked |
| 391 |
|
| 392 |
Sent when a refresh tracker is revoked as part of the ``/refresh`` view |
| 393 |
when the provided refresh token doesn't match the current generation #. |
| 394 |
In addition to the app (which is the sender), it is passed the `user` and |
| 395 |
the `refresh_tracker` objects, and the `refresh_error` tuple. |
| 396 |
|
| 397 |
.. versionadded:: 5.9.0 |
| 398 |
|
| 399 |
.. data:: tf_code_confirmed |
| 400 |
|
| 401 |
Sent when a user performs two-factor authentication login on the site. In |
| 402 |
addition to the app (which is the sender), it is passed `user` |
| 403 |
and `method` arguments. |
| 404 |
|
| 405 |
.. versionadded:: 3.3.0 |
| 406 |
|
| 407 |
.. data:: tf_profile_changed |
| 408 |
|
| 409 |
Sent when two-factor is used and user logs in. In addition to the app |
| 410 |
(which is the sender), it is passed `user` and `method` arguments. |
| 411 |
|
| 412 |
.. versionadded:: 3.3.0 |
| 413 |
|
| 414 |
.. data:: tf_disabled |
| 415 |
|
| 416 |
Sent when two-factor is disabled. In addition to the app |
| 417 |
(which is the sender), it is passed `user` argument. |
| 418 |
|
| 419 |
.. versionadded:: 3.3.0 |
| 420 |
|
| 421 |
.. data:: tf_security_token_sent |
| 422 |
|
| 423 |
Sent when a two factor security/access code is sent. In addition to the app |
| 424 |
(which is the sender), it is passed `user`, `method`, `login_token` and `token` (deprecated) arguments. |
| 425 |
|
| 426 |
.. versionadded:: 3.3.0 |
| 427 |
|
| 428 |
.. data:: username_changed |
| 429 |
|
| 430 |
Sent when a username is successfully changed. In addition to the |
| 431 |
app (which is the sender), it is passed the `user` and `old_username` arguments. |
| 432 |
|
| 433 |
.. data:: username_recovery_email_sent |
| 434 |
|
| 435 |
Sent when a username is successfully recovered and sent over email. In addition to the |
| 436 |
app (which is the sender), it is passed the `user` argument. |
| 437 |
|
| 438 |
.. versionadded:: 5.6.0 |
| 439 |
|
| 440 |
.. data:: us_security_token_sent |
| 441 |
|
| 442 |
Sent when a unified sign in access code is sent. In addition to the app |
| 443 |
(which is the sender), it is passed `user`, `method`, `token` (deprecated), |
| 444 |
`login_token`, |
| 445 |
`phone_number`, and `send_magic_link` arguments. |
| 446 |
|
| 447 |
.. versionadded:: 3.4.0 |
| 448 |
|
| 449 |
.. data:: us_profile_changed |
| 450 |
|
| 451 |
Sent when user completes changing their unified sign in profile. In addition to the app |
| 452 |
(which is the sender), it is passed `user`, `methods`, and `delete` arguments. |
| 453 |
`delete` will be set to ``True`` if the user removed a sign in option. |
| 454 |
|
| 455 |
.. versionadded:: 3.4.0 |
| 456 |
|
| 457 |
.. versionchanged:: 5.0.0 |
| 458 |
Added delete argument and changed `method` to `methods` which is now a list. |
| 459 |
|
| 460 |
.. data:: wan_registered |
| 461 |
|
| 462 |
Sent when a WebAuthn credential was successfully created. In addition to the app |
| 463 |
(which is the sender), it is passed `user` and `name` arguments. |
| 464 |
|
| 465 |
.. versionadded:: 5.0.0 |
| 466 |
|
| 467 |
.. data:: wan_deleted |
| 468 |
|
| 469 |
Sent when a WebAuthn credential was deleted. In addition to the app |
| 470 |
(which is the sender), it is passed `user` and `name` arguments. |
| 471 |
|
| 472 |
.. versionadded:: 5.0.0 |
| 473 |
|
| 474 |
.. _Flask async: https://flask.palletsprojects.com/en/3.0.x/async-await/#using-async-and-await |
| 475 |
.. _Flask documentation on signals: https://flask.palletsprojects.com/en/2.3.x/signals/ |
| 476 |
.. _Blinker async: https://blinker.readthedocs.io/en/stable/#async-receivers |