當前位置: 首頁>>代碼示例>>Python>>正文


Python csrf.generate_csrf方法代碼示例

本文整理匯總了Python中flask_wtf.csrf.generate_csrf方法的典型用法代碼示例。如果您正苦於以下問題:Python csrf.generate_csrf方法的具體用法?Python csrf.generate_csrf怎麽用?Python csrf.generate_csrf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在flask_wtf.csrf的用法示例。


在下文中一共展示了csrf.generate_csrf方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: csrf_token

# 需要導入模塊: from flask_wtf import csrf [as 別名]
# 或者: from flask_wtf.csrf import generate_csrf [as 別名]
def csrf_token(self):
        # First, we'll wrap our request shim around the test client, so that
        # it will work correctly when Flask asks it to set a cookie.
        request = RequestShim(self) 
        # Next, we need to look up any cookies that might already exist on
        # this test client, such as the secure cookie that powers `flask.session`,
        # and make a test request context that has those cookies in it.
        environ_overrides = {}
        self.cookie_jar.inject_wsgi(environ_overrides)
        with flask.current_app.test_request_context(
                "/login", environ_overrides=environ_overrides,
            ):
            # Now, we call Flask-WTF's method of generating a CSRF token...
            csrf_token = generate_csrf()
            # ...which also sets a value in `flask.session`, so we need to
            # ask Flask to save that value to the cookie jar in the test
            # client. This is where we actually use that request shim we made! 
            flask.current_app.save_session(flask.session, request)
            # And finally, return that CSRF token we got from Flask-WTF.
            return csrf_token 
開發者ID:scragg0x,項目名稱:realms-wiki,代碼行數:22,代碼來源:flask_csrf_test_client.py

示例2: base_render_json

# 需要導入模塊: from flask_wtf import csrf [as 別名]
# 或者: from flask_wtf.csrf import generate_csrf [as 別名]
def base_render_json(
    form,
    include_user=True,
    include_auth_token=False,
    additional=None,
    error_status_code=400,
):
    has_errors = len(form.errors) > 0

    user = form.user if hasattr(form, "user") else None
    if has_errors:
        code = error_status_code
        payload = json_error_response(errors=form.errors)
    else:
        code = 200
        payload = dict()
        if user:
            # This allows anonymous GETs via JSON
            if include_user:
                payload["user"] = user.get_security_payload()

            if include_auth_token:
                # view wants to return auth_token - check behavior config
                if (
                    config_value("BACKWARDS_COMPAT_AUTH_TOKEN")
                    or "include_auth_token" in request.args
                ):
                    token = user.get_auth_token()
                    payload["user"]["authentication_token"] = token

        # Return csrf_token on each JSON response - just as every form
        # has it rendered.
        payload["csrf_token"] = csrf.generate_csrf()
        if additional:
            payload.update(additional)

    return _security._render_json(payload, code, headers=None, user=user) 
開發者ID:Flask-Middleware,項目名稱:flask-security,代碼行數:39,代碼來源:utils.py

示例3: set_xsrf_cookie

# 需要導入模塊: from flask_wtf import csrf [as 別名]
# 或者: from flask_wtf.csrf import generate_csrf [as 別名]
def set_xsrf_cookie(response):
    """
    Sets the csrf token used by csrf protect as a cookie to allow usage with
    react.
    """
    response.set_cookie("X-CSRF", generate_csrf())
    return response 
開發者ID:Flowminder,項目名稱:FlowKit,代碼行數:9,代碼來源:main.py

示例4: configure_app

# 需要導入模塊: from flask_wtf import csrf [as 別名]
# 或者: from flask_wtf.csrf import generate_csrf [as 別名]
def configure_app(app, config_object):
    """General application configuration:

    - register the app's config
    - register Jinja extensions
    - register functions to run on before/after request
    """
    # automatically configure a migrations folder for each bundle
    config_object.ALEMBIC['version_locations'] = [
        (bundle._name, os.path.join(PROJECT_ROOT,
                                    bundle.module_name.replace('.', os.sep),
                                    'migrations'))
        for bundle in app.bundles if bundle.has_models
    ]
    app.config.from_object(config_object)

    app.jinja_env.add_extension('jinja2_time.TimeExtension')

    @app.before_request
    def enable_session_timeout():
        session.permanent = True  # set session to use PERMANENT_SESSION_LIFETIME
        session.modified = True   # reset the session timer on every request

    @app.after_request
    def set_csrf_cookie(response):
        if response:
            response.set_cookie('csrf_token', generate_csrf())
        return response 
開發者ID:briancappello,項目名稱:flask-react-spa,代碼行數:30,代碼來源:app.py

示例5: inject_csrf_token

# 需要導入模塊: from flask_wtf import csrf [as 別名]
# 或者: from flask_wtf.csrf import generate_csrf [as 別名]
def inject_csrf_token(response):
    response.headers.set("X-CSRF-Token", generate_csrf())
    return response


# EXPERIMENT PORTAL ############################################################ 
開發者ID:expfactory,項目名稱:expfactory,代碼行數:8,代碼來源:main.py

示例6: init_app

# 需要導入模塊: from flask_wtf import csrf [as 別名]
# 或者: from flask_wtf.csrf import generate_csrf [as 別名]
def init_app(self, app):
        app.extensions['csrf'] = self

        app.config.setdefault('WTF_CSRF_ENABLED', True)
        app.config.setdefault('WTF_CSRF_CHECK_DEFAULT', True)
        app.config['WTF_CSRF_METHODS'] = set(app.config.get(
            'WTF_CSRF_METHODS', ['POST', 'PUT', 'PATCH', 'DELETE']
        ))
        app.config.setdefault('WTF_CSRF_FIELD_NAME', 'csrf_token')
        app.config.setdefault(
            'WTF_CSRF_HEADERS', ['X-CSRFToken', 'X-CSRF-Token']
        )
        app.config.setdefault('WTF_CSRF_TIME_LIMIT', 3600)
        app.config.setdefault('WTF_CSRF_SSL_STRICT', True)

        app.jinja_env.globals['csrf_token'] = generate_csrf
        app.context_processor(lambda: {'csrf_token': generate_csrf})

        def _set_csrf_valid_true():
            """設置驗證通過, 滿足 _FlaskFormCSRF.validate_csrf_token 內判斷"""
            g.csrf_valid = True
            return

        @app.before_request
        def csrf_protect():
            if not app.config['WTF_CSRF_ENABLED']:
                return _set_csrf_valid_true()

            if not app.config['WTF_CSRF_CHECK_DEFAULT']:
                return _set_csrf_valid_true()

            if request.method not in app.config['WTF_CSRF_METHODS']:
                return _set_csrf_valid_true()

            if not request.endpoint:
                return _set_csrf_valid_true()

            view = app.view_functions.get(request.endpoint)

            if not view:
                return _set_csrf_valid_true()

            if request.blueprint in self._exempt_blueprints:
                return _set_csrf_valid_true()

            dest = '%s.%s' % (view.__module__, view.__name__)

            if dest in self._exempt_views:
                return _set_csrf_valid_true()

            self.protect() 
開發者ID:fufuok,項目名稱:FF.PyAdmin,代碼行數:53,代碼來源:csrf.py


注:本文中的flask_wtf.csrf.generate_csrf方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。