/
oauth_token.py
418 lines (346 loc) · 14 KB
/
oauth_token.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Hive Appier Framework
# Copyright (c) 2008-2019 Hive Solutions Lda.
#
# This file is part of Hive Appier Framework.
#
# Hive Appier Framework is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Appier Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Appier Framework. If not, see <http://www.apache.org/licenses/>.
__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """
__version__ = "1.0.0"
""" The version of the module """
__revision__ = "$LastChangedRevision$"
""" The revision number of the module """
__date__ = "$LastChangedDate$"
""" The last change date of the module """
__copyright__ = "Copyright (c) 2008-2019 Hive Solutions Lda."
""" The copyright for the module """
__license__ = "Apache License, Version 2.0"
""" The license for the module """
import time
import appier
from appier_extras.parts.admin.models import base
class OAuthToken(base.Base):
"""
Model class that represent an OAuth 2.0 access token
that as been in created for a specific user context
and with a certain duration scope.
"""
DEFAULT_DURATION = 3600
""" The default duration of the OAuth token, this
value should not be too long to avoid security issues """
CODE_DURATION = 600
""" The authorization code duration (in seconds), to be
used for proper authorization code validation """
name = appier.field(
index = "hashed",
default = True,
safe = True,
immutable = True
)
""" Simplified name value, created from the first few
characters of the (original) access token value """
access_token = appier.field(
index = "hashed",
safe = True
)
""" The actual string representing an authorization
issued to the client, this is not an immutable value
as the refresh token may be used to generate a new one """
access_token_date = appier.field(
type = float,
safe = True,
private = True,
meta = "datetime"
)
""" The date when the access token was last generated, multiple
access token may be generated using the refresh token"""
authorization_code = appier.field(
index = "hashed",
safe = True,
private = True
)
""" The authorization code generated by the
authorization server """
authorization_code_date = appier.field(
type = float,
safe = True,
private = True,
meta = "datetime"
)
""" The date when the authorization code was generated """
username = appier.field(
index = "hashed",
safe = True,
immutable = True
)
""" The name of the user that is authorized this
access token, will be used for custom ACL creation """
scope = appier.field(
type = list,
private = True,
immutable = True
)
""" The OAuth based scope string that "created" this
token with its values sorted alphabetically """
expires_in = appier.field(
type = int,
index = "all",
immutable = True
)
""" The duration in seconds of the access token lifetime """
redirect_uri = appier.field(
index = "hashed",
immutable = True,
meta = "url",
description = "Redirect URI"
)
""" An absolute uri to which the authorization server
will redirect the user-agent to when the end-user
authorization step is completed """
refresh_token = appier.field(
index = "hashed",
safe = True,
private = True,
immutable = True
)
""" A token used by the client to obtain a new access token
(in addition or as a replacement for an expired access
token), without having to involve the resource owner. """
tokens = appier.field(
type = list,
safe = True,
private = True,
immutable = True
)
""" The ACL tokens associated with this access token, should
be created taking into account the token scope """
client = appier.field(
type = appier.reference(
"OAuthClient",
name = "id"
),
immutable = True
)
""" The reference to the OAuth client that has been used
for the generation of this token """
@classmethod
def validate(cls):
return super(OAuthToken, cls).validate() + [
appier.not_null("username"),
appier.not_empty("username"),
appier.not_null("scope"),
appier.not_empty("scope"),
appier.string_gt("scope", 0),
appier.not_null("redirect_uri"),
appier.not_empty("redirect_uri"),
appier.is_url("redirect_uri"),
appier.not_null("client")
]
@classmethod
def list_names(cls):
return ["name", "created", "username"]
@classmethod
def order_name(cls):
return ["id", -1]
@classmethod
def reuse_s(cls, redirect_uri, scope, oauth_client, account = None, owner = None):
# defaults the provided owner value to the global registered
# app to be used if required for account defaulting
owner = owner or appier.get_app()
# retrieves the current account from session and then
# normalizes the provided scope list to convert it to
# tokens (filters on account permissions) then tries to
# retrieve an already existing compatible OAuth token
account = account or owner.admin_part.account_c.from_session()
tokens = cls._filter_scope_g(scope, account = account)
oauth_token = cls.get_e(
redirect_uri = redirect_uri,
username = account.username,
scope = scope,
tokens = tokens,
client = oauth_client.id,
rules = False,
raise_e = False
)
# in case there's no valid equivalent token, returns the
# control flow immediately with an invalid value
if not oauth_token: return False, tokens, None
# in case the access token contained in the object is already
# expired then refreshes the access token so that it can live
# one more time for this request (refreshed re-usage scenario)
if oauth_token.is_expired(): oauth_token.refresh_access_token_s()
# in case there's an already existing OAuth token that
# has the same requirements (scope, client, redirect URL)
# of the one being requested, then a new authorization code
# is generated and the user agent is redirected immediately
# as there's no extra need for user interaction
oauth_token.set_code_s()
# returns a valid result indicating both the retrieved tokens
# and the OAuth token that can be used for re-usage
return True, tokens, oauth_token
@classmethod
def login(cls, access_token, rules = False):
oauth_token = cls.get_e(
access_token = access_token,
rules = rules
)
oauth_token.touch_expired()
return oauth_token
@classmethod
def _filter_scope_g(cls, scope, account = None, owner = None):
"""
Filters the provided sequence of tokens for the scope, so
that only the ones allowed for the requested account are used.
This avoid security issues like someone requesting values
for a token that is for which the user is not allowed.
:type scope: List
:param scope: The list of tokens to be filtered.
:type account: Account
:param account: The account that is going to be used for the
filtering of the values, in case none is provided the current
account in session is used.
:rtype: List
:return: The resulting filtering list containing only the
tokens for which the provided account is capable.
"""
# defaults the provided owner value to the global registered
# app to be used if required for account defaulting
owner = owner or appier.get_app()
# builds the list that is going to be used to store the
# result of the scope filtering (ACL verification)
result = []
# retrieves the complete set of tokens from the account
# and then converts them into the map version of them
account = account or owner.admin_part.account_c.from_session()
tokens = account.tokens()
tokens_m = appier.to_tokens_m(tokens)
# iterates over each token of the scope to validate it
# according to the ACL of the associated account
for token in scope:
valid = appier.check_token(None, token, tokens_m = tokens_m)
if not valid: continue
result.append(token)
# returns the final result that contains only the scope
# tokens for which the account is entitle to register
return result
@classmethod
def _underscore(cls, plural = True):
return "oauth_tokens" if plural else "oauth_token"
@classmethod
def _readable(cls, plural = False):
return "OAuth Tokens" if plural else "OAuth Token"
def pre_create(self):
base.Base.pre_create(self)
cls = self.__class__
duration = appier.conf("OAUTH_DURATION", cls.DEFAULT_DURATION, cast = int)
self.access_token = appier.gen_token()
self.access_token_date = time.time()
self.client_secret = appier.gen_token()
self.authorization_code = appier.gen_token()
self.authorization_code_date = time.time()
self.expires_in = duration
self.refresh_token = appier.gen_token()
self.tokens = self._filter_scope(self.scope)
self.name = self.access_token[:8]
self._verify()
def set_code_s(self):
self.authorization_code = appier.gen_token()
self.authorization_code_date = time.time()
self.save()
def unset_code_s(self):
self.authorization_code = None
self.authorization_code_date = None
self.save()
def set_access_token_s(self):
self.access_token = appier.gen_token()
self.access_token_date = time.time()
self.save()
def unset_access_token_s(self):
self.access_token = None
self.access_token_date = None
self.save()
def refresh_access_token_s(self):
self.set_access_token_s()
def get_account(self):
return self.owner.admin_part.account_c.get(
username = self.username
)
def is_expired(self):
access_token_date = self.access_token_date or self.created
return time.time() > access_token_date + self.expires_in
def touch_expired(self, delete = None):
"""
Method to be called upon the token usage so that the
expiration for the OAuth token may be checked.
If the verification fails it's possible to have the
current token removed from the data source if there's
no refresh token defined.
:type delete: bool
:param delete: If the token should be automatically
removed from the data source if it's expired (any of
the verification fails).
"""
try:
self.verify_expired()
except:
if delete == None: delete = False if self.refresh_token else True
if delete: self.delete()
raise
def verify_code(self, code, grant_type = "authorization_code"):
cls = self.__class__
appier.verify(not self.authorization_code == None)
appier.verify(not self.authorization_code_date == None)
appier.verify(self.authorization_code == code)
appier.verify(time.time() - self.authorization_code_date < cls.CODE_DURATION)
appier.verify(grant_type, "authorization_code")
def verify_refresh(self, refresh_token, grant_type = "refresh_token"):
appier.verify(not self.refresh_token == None)
appier.verify(not self.refresh_token == None)
appier.verify(self.refresh_token == refresh_token)
appier.verify(grant_type, "refresh_token")
def verify_expired(self):
appier.verify(
not self.is_expired(),
message = "OAuth access token is expired",
code = 403,
exception = appier.OperationalError
)
def _verify(self):
self._verify_scope()
def _verify_scope(self):
scope_s = set(self.scope)
appier.verify(len(self.scope) == len(scope_s))
def _filter_scope(self, scope):
"""
Filters the provided sequence of tokens for the scope, so
that only the ones allowed for the requested user are used.
This avoid security issues like someone requesting values
for a token that is for which the user is not allowed.
:type scope: List
:param scope: The list of tokens to be filtered.
:rtype: List
:return: The resulting filtering list containing only the
tokens for which the impersonated user is capable.
"""
cls = self.__class__
account = self.get_account()
return cls._filter_scope_g(scope, account = account)
def _set_session(self, unset = True, safes = [], method = "set"):
cls = self.__class__
account = self.get_account()
account._set_session(unset = unset, safes = safes, method = method)
if unset: return
set("tokens", self.tokens)