-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from gnosischain/dev
Add support for CSRF.
- Loading branch information
Showing
18 changed files
with
243 additions
and
43 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import requests | ||
import logging | ||
|
||
import requests | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
|
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,48 @@ | ||
|
||
import random | ||
|
||
from Crypto.Cipher import PKCS1_OAEP | ||
from Crypto.PublicKey import RSA | ||
|
||
|
||
class CSRFTokenItem: | ||
def __init__(self, request_id, token): | ||
self.request_id = request_id | ||
self.token = token | ||
|
||
|
||
class CSRFToken: | ||
def __init__(self, privkey, salt): | ||
self._privkey = RSA.import_key(privkey) | ||
self._pubkey = self._privkey.publickey() | ||
self._salt = salt | ||
|
||
def generate_token(self): | ||
request_id = '%d' % random.randint(0, 1000) | ||
data_to_encrypt = '%s%s' % (request_id, self._salt) | ||
|
||
cipher_rsa = PKCS1_OAEP.new(self._pubkey) | ||
token = cipher_rsa.encrypt(data_to_encrypt.encode()) | ||
|
||
return CSRFTokenItem(request_id, token.hex()) | ||
|
||
def validate_token(self, request_id, token): | ||
try: | ||
cipher_rsa = PKCS1_OAEP.new(self._privkey) | ||
decrypted_text = cipher_rsa.decrypt(bytes.fromhex(token)).decode() | ||
|
||
expected_text = '%s%s' % (request_id, self._salt) | ||
if decrypted_text == expected_text: | ||
return True | ||
return False | ||
except Exception: | ||
return False | ||
|
||
|
||
class CSRF: | ||
_instance = None | ||
|
||
def __new__(cls, privatekey, salt): | ||
if not hasattr(cls, 'instance'): | ||
cls.instance = CSRFToken(privatekey, salt) | ||
return cls.instance |
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
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
Oops, something went wrong.