This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
67 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import os | ||
import binascii | ||
from flask import session, request | ||
|
||
|
||
TOKEN = '_csrf_token' | ||
REACT_HEADER_NAME = 'X-CSRFToken' | ||
|
||
|
||
def random_string(length=32): | ||
return binascii.b2a_hex(os.urandom(length)).decode('utf-8') | ||
|
||
|
||
def get_csrf_token(): | ||
if TOKEN not in session: | ||
session[TOKEN] = random_string() | ||
return session[TOKEN] | ||
|
||
|
||
def check_valid_header_csrf(): | ||
try: | ||
return session[TOKEN] == request.headers[REACT_HEADER_NAME] | ||
except KeyError: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters