-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ADD custom encrypted fields #312
Open
kamil1marczak
wants to merge
1
commit into
chibisov:master
Choose a base branch
from
kamil1marczak:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ python: | |
- 3.8 | ||
|
||
install: | ||
- python -m pip install -U pip | ||
- pip install tox tox-travis | ||
|
||
script: | ||
|
Binary file not shown.
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,3 +1,3 @@ | ||
__version__ = '0.7.0' # from 0.7.0 | ||
__version__ = '0.7.1' # from 0.7.0 | ||
|
||
VERSION = __version__ |
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ nose | |
django-nose | ||
django-filter>=2.1.0 | ||
mock | ||
ipdb | ||
ipdb | ||
cryptography==3.2.1 |
Empty file.
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,166 @@ | ||
import time | ||
|
||
from rest_framework import serializers | ||
from rest_framework_extensions.fields import ( | ||
CryptoBinaryField, | ||
CryptoCharField, | ||
) | ||
import datetime | ||
from django.test import TestCase | ||
from django.conf import settings | ||
|
||
import base64 | ||
|
||
from cryptography.fernet import Fernet | ||
from cryptography.hazmat.primitives import hashes | ||
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC | ||
|
||
DEFAULT_PASSWORD = b"Non_nobis1solum?nati!sumus" | ||
DEFAULT_SALT = settings.SECRET_KEY | ||
|
||
|
||
def _generate_password_key(salt=DEFAULT_SALT, password=DEFAULT_PASSWORD): | ||
kdf = PBKDF2HMAC( | ||
algorithm=hashes.SHA256(), | ||
length=32, | ||
salt=_to_bytes(salt), | ||
iterations=100000, | ||
) | ||
|
||
key = base64.urlsafe_b64encode(kdf.derive(_to_bytes(password))) | ||
return key | ||
|
||
|
||
def _to_bytes(v): | ||
if isinstance(v, str): | ||
return v.encode("utf-8") | ||
|
||
if isinstance(v, bytes): | ||
return v | ||
|
||
|
||
|
||
def _encrypt(token, value_in_str): | ||
b_message = value_in_str.encode("utf-8") | ||
encrypted_message = token.encrypt(b_message) | ||
return encrypted_message | ||
|
||
|
||
def _decrypt(token, value, ttl=None): | ||
ttl = int(ttl) if ttl else None | ||
decrypted_message = token.decrypt(_to_bytes(value), ttl) | ||
return decrypted_message.decode("utf-8") | ||
|
||
|
||
class SaveCrypto(object): | ||
def __init__(self, message=None, created=None): | ||
self.message = message | ||
self.created = created or datetime.datetime.now() | ||
|
||
|
||
class CryptoSerializer(serializers.Serializer): | ||
message = CryptoBinaryField(required=False) | ||
created = serializers.DateTimeField() | ||
|
||
def update(self, instance, validated_data): | ||
instance.message = validated_data["message"] | ||
return instance | ||
|
||
def create(self, validated_data): | ||
return SaveCrypto(**validated_data) | ||
|
||
|
||
class CryptoCharSerializer(serializers.Serializer): | ||
message = CryptoCharField(required=False) | ||
created = serializers.DateTimeField() | ||
|
||
|
||
class SaltCryptoSerializerSerializer(CryptoSerializer): | ||
message = CryptoBinaryField(salt="Salt") | ||
created = serializers.DateTimeField() | ||
|
||
|
||
class PasswordCryptoSerializerSerializer(CryptoSerializer): | ||
message = CryptoBinaryField(password="Password") | ||
created = serializers.DateTimeField() | ||
|
||
|
||
class TtlCryptoSerializerSerializer(CryptoSerializer): | ||
message = CryptoBinaryField(ttl=1) | ||
created = serializers.DateTimeField() | ||
|
||
|
||
class TestPointSerializer(TestCase): | ||
def test_create(self): | ||
""" | ||
Test for creating CryptoBinaryField | ||
""" | ||
now = datetime.datetime.now() | ||
message = "test message" | ||
serializer = CryptoSerializer(data={"created": now, "message": message}) | ||
model_data = SaveCrypto(message=message, created=now) | ||
|
||
self.assertTrue(serializer.is_valid()) | ||
self.assertEqual(serializer.validated_data["created"], model_data.created) | ||
self.assertFalse(serializer.validated_data is model_data) | ||
self.assertIs(type(serializer.validated_data["message"]), bytes) | ||
|
||
def test_create_char(self): | ||
""" | ||
Test for creating CryptoCharField | ||
""" | ||
now = datetime.datetime.now() | ||
message = "test message" | ||
serializer = CryptoCharSerializer(data={"created": now, "message": message}) | ||
model_data = SaveCrypto(message=message, created=now) | ||
|
||
self.assertTrue(serializer.is_valid()) | ||
self.assertEqual(serializer.validated_data["created"], model_data.created) | ||
self.assertFalse(serializer.validated_data is model_data) | ||
self.assertIs(type(serializer.validated_data["message"]), str) | ||
|
||
def test_serialization(self): | ||
""" | ||
Regular JSON serialization should output float values | ||
""" | ||
now = datetime.datetime.now() | ||
message = "test message" | ||
key = _generate_password_key(DEFAULT_SALT, DEFAULT_PASSWORD) | ||
token = Fernet(key) | ||
encrypted_message = _encrypt(token, message) | ||
model_data = SaveCrypto(message=encrypted_message, created=now) | ||
serializer = CryptoSerializer(model_data) | ||
self.assertEqual(serializer.data["message"], message) | ||
|
||
def test_serialization_salt(self): | ||
now = datetime.datetime.now() | ||
message = "test message" | ||
key = _generate_password_key("Salt", DEFAULT_PASSWORD) | ||
token = Fernet(key) | ||
encrypted_message = _encrypt(token, message) | ||
model_data = SaveCrypto(message=encrypted_message, created=now) | ||
serializer = SaltCryptoSerializerSerializer(model_data) | ||
time.sleep(3) | ||
self.assertEqual(serializer.data["message"], message) | ||
|
||
def test_serialization_password(self): | ||
now = datetime.datetime.now() | ||
message = "test message" | ||
key = _generate_password_key(DEFAULT_SALT, "Password") | ||
token = Fernet(key) | ||
encrypted_message = _encrypt(token, message) | ||
model_data = SaveCrypto(message=encrypted_message, created=now) | ||
serializer = PasswordCryptoSerializerSerializer(model_data) | ||
time.sleep(3) | ||
self.assertEqual(serializer.data["message"], message) | ||
|
||
def test_serialization_ttl(self): | ||
now = datetime.datetime.now() | ||
message = "test message" | ||
key = _generate_password_key(DEFAULT_SALT, DEFAULT_PASSWORD) | ||
token = Fernet(key) | ||
encrypted_message = _encrypt(token, message) | ||
model_data = SaveCrypto(message=encrypted_message, created=now) | ||
serializer = TtlCryptoSerializerSerializer(model_data) | ||
time.sleep(3) | ||
self.assertEqual(serializer.data["message"], None) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
python 2 compatibilities is not needed anymore so just super().init(*, **)