-
Notifications
You must be signed in to change notification settings - Fork 1
/
peewee_encrypted_field.py
116 lines (88 loc) · 3.7 KB
/
peewee_encrypted_field.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name: peewee_encrypted_field.py
# Module: peewee_encrypted_field
#
# Created: 02.12.2015 14:01
# Copyright: (c) Constantin Roganov, 2015 - 2016
# MIT License
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-------------------------------------------------------------------------------
#!/usr/bin/env python
"""Encrypted field for Peewee.
Saves data to the DB as Fernet tokens.
For details about fernet see https://github.com/fernet/spec
Uses simple fernet implementation https://github.com/heroku/fernet-py
Idea caught from SQLAlchemy.
"""
from __future__ import unicode_literals
import fernet
import fernet.secret as secret
from peewee import *
__author__ = 'Constantin Roganov'
class KeyIsInvalid(Exception): pass
def validate_key(key):
"""Perform a simple key validation.
In case key is invalid raises exception.
"""
try:
secret.Secret(key)
except secret.Secret.InvalidSecret as e:
raise KeyIsInvalid(e.message)
class EncryptedField(Field):
"""Encrypted field.
Encrypted content is a Fernet token.
Field maintains a list of keys as map of maps where the first key is
model_class and second key is id(self_field).
"""
class KeyIsUndefined(RuntimeError): pass
class KeyAlreadyExists(RuntimeError): pass
_keys = {}
db_field = 'text'
def db_value(self, value):
if not self.key:
raise EncryptedField.KeyIsUndefined()
return fernet.generate(self.key, value)
def python_value(self, value):
if not self.key:
raise EncryptedField.KeyIsUndefined()
return super(EncryptedField, self).python_value(
fernet.Token(value, self.key, enforce_ttl=False).message()
)
def key_exists(self):
return (self.model_class in EncryptedField._keys
and id(self) in EncryptedField._keys[self.model_class])
@property
def key(self):
return (
EncryptedField._keys[self.model_class][id(self)] if self.key_exists() else None
)
@key.setter
def key(self, key_val):
"""Allows to set key only once"""
if self.key_exists():
raise EncryptedField.KeyAlreadyExists()
validate_key(key_val)
if self.model_class not in self._keys:
EncryptedField._keys[self.model_class] = {}
EncryptedField._keys[self.model_class][id(self)] = key_val
@key.deleter
def key(self):
if self.key_exists():
del EncryptedField._keys[self.model_class][id(self)]