forked from python-social-auth/social-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
344 lines (277 loc) · 10.3 KB
/
storage.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""Models mixins for Social Auth"""
import re
import time
import base64
import uuid
import warnings
from datetime import datetime, timedelta
import six
from openid.association import Association as OpenIdAssociation
from .exceptions import MissingBackend
from .backends.utils import get_backend
NO_ASCII_REGEX = re.compile(r'[^\x00-\x7F]+')
NO_SPECIAL_REGEX = re.compile(r'[^\w.@+_-]+', re.UNICODE)
class UserMixin(object):
# Consider tokens that expire in 5 seconds as already expired
ACCESS_TOKEN_EXPIRED_THRESHOLD = 5
user = ''
provider = ''
uid = None
extra_data = None
def get_backend(self, strategy):
return get_backend(strategy.get_backends(), self.provider)
def get_backend_instance(self, strategy):
try:
backend_class = self.get_backend(strategy)
except MissingBackend:
return None
else:
return backend_class(strategy=strategy)
@property
def access_token(self):
"""Return access_token stored in extra_data or None"""
return self.extra_data.get('access_token')
@property
def tokens(self):
warnings.warn('tokens is deprecated, use access_token instead')
return self.access_token
def refresh_token(self, strategy, *args, **kwargs):
token = self.extra_data.get('refresh_token') or \
self.extra_data.get('access_token')
backend = self.get_backend(strategy)
if token and backend and hasattr(backend, 'refresh_token'):
backend = backend(strategy=strategy)
response = backend.refresh_token(token, *args, **kwargs)
extra_data = backend.extra_data(self,
self.uid,
response,
self.extra_data)
if self.set_extra_data(extra_data):
self.save()
def expiration_timedelta(self):
"""Return provider session live seconds. Returns a timedelta ready to
use with session.set_expiry().
If provider returns a timestamp instead of session seconds to live, the
timedelta is inferred from current time (using UTC timezone). None is
returned if there's no value stored or it's invalid.
"""
if self.extra_data and 'expires' in self.extra_data:
try:
expires = int(self.extra_data.get('expires'))
except (ValueError, TypeError):
return None
now = datetime.utcnow()
# Detect if expires is a timestamp
if expires > time.mktime(now.timetuple()):
# expires is a datetime, return the remaining difference
return datetime.utcfromtimestamp(expires) - now
else:
# expires is the time to live seconds since creation,
# check against auth_time if present, otherwise return
# the value
auth_time = self.extra_data.get('auth_time')
if auth_time:
reference = datetime.utcfromtimestamp(auth_time)
return (reference + timedelta(seconds=expires)) - now
else:
return timedelta(seconds=expires)
def expiration_datetime(self):
# backward compatible alias
return self.expiration_timedelta()
def access_token_expired(self):
"""Return true / false if access token is already expired"""
expiration = self.expiration_timedelta()
return expiration and \
expiration.total_seconds() <= self.ACCESS_TOKEN_EXPIRED_THRESHOLD
def get_access_token(self, strategy):
"""Returns a valid access token."""
if self.access_token_expired():
self.refresh_token(strategy)
return self.access_token
def set_extra_data(self, extra_data=None):
if extra_data and self.extra_data != extra_data:
if self.extra_data and not isinstance(
self.extra_data, six.string_types):
self.extra_data.update(extra_data)
else:
self.extra_data = extra_data
return True
@classmethod
def clean_username(cls, value):
"""Clean username removing any unsupported character"""
value = NO_ASCII_REGEX.sub('', value)
value = NO_SPECIAL_REGEX.sub('', value)
return value
@classmethod
def changed(cls, user):
"""The given user instance is ready to be saved"""
raise NotImplementedError('Implement in subclass')
@classmethod
def get_username(cls, user):
"""Return the username for given user"""
raise NotImplementedError('Implement in subclass')
@classmethod
def user_model(cls):
"""Return the user model"""
raise NotImplementedError('Implement in subclass')
@classmethod
def username_max_length(cls):
"""Return the max length for username"""
raise NotImplementedError('Implement in subclass')
@classmethod
def allowed_to_disconnect(cls, user, backend_name, association_id=None):
"""Return if it's safe to disconnect the social account for the
given user"""
raise NotImplementedError('Implement in subclass')
@classmethod
def disconnect(cls, entry):
"""Disconnect the social account for the given user"""
raise NotImplementedError('Implement in subclass')
@classmethod
def user_exists(cls, *args, **kwargs):
"""
Return True/False if a User instance exists with the given arguments.
Arguments are directly passed to filter() manager method.
"""
raise NotImplementedError('Implement in subclass')
@classmethod
def create_user(cls, *args, **kwargs):
"""Create a user instance"""
raise NotImplementedError('Implement in subclass')
@classmethod
def get_user(cls, pk):
"""Return user instance for given id"""
raise NotImplementedError('Implement in subclass')
@classmethod
def get_users_by_email(cls, email):
"""Return users instances for given email address"""
raise NotImplementedError('Implement in subclass')
@classmethod
def get_social_auth(cls, provider, uid):
"""Return UserSocialAuth for given provider and uid"""
raise NotImplementedError('Implement in subclass')
@classmethod
def get_social_auth_for_user(cls, user, provider=None, id=None):
"""Return all the UserSocialAuth instances for given user"""
raise NotImplementedError('Implement in subclass')
@classmethod
def create_social_auth(cls, user, uid, provider):
"""Create a UserSocialAuth instance for given user"""
raise NotImplementedError('Implement in subclass')
class NonceMixin(object):
"""One use numbers"""
server_url = ''
timestamp = 0
salt = ''
@classmethod
def use(cls, server_url, timestamp, salt):
"""Create a Nonce instance"""
raise NotImplementedError('Implement in subclass')
class AssociationMixin(object):
"""OpenId account association"""
server_url = ''
handle = ''
secret = ''
issued = 0
lifetime = 0
assoc_type = ''
@classmethod
def oids(cls, server_url, handle=None):
kwargs = {'server_url': server_url}
if handle is not None:
kwargs['handle'] = handle
return sorted([
(assoc.id, cls.openid_association(assoc))
for assoc in cls.get(**kwargs)
], key=lambda x: x[1].issued, reverse=True)
@classmethod
def openid_association(cls, assoc):
secret = assoc.secret
if not isinstance(secret, six.binary_type):
secret = secret.encode()
return OpenIdAssociation(assoc.handle, base64.decodestring(secret),
assoc.issued, assoc.lifetime,
assoc.assoc_type)
@classmethod
def store(cls, server_url, association):
"""Create an Association instance"""
raise NotImplementedError('Implement in subclass')
@classmethod
def get(cls, *args, **kwargs):
"""Get an Association instance"""
raise NotImplementedError('Implement in subclass')
@classmethod
def remove(cls, ids_to_delete):
"""Remove an Association instance"""
raise NotImplementedError('Implement in subclass')
class CodeMixin(object):
email = ''
code = ''
verified = False
def verify(self):
self.verified = True
self.save()
@classmethod
def generate_code(cls):
return uuid.uuid4().hex
@classmethod
def make_code(cls, email):
code = cls()
code.email = email
code.code = cls.generate_code()
code.verified = False
code.save()
return code
@classmethod
def get_code(cls, code):
raise NotImplementedError('Implement in subclass')
class PartialMixin(object):
token = ''
data = ''
next_step = ''
backend = ''
@property
def args(self):
return self.data.get('args', [])
@args.setter
def args(self, value):
self.data['args'] = value
@property
def kwargs(self):
return self.data.get('kwargs', {})
@kwargs.setter
def kwargs(self, value):
self.data['kwargs'] = value
def extend_kwargs(self, values):
self.data['kwargs'].update(values)
@classmethod
def generate_token(cls):
return uuid.uuid4().hex
@classmethod
def load(cls, token):
raise NotImplementedError('Implement in subclass')
@classmethod
def destroy(cls, token):
raise NotImplementedError('Implement in subclass')
@classmethod
def prepare(cls, backend, next_step, data):
partial = cls()
partial.backend = backend
partial.next_step = next_step
partial.data = data
partial.token = cls.generate_token()
return partial
@classmethod
def store(cls, partial):
partial.save()
return partial
class BaseStorage(object):
user = UserMixin
nonce = NonceMixin
association = AssociationMixin
code = CodeMixin
partial = PartialMixin
@classmethod
def is_integrity_error(cls, exception):
"""Check if given exception flags an integrity error in the DB"""
raise NotImplementedError('Implement in subclass')