Configuration ============= .. warning:: Be sure to set all configuration (in app.config["xxx"]) *PRIOR* to instantiating the Security class or calling security.init_app(). The following configuration values are used by Flask-Security: Core -------------- These configuration keys are used globally across all features. .. py:data:: SECRET_KEY This is actually part of Flask - but is used by Flask-Security to sign all tokens. It is critical this is set to a strong value. For python3 consider using: ``secrets.token_urlsafe()`` .. py:data:: SECRET_KEY_FALLBACKS This is part of Flask (>=3.1) but can be used by Flask-Security to unsign tokens. See Flask documentation https://flask.palletsprojects.com/en/stable/config/#SECRET_KEY_FALLBACKS .. versionadded:: 5.6.0 .. py:data:: SECURITY_BLUEPRINT_NAME Specifies the name for the Flask-Security blueprint. Default: ``"security"``. .. py:data:: SECURITY_URL_PREFIX Specifies the URL prefix for the Flask-Security blueprint. Default: ``None``. .. py:data:: SECURITY_STATIC_FOLDER Specifies the folder name for static files (webauthn). Default: ``"static"``. .. versionadded:: 5.1.0 .. py:data:: SECURITY_STATIC_FOLDER_URL Specifies the URL for static files used by Flask-Security (webauthn). See Flask documentation https://flask.palletsprojects.com/en/latest/blueprints/#static-files Default: ``"/fs-static"``. .. versionadded:: 5.1.0 .. py:data:: SECURITY_SUBDOMAIN Specifies the subdomain for the Flask-Security blueprint. If your authenticated content is on a different subdomain, also enable :py:data:`SECURITY_REDIRECT_ALLOW_SUBDOMAINS`. Default: ``None``. .. py:data:: SECURITY_FLASH_MESSAGES Specifies whether or not to flash messages for actions certain endpoint perform. Normally Flash-Security views will flash informational or error messages only when the operation results in a redirect. Default: ``True``. .. py:data:: SECURITY_I18N_DOMAIN Specifies the name for domain used for translations. Default: ``"flask_security"``. .. py:data:: SECURITY_I18N_DIRNAME Specifies the directory containing the ``MO`` files used for translations. When using flask-babel this can also be a list of directory names - this enables application to override a subset of messages if desired. The default ``builtin`` uses translations shipped with Flask-Security. Default: ``"builtin"``. .. versionchanged:: 5.2.0 "builtin" is a special name which will be interpreted as the ``translations`` directory within the installation of Flask-Security. .. py:data:: SECURITY_TOKEN_AUTHENTICATION_KEY Specifies the query string parameter to read when using token authentication. Default: ``"auth_token"``. .. py:data:: SECURITY_TOKEN_AUTHENTICATION_HEADER Specifies the HTTP header to read when using token authentication. Default: ``"Authentication-Token"``. .. py:data:: SECURITY_TOKEN_MAX_AGE Specifies the number of seconds before an authentication token expires. Default: ``None``, meaning the token never expires. .. py:data:: SECURITY_TOKEN_EXPIRE_TIMESTAMP A callable that returns a unix timestamp in the future when this specific authentication token should expire. Returning 0 means no expiration. It is passed the currently authenticated User so any fields can be used to customize an expiration time. Of course it is called in a request context so any information about the current request can also be used. If BOTH this and :data:`SECURITY_TOKEN_MAX_AGE` are set - the shorter is used. .. note:: These 2 expiry options work differently - with this one, the actual expire timestamp is in the auth_token. The signed token (using itsdangerous) has the timestamp the token was generated. On validation, that is checked against ``SECURITY_TOKEN_MAX_AGE``. So for MAX_AGE, at the time of validation, the token hasn't yet been associated with a User. Default: ``lambda user: 0`` .. py:data:: SECURITY_CACHE_CONTROL Specifies the Cache-Control header attributes to be sent with all responses from Flask-Security endpoints. Each entry in the dict is a (attribute, value) pair, where allowed attributes are defined in the Flask documentation https://flask.palletsprojects.com/en/stable/api/#flask.Response.cache_control. .. note:: Flask-Security does not pretend to handle the Cache-Control header for your application. This default setting is to ensure that out of the box, Flask-Security responses which might contain sensitive information aren't cached. Please see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control#cache_directives. Responses from your own application endpoints (including endpoints protected with a Flask-Security decorator, such as token-authenticated APIs) are not modified - set the Cache-Control header there yourself if needed. .. note:: Flask itself adds the 'Vary: Cookie' header to all responses that access the session (cookie). This of course includes all non-anonymous Flask-Security endpoints as well as any application endpoints that are decorated with a Flask-Security decorator. This alone is likely enough to ensure no personal information is leaked. Default: ``{"private": True, "no-store": True}``. .. versionadded:: 5.8.0 .. py:data:: SECURITY_EMAIL_VALIDATOR_ARGS Email address are validated and normalized via the ``mail_util_cls`` which defaults to :class:`.MailUtil`. That uses the `email_validator`_ package whose methods have configurable options - these can be set here and will be passed in. For example setting this to: ``{"check_deliverability": False}`` is useful when unit testing if the emails are fake. ``mail_util_cls`` has 2 methods - ``normalize`` and ``validate``. Both ensure the passed value is a valid email address, and returns a normalized version. ``validate`` additionally, by default, verifies that the email address can likely actually receive an email. Default: ``None``, meaning use the defaults from email_validator package. .. versionadded:: 4.0.0 .. _email_validator: https://pypi.org/project/email-validator/ .. py:data:: SECURITY_DEFAULT_HTTP_AUTH_REALM Specifies the default authentication realm when using basic HTTP auth. Default: ``Login Required`` .. py:data:: SECURITY_REDIRECT_BEHAVIOR Passwordless login, confirmation, reset password, unified signin, change_email, and Social Login have GET endpoints that validate the passed token and redirect to an action form. For Single-Page-Applications style UIs which need to control their own internal URL routing these redirects need to not contain forms, but contain relevant information as query parameters. Setting this to ``"spa"`` will enable that behavior. When this is enabled, the following must also be defined: - :py:data:`SECURITY_POST_OAUTH_LOGIN_VIEW` (if :py:data:`SECURITY_OAUTH_ENABLE` is True) - :py:data:`SECURITY_POST_OAUTH_VERIFY_VIEW` (if :py:data:`SECURITY_OAUTH_ENABLE` is True) - :py:data:`SECURITY_VERIFY_ERROR_VIEW` (if :py:data:`SECURITY_OAUTH_ENABLE` is True) - :py:data:`SECURITY_LOGIN_ERROR_VIEW` - :py:data:`SECURITY_CONFIRM_ERROR_VIEW` - :py:data:`SECURITY_POST_CHANGE_EMAIL_VIEW` - :py:data:`SECURITY_CHANGE_EMAIL_ERROR_VIEW` - :py:data:`SECURITY_POST_CONFIRM_VIEW` - :py:data:`SECURITY_RESET_ERROR_VIEW` - :py:data:`SECURITY_RESET_VIEW` Default: ``None`` which is existing html-style form redirects. .. versionadded:: 3.3.0 .. py:data:: SECURITY_REDIRECT_HOST Mostly for development purposes, the UI is often developed separately and is running on a different port than the Flask application. In order to test redirects, the `netloc` of the redirect URL needs to be rewritten. Setting this to e.g. `localhost:8080` does that. .. tip:: Be aware that when this is set, any of the `*_VIEW` configuration variables that are set to URLs and not endpoints, will be redirected to this host. Default: ``None``. .. versionadded:: 3.3.0 .. py:data:: SECURITY_REDIRECT_ALLOW_SUBDOMAINS If ``True`` then subdomains (and the root domain) of the top-level host set by Flask's ``SERVER_NAME`` configuration will be allowed as post-view redirect targets. This is beneficial if you wish to place your authentication on one subdomain and authenticated content on another, for example ``auth.domain.tld`` and ``app.domain.tld``. Default: ``False``. .. versionadded:: 4.0.0 .. py:data:: SECURITY_REDIRECT_BASE_DOMAIN Set the base domain for checking allowable redirects. The intent here is to allow an application to be served on e.g. "flaskapp.my.org" and redirect to "myservice.my.org" (which maybe isn't a Flask app). Flask's SERVER_NAME can't be used to verify redirects in this case. Note that in most cases the application will want to set Flask's SESSION_COOKIE_DOMAIN to be this base domain - otherwise authorization information won't be sent. Default: ``None`` .. versionadded:: 5.5.0 .. py:data:: SECURITY_REDIRECT_ALLOWED_SUBDOMAINS A list of subdomains. Each will be prepended to ``SECURITY_REDIRECT_BASE_DOMAIN`` and checked against the requested redirect. Default: ``[]`` .. versionadded:: 5.5.0 .. note:: The above 4 config options apply BOTH to the handling of ``next`` parameter as well as all the ``XXX_VIEW`` URL configuration options for those views that perform a redirect after processing. .. py:data:: SECURITY_CSRF_PROTECT_MECHANISMS Authentication mechanisms that require CSRF protection. These are the same mechanisms as are permitted in the ``@auth_required`` decorator. Default: ``("basic", "session", "token")``. .. py:data:: SECURITY_CSRF_IGNORE_UNAUTH_ENDPOINTS If ``True`` then CSRF will not be required for endpoints that don't require authentication (e.g. login, logout, register, forgot_password). Default: ``False``. .. py:data:: SECURITY_CSRF_COOKIE_NAME The name for the CSRF cookie. This usually should be dictated by your client-side code - more information can be found at :ref:`csrf_topic` Default: ``None`` - meaning no cookie will be sent. .. py:data:: SECURITY_CSRF_COOKIE A dict that defines the parameters required to set a CSRF cookie. The complete set of parameters is described in Flask's `set_cookie`_ documentation. Default: ``{"samesite": "Strict", "httponly": False, "secure": False}`` .. versionchanged:: 4.1.0 The 'key' attribute was deprecated in favor of a separate configuration variable :data:`SECURITY_CSRF_COOKIE_NAME`. .. py:data:: SECURITY_CSRF_HEADER The HTTP Header name that will contain the CSRF token. ``X-XSRF-Token`` is used by packages such as `axios`_. Default: ``"X-XSRF-Token"``. .. py:data:: SECURITY_CSRF_COOKIE_REFRESH_EACH_REQUEST By default, csrf_tokens have an expiration (controlled by the configuration variable ``WTF_CSRF_TIME_LIMIT``. This can cause CSRF failures if say an application is left idle for a long time. You can set that time limit to ``None`` or have the CSRF cookie sent on every request (which will give it a new expiration time). Default: ``False``. .. py:data:: SECURITY_SCRIPT_NONCE_KEY If set, adds a nonce attribute to all ``