6 examples of 'csrf_token django' in Python

Every line of 'csrf_token django' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
36def get_csrf_token(request):
37 return request.session.get_csrf_token()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

77def test_csrf_token(self):
78 request = HttpRequest()
79 CsrfViewMiddleware().process_view(request, lambda r: None, (), {})
80
81 template = self.engine.get_template('template_backends/csrf.html')
82 content = template.render(request=request)
83
84 expected = '<input type="hidden" name="csrfmiddlewaretoken" value="([^"]+)">'
85 match = re.match(expected, content) or re.match(expected.replace('"', "'"), content)
86 self.assertTrue(match, "hidden csrftoken field not found in output")
87 self.assertTrue(equivalent_tokens(match.group(1), get_token(request)))
420def test_csrf_token_not_in_cookie(app):
421 test_client = app.test_client()
422
423 app.config['JWT_CSRF_IN_COOKIES'] = False
424
425 # Test the default access cookies
426 response = test_client.get('/access_token')
427 cookies = response.headers.getlist('Set-Cookie')
428 assert len(cookies) == 1
429 access_cookie = _get_cookie_from_response(response, 'access_token_cookie')
430 assert access_cookie is not None
431
432 # Test the default refresh cookies
433 response = test_client.get('/refresh_token')
434 cookies = response.headers.getlist('Set-Cookie')
435 assert len(cookies) == 1
436 refresh_cookie = _get_cookie_from_response(response, 'refresh_token_cookie')
437 assert refresh_cookie is not None
147@override_settings(CSRF_HEADER_NAME='HTTP_X_CSRFTOKEN_CUSTOMIZED')
148def test_csrf_token_in_header_with_customized_name(self):
149 """
150 settings.CSRF_HEADER_NAME can be used to customize the CSRF header name
151 """
152 req = self._get_POST_csrf_cookie_request()
153 req.META['HTTP_X_CSRFTOKEN_CUSTOMIZED'] = self._csrf_id
154 self.mw.process_request(req)
155 req2 = self.mw.process_view(req, post_form_view, (), {})
156 self.assertIsNone(req2)
76def rosetta_csrf_token(parser, token):
77 try:
78 from django.template.defaulttags import csrf_token
79 return csrf_token(parser, token)
80 except ImportError:
81 return RosettaCsrfTokenPlaceholder()
96def make_csrf_cookie(name, tok_length):
97
98 tok = ''.join(
99 random.choice(string.ascii_letters) for x in range(tok_length)
100 )
101 return dump_cookie(name, value=tok)

Related snippets