-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_obscure.py
220 lines (158 loc) · 5.76 KB
/
flask_obscure.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Obscure sequential IDs in URL variables and templates.
Impliments routing converters and fiters in Flask to obscure sequential
integer IDs. This is base on the 'Obscure' python module.
Once installed, the following converters and filters are available:
num, hex, b32, b64, and tame
"""
from werkzeug.routing import BaseConverter, IntegerConverter
from obscure import Obscure as _mod_Obscure, _base32_custom as _tame_alphabet
__version__ = "0.1.3"
class Obscure(_mod_Obscure):
"""Obscure interger IDs in URLs.
A ``salt`` value is needed. You can provide it when initializing
the app or from the flask configuration under the parameter
``OBSCURE_SALT``.
"""
def __init__(self, app=None, salt=None):
"""Add converters and filters to a :class:`Flask` instance.
Args:
app: a :class:`flask:Flask` instance or None
salt (integer): random 32-bit integer for uniqueness
"""
self.salt = salt
if app is not None:
self.init_app(app, self.salt)
def init_app(self, app, salt=None):
"""Add converters and filters to a :class:`Flask` instance.
Args:
app: a :class:`flask:Flask` instance
salt (integer): random 32-bit integer for uniqueness
Raises:
KeyError: ``OBSCURE_SALT`` must be in the
:class:`flask.Config` if it is not given as a parameter.
"""
salt = salt or self.salt or int(app.config["OBSCURE_SALT"])
_mod_Obscure.__init__(self, salt)
for converter_name, base in converters.items():
class_name = "Obscure" + base.__name__
class_ = type(class_name, (base,), {"obscure": self})
app.url_map.converters[converter_name] = class_
# Lambda can't use locals so we bind
# the local variables to input variables.
filter_ = lambda x, c=class_, s=salt: c(s).to_url(x)
app.add_template_filter(filter_, converter_name)
class Num(IntegerConverter):
"""Obscure interger ID with salted value and format as
an alternative, non-sequential number.
Rule('/customer/<num:customer_id>')
"""
def __init__(self, map):
IntegerConverter.__init__(self, map, max=0xFFFFFFFF)
def to_python(self, value):
"""Restores original number.
Args:
value (number string): obscured, non-sequential number
Returns:
integer: the original number
See Also:
to_url
"""
value = IntegerConverter.to_python(self, int(value))
return self.obscure.transform(value)
def to_url(self, value):
"""Convert value to alternate, non-sequential integer format.
Args:
value (integer): number to obscure
Returns:
string: an obscured, non-sequential number
See Also:
to_python
"""
"""
:param value: integer to convert
:returns: integer as a string
"""
return str(self.obscure.transform(value))
class Hex(BaseConverter):
"""Obscure numerical ID and format as hex.
Rule('/customer/<hex:customer_id>')
"""
weight = 50
regex = "[abcdef0123456789]{8}"
def to_python(self, value):
"""Restores original number.
Args:
value: 8 digit hexadecimal string
Returns:
integer: original integer
See Also:
to_url
"""
"""
:param value: 8 digit hexadecimal format string
:returns: integer
"""
return self.obscure.decode_hex(value)
def to_url(self, value):
"""Convert value to hexadecimal format.
:param value: integer to convert
:returns: string in hexadecimal format
"""
return self.obscure.encode_hex(value)
class Base32(BaseConverter):
"""Obscure numerical ID and format as base32.
Rule('/customer/<b32:customer_id>')
"""
weight = 50
regex = "[A-Z2-7]{7}"
def to_python(self, value):
"""Restores original number.
:param value: 7 digit base32 format string
:returns: integer
"""
return self.obscure.decode_base32(str(value))
def to_url(self, value):
"""Convert value to Base32 format.
:param value: integer to convert
:returns: string in Base32 format
"""
return self.obscure.encode_base32(value)
class Base64(BaseConverter):
"""Obscure numerical ID and format as url-safe base64.
Rule('/customer/<b64:customer_id>')
"""
weight = 50
regex = "[-_A-Za-z0-9]{6}"
def to_python(self, value):
"""Restores original number.
:param value: alternate Base64 format string
:returns: integer
"""
return self.obscure.decode_base64(str(value))
def to_url(self, value):
"""Convert value to Base64 format.
:param value: integer to convert
:returns: string in Base64 format
"""
return self.obscure.encode_base64(value)
class Tame(BaseConverter):
"""Obscure numerical ID and format as a custom base32
with the vowels 'I', 'O', and 'U' removed to eliminate common
offensive words.
Rule('/customer/<tame:customer_id>')
"""
weight = 50
regex = "[%s]{7}" % _tame_alphabet
def to_python(self, value):
"""Restores original number.
:param value: alternate Base32 format string
:returns: integer
"""
return self.obscure.decode_tame(str(value))
def to_url(self, value):
"""Convert value to alternate Base32 format.
:param value: integer to convert
:returns: alternate Base32 format
"""
return self.obscure.encode_tame(value)
converters = {"num": Num, "hex": Hex, "tame": Tame, "b32": Base32, "b64": Base64}