-
Notifications
You must be signed in to change notification settings - Fork 51
/
fields.py
156 lines (102 loc) · 4.91 KB
/
fields.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from django.db import models
from pgcrypto import (
DIGEST_SQL,
HMAC_SQL,
PGP_PUB_ENCRYPT_SQL_WITH_NULLIF,
PGP_SYM_ENCRYPT_SQL_WITH_NULLIF,
)
from pgcrypto.lookups import (
HashLookup,
)
from pgcrypto.mixins import (
DecimalPGPFieldMixin,
get_setting,
HashMixin,
PGPPublicKeyFieldMixin,
PGPSymmetricKeyFieldMixin,
)
class TextDigestField(HashMixin, models.TextField):
"""Text digest field for postgres."""
encrypt_sql = DIGEST_SQL
def get_encrypt_sql(self, connection):
"""Get encrypt sql."""
return self.encrypt_sql.format(get_setting(connection, 'PGCRYPTO_KEY'))
TextDigestField.register_lookup(HashLookup)
class TextHMACField(HashMixin, models.TextField):
"""Text HMAC field for postgres."""
encrypt_sql = HMAC_SQL
TextHMACField.register_lookup(HashLookup)
class EmailPGPPublicKeyField(PGPPublicKeyFieldMixin, models.EmailField):
"""Email PGP public key encrypted field."""
class IntegerPGPPublicKeyField(PGPPublicKeyFieldMixin, models.IntegerField):
"""Integer PGP public key encrypted field."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'INT4'
class BigIntegerPGPPublicKeyField(PGPPublicKeyFieldMixin, models.IntegerField):
"""BigInteger PGP public key encrypted field."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'BIGINT'
class TextPGPPublicKeyField(PGPPublicKeyFieldMixin, models.TextField):
"""Text PGP public key encrypted field."""
class CharPGPPublicKeyField(PGPPublicKeyFieldMixin, models.CharField):
"""Char PGP public key encrypted field."""
class DatePGPPublicKeyField(PGPPublicKeyFieldMixin, models.DateField):
"""Date PGP public key encrypted field for postgres."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'DATE'
class DateTimePGPPublicKeyField(PGPPublicKeyFieldMixin, models.DateTimeField):
"""DateTime PGP public key encrypted field for postgres."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'TIMESTAMPTZ'
class BooleanPGPPublicKeyField(PGPPublicKeyFieldMixin, models.BooleanField):
"""Boolean PGP public key encrypted field."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'BOOL'
class EmailPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.EmailField):
"""Email PGP symmetric key encrypted field."""
class IntegerPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.IntegerField):
"""Integer PGP symmetric key encrypted field."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'INT4'
class BigIntegerPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.IntegerField):
"""BigInteger PGP symmetric key encrypted field."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'BIGINT'
class TextPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.TextField):
"""Text PGP symmetric key encrypted field for postgres."""
class CharPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.CharField):
"""Char PGP symmetric key encrypted field for postgres."""
class DatePGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.DateField):
"""Date PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'DATE'
class DateTimePGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.DateTimeField):
"""DateTime PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'TIMESTAMPTZ'
class BooleanPGPSymmetricKeyField(PGPPublicKeyFieldMixin, models.BooleanField):
"""Boolean PGP public key encrypted field."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'BOOL'
class DecimalPGPPublicKeyField(DecimalPGPFieldMixin,
PGPPublicKeyFieldMixin, models.DecimalField):
"""Decimal PGP public key encrypted field for postgres."""
class DecimalPGPSymmetricKeyField(DecimalPGPFieldMixin,
PGPSymmetricKeyFieldMixin, models.DecimalField):
"""Decimal PGP symmetric key encrypted field for postgres."""
class FloatPGPPublicKeyField(PGPPublicKeyFieldMixin, models.FloatField):
"""Float PGP public key encrypted field for postgres."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'DOUBLE PRECISION'
class FloatPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.FloatField):
"""Float PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'DOUBLE PRECISION'
class TimePGPPublicKeyField(PGPPublicKeyFieldMixin, models.TimeField):
"""Time PGP public key encrypted field for postgres."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'TIME'
class TimePGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.TimeField):
"""Float PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL_WITH_NULLIF
cast_type = 'TIME'