-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.py
663 lines (601 loc) · 29.3 KB
/
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# Copyright (c) Sebastian Scholz
# See LICENSE for details.
""" The token endpoint. """
import logging
import string
import time
import json
import warnings
from abc import ABCMeta, abstractmethod
from twisted.web.resource import Resource
from twisted.web.http import OK
from txoauth2.clients import PublicClient
from txoauth2.granttypes import GrantTypes
from .errors import InsecureConnectionError, MissingParameterError, InvalidParameterError, \
InvalidTokenError, InvalidScopeError, UnsupportedGrantTypeError, MultipleParameterError, \
MultipleClientCredentialsError, OAuth2Error, InvalidClientIdError, DifferentRedirectUriError, \
UnauthorizedClientError, MalformedParameterError, MultipleClientAuthenticationError, \
NoClientAuthenticationError, MalformedRequestError, ServerError
class TokenFactory(object):
""" A factory that can generate tokens. """
__metaclass__ = ABCMeta
@abstractmethod
def generateToken(self, lifetime, client, scope, additionalData=None):
"""
Generate a new token. The generated token must comply to the specification
(see https://www.oauth.com/oauth2-servers/access-tokens/access-token-response/#token).
See TokenResource::VALID_TOKEN_CHARS for a list of allowed characters in the token.
:raises ValueError: If the scope is not valid.
:param lifetime: The lifetime of the new token in seconds or None for infinite lifetime.
:param client: The client that gets the new token.
:param scope: A list of scopes that the new token will grant access to.
:param additionalData: Any additional data that was passes to OAuth2::grantAccess.
:return: A new token as a string.
"""
raise NotImplementedError()
class TokenStorage(object):
""" An object that stores and manages tokens. """
__metaclass__ = ABCMeta
@abstractmethod
def contains(self, token):
"""
:param token: The token to validate.
:return: True if the token is stored in this token storage, False otherwise.
"""
raise NotImplementedError()
@abstractmethod
def hasAccess(self, token, scope):
"""
Return True if the token is stored in this token storage
and grants access to the given list of scopes (e.g. was
store called with the token and at least the give scopes).
:raises KeyError: If the token is not in the token store.
:param token: The token to validate.
:param scope: The scope or scope list the token must grant access to.
:return: True, if the token grants access to the scopes, False otherwise.
"""
raise NotImplementedError()
@abstractmethod
def getTokenAdditionalData(self, token):
"""
Get the additional data that was passed to store together with the given token.
:raises KeyError: If the token was not found in the token storage
:param token: A token.
:return: The additional data that was stored alongside the token.
"""
raise NotImplementedError()
@abstractmethod
def getTokenScope(self, token):
"""
Get the scope that was passed to store together with the given token.
:raises KeyError: If the token was not found in the token storage
:param token: A token.
:return: The scope as a list of strings that was stored alongside the token.
"""
raise NotImplementedError()
@abstractmethod
def getTokenClient(self, token):
"""
Get the client id of the client that was passed to store together with the given token.
:raises KeyError: If the token was not found in the token storage
:param token: A token.
:return: The id of the client that was stored alongside the token.
"""
raise NotImplementedError()
@abstractmethod
def getTokenLifetime(self, token):
"""
:raises KeyError: If the token was not found in the token storage
:param token: A token.
:return: The current lifetime of the given token in seconds.
"""
raise NotImplementedError()
@abstractmethod
def store(self, token, client, scope, additionalData=None, expireTime=None):
"""
Store the given token in the token storage alongside
the optional additional data. If expireTime is not None,
it marks the point in time in seconds since the epoch at
which the token should expire
(contains should then return False for this token).
:raises ValueError: If the token is not a string.
:param token: The token to store.
:param client: The client that this token was made for.
:param scope: The scope this token grants access to.
:param additionalData: Optional additional data that
was passed to OAuth2.grantAccess.
:param expireTime: Optionally the seconds since the epoch,
when the token should expire.
"""
raise NotImplementedError()
@abstractmethod
def remove(self, token):
"""
Remove the toke from the token storage.
:raises KeyError: If the token was not found in the token storage
:param token: The token to remove.
"""
raise NotImplementedError()
class PersistentStorage(object):
"""
A key value storage that can store data between a call to OAuth2.grantAccess
and the corresponding POST request to the TokenResource from the client.
"""
__metaclass__ = ABCMeta
@abstractmethod
def put(self, key, data, expireTime=None):
"""
Store the given data with the given key.
If expireTime is not None, it marks when the data
should expire in seconds since the epoch.
:param key: The key of the data.
:param data: Some data.
:param expireTime: Optionally the seconds since the epoch,
when the data should expire.
"""
raise NotImplementedError()
@abstractmethod
def pop(self, key):
"""
Return the data that was previously stored with the given key and remove it.
:raises KeyError: If no data was stored with the key.
:param key: The key the data was stored at.
:return: The data that was stored.
"""
raise NotImplementedError()
class UserPasswordManager(object):
"""
A password manager that can authenticate a resource owner with a username and password.
This is only used in the Resource Owner Password Credentials Grant.
See https://tools.ietf.org/html/rfc6749#section-4.3
"""
__metaclass__ = ABCMeta
@abstractmethod
def authenticate(self, username, password):
"""
Authenticate a resource owner.
:param username: The username of the resource owner as a byte string.
:param password: The plaintext password of the resource owner as a byte string.
:return: True, if the resource owner could be authenticated, False otherwise.
"""
raise NotImplementedError()
class TokenResource(Resource, object):
"""
This resource handles creation and refreshing of access tokens.
If authTokenLifeTime is None, the tokens generated by this resource will never expire.
Otherwise, the generated access token will have a lifetime in seconds as specified by
authTokenLifeTime. Additionally, a refresh token will be generated with an unlimited
lifetime. This refresh token can be used at this resource to generate additional
access tokens with limited lifetime, if the old one expired.
There are two scenarios in which this resource plays a role:
A: 1. The client's authorization request was granted by the user
and a code was generated in OAuth2.grantAccess.
2. The client now generates a POST request to this resource,
passing the code as an argument.
3. This resource creates and stores an access token and returns it.
Depending on authTokenLifeTime, a refresh token is also created,
stored and returned.
B: 1. The client has a refresh token and an access token which expired.
2. The client generates a POST request to this resource,
passing the refresh token as an argument.
3. This resource creates and stores another access token and returns it.
"""
VALID_TOKEN_CHARS = string.digits + string.ascii_letters + '-._~+/'
tokenFactory = None
persistentStorage = None
allowInsecureRequestDebug = False
refreshTokenStorage = None
# This is the token storage singleton
_OAuthTokenStorage = None
clientStorage = None
authTokenLifeTime = 3600
minRefreshTokenLifeTime = 1209600 # = 14 days
defaultScope = None
acceptedGrantTypes = [GrantTypes.REFRESH_TOKEN.value, GrantTypes.AUTHORIZATION_CODE.value,
GrantTypes.CLIENT_CREDENTIALS.value, GrantTypes.PASSWORD.value]
def __init__(self, tokenFactory, persistentStorage, refreshTokenStorage, authTokenStorage,
clientStorage, authTokenLifeTime=3600, minRefreshTokenLifeTime=1209600,
passwordManager=None, allowInsecureRequestDebug=False, grantTypes=None,
defaultScope=None):
"""
Create a new TokenResource.
The given authTokenStorage will be used to check tokens when
isAuthorized or the oauth2 is used. The authTokenLifeTime
parameter governs the lifetime of generated tokens in seconds.
If it is None, refresh tokens will be generated in addition to
the access tokens with limited lifetimes.
:raises ValueError: If the password grant flow is enabled but no password manager supplied.
:param tokenFactory: A token factory. Will generate both access and refresh tokens.
:param persistentStorage: Persistent storage to access data stored by OAuth2.grantAccess.
:param refreshTokenStorage: A token storage for refresh tokens.
:param authTokenStorage: A token storage for access tokens. Will be used as a singleton.
:param clientStorage: A handle to the storage of known clients.
:param authTokenLifeTime: Either lifetime in seconds or None for an unlimited lifetime.
:param minRefreshTokenLifeTime: Either the minimum lifetime of a refresh token in seconds
or None for an unlimited lifetime.
:param passwordManager: The password manager to use for the password grant flow.
:param allowInsecureRequestDebug: If True, allow requests over insecure connections.
Do NOT use in production!
:param grantTypes: The grant types that are enabled for this authorization endpoint.
:param defaultScope: The default scope for tokens if a request does not contain any.
"""
super(TokenResource, self).__init__()
self.allowedMethods = [b'POST']
self.allowInsecureRequestDebug = allowInsecureRequestDebug
self.refreshTokenStorage = refreshTokenStorage
self.tokenFactory = tokenFactory
self.persistentStorage = persistentStorage
self.clientStorage = clientStorage
self.passwordManager = passwordManager
self.authTokenLifeTime = authTokenLifeTime
self.minRefreshTokenLifeTime = minRefreshTokenLifeTime
self.defaultScope = defaultScope
if TokenResource._OAuthTokenStorage is not None \
and TokenResource._OAuthTokenStorage != authTokenStorage:
warnings.warn(
'Registering {newStorage!r} as the token storage singleton overwrites previously '
'registered singleton {oldStorage!r}. The @auth decorator and isAuthorized methods '
'won\'t be able to see tokens stored in the old token storage.'.format(
newStorage=authTokenStorage, oldStorage=TokenResource._OAuthTokenStorage),
RuntimeWarning
)
TokenResource._OAuthTokenStorage = authTokenStorage
if grantTypes is not None:
if GrantTypes.IMPLICIT in grantTypes:
grantTypes.remove(GrantTypes.IMPLICIT)
grantTypes = [grantType.value if isinstance(grantType, GrantTypes) else grantType
for grantType in grantTypes]
self.acceptedGrantTypes = grantTypes
if GrantTypes.PASSWORD.value in self.acceptedGrantTypes and passwordManager is None:
raise ValueError('The passwordManager must not be None '
'if the password grant flow is enabled')
self.render_HEAD = None # Disable automatic HEAD handling. pylint: disable=invalid-name
def render_POST(self, request): # pylint: disable=invalid-name
"""
Handle a POST request according to the OAuth2 specification.
See the docstring of this class for more information.
:param request: The POST request.
:return: A response or NOT_DONE_YET
"""
if not self.allowInsecureRequestDebug and not request.isSecure():
return InsecureConnectionError().generate(request)
contentTypeHeader = request.getHeader(b'Content-Type')
if contentTypeHeader is None or \
not contentTypeHeader.startswith(b'application/x-www-form-urlencoded'):
message = 'The Content-Type must be "application/x-www-form-urlencoded"'
return MalformedRequestError(message).generate(request)
if b'grant_type' not in request.args:
return MissingParameterError(name='grant_type').generate(request)
if len(request.args[b'grant_type']) != 1:
return MultipleParameterError('grant_type').generate(request)
try:
grantType = request.args[b'grant_type'][0].decode('utf-8')
except UnicodeDecodeError:
return InvalidParameterError('grant_type').generate(request)
if grantType not in self.acceptedGrantTypes:
return UnsupportedGrantTypeError(grantType).generate(request)
try:
if grantType == GrantTypes.REFRESH_TOKEN.value:
return self._handleRefreshRequest(
request, self._authenticateClient(request, grantType))
elif grantType == GrantTypes.AUTHORIZATION_CODE.value:
return self._handleAuthorizationCodeRequest(
request, self._authenticateClient(request, grantType))
elif grantType == GrantTypes.CLIENT_CREDENTIALS.value:
return self._handleClientCredentialsRequest(
request, self._authenticateClient(request, grantType))
elif grantType == GrantTypes.PASSWORD.value:
return self._handlePasswordRequest(
request, self._authenticateClient(request, grantType))
# pylint: disable=assignment-from-no-return
result = self.onCustomGrantTypeRequest(request, grantType)
if isinstance(result, OAuth2Error):
warnings.warn('Returning an error from onCustomGrantTypeRequest is '
'deprecated, it should be raised instead.', DeprecationWarning)
raise result
return result
except OAuth2Error as error:
return error.generate(request)
except Exception as error: # pylint: disable=broad-except
logging.getLogger('txOauth2').error(
'Caught exception while handling a token request: %s', str(error), exc_info=True)
return ServerError(message=str(error)).generate(request)
def _handleRefreshRequest(self, request, client):
"""
Handle a request for the refresh token flow.
:param request: The request.
:param client: The client that makes the request.
:return: The result of the request.
"""
if b'refresh_token' not in request.args:
raise MissingParameterError('refresh_token')
if len(request.args[b'refresh_token']) != 1:
raise MultipleParameterError('refresh_token')
try:
refreshToken = request.args[b'refresh_token'][0].decode('utf-8')
tokenScope = self.refreshTokenStorage.getTokenScope(refreshToken)
additionalData = self.refreshTokenStorage.getTokenAdditionalData(refreshToken)
clientId = self.refreshTokenStorage.getTokenClient(refreshToken)
except (KeyError, UnicodeDecodeError):
raise InvalidTokenError('refresh token')
if clientId != client.id:
raise InvalidTokenError('refresh token')
if b'scope' in request.args:
if len(request.args[b'scope']) != 1:
raise MultipleParameterError('scope')
try:
scope = request.args[b'scope'][0].decode('utf-8').split()
except UnicodeDecodeError:
raise InvalidScopeError(request.args[b'scope'][0])
for requestedScope in scope:
if requestedScope not in tokenScope:
raise InvalidScopeError(scope)
else:
scope = tokenScope
try:
accessToken = self._storeNewAccessToken(client, scope, additionalData)
except ValueError:
raise InvalidScopeError(scope)
newRefreshToken = None
if self._shouldExpireRefreshToken(refreshToken):
self.refreshTokenStorage.remove(refreshToken)
newRefreshToken = self._storeNewRefreshToken(client, scope, additionalData)
return self._buildResponse(request, accessToken, scope, newRefreshToken)
def _handleAuthorizationCodeRequest(self, request, client):
"""
Handle a request for the authorization code flow.
:param request: The request.
:param client: The client that makes the request.
:return: The result of the request.
"""
redirectUri = None
if b'code' not in request.args:
raise MissingParameterError('code')
if len(request.args[b'code']) != 1:
raise MultipleParameterError('code')
if b'redirect_uri' in request.args:
if len(request.args[b'redirect_uri']) != 1:
raise MultipleParameterError('redirect_uri')
try:
redirectUri = request.args[b'redirect_uri'][0].decode('utf-8')
except UnicodeDecodeError:
raise InvalidParameterError('redirect_uri')
try:
data = self.persistentStorage.pop('code' + request.args[b'code'][0].decode('utf-8'))
except (KeyError, UnicodeDecodeError):
raise InvalidTokenError('authorization code')
if data['client_id'] != client.id:
raise InvalidTokenError('authorization code')
if data['redirect_uri'] is not None:
if redirectUri is None:
raise MissingParameterError('redirect_uri')
if data['redirect_uri'] != redirectUri:
raise DifferentRedirectUriError()
additionalData = data['additional_data']
scope = data['scope']
accessToken = self._storeNewAccessToken(client, scope, additionalData)
refreshToken = None
if self.authTokenLifeTime is not None:
refreshToken = self._storeNewRefreshToken(client, scope, additionalData)
return self._buildResponse(request, accessToken, scope, refreshToken)
def _handleClientCredentialsRequest(self, request, client):
"""
Handle a request for the client credentials flow.
:param request: The request.
:param client: The client that makes the request.
:return: The result of the request.
"""
if isinstance(client, PublicClient):
raise UnauthorizedClientError(GrantTypes.CLIENT_CREDENTIALS.value)
scope = self._getScope(request)
try:
accessToken = self._storeNewAccessToken(client, scope, None)
except ValueError:
raise InvalidScopeError(scope)
return self._buildResponse(request, accessToken, scope)
def _handlePasswordRequest(self, request, client):
"""
Handle a request for the password flow.
:param request: The request.
:param client: The client that makes the request.
:return: The result of the request.
"""
for name in [b'username', b'password']:
if name not in request.args:
raise MissingParameterError(name.decode('utf-8'))
if len(request.args[name]) != 1:
raise MultipleParameterError(name.decode('utf-8'))
username = request.args[b'username'][0]
password = request.args[b'password'][0]
scope = self._getScope(request)
if not self.passwordManager.authenticate(username, password):
raise InvalidTokenError('username or password')
try:
accessToken = self._storeNewAccessToken(client, scope, None)
except ValueError:
raise InvalidScopeError(scope)
refreshToken = None
if self.authTokenLifeTime is not None:
refreshToken = self._storeNewRefreshToken(client, scope, None)
return self._buildResponse(request, accessToken, scope, refreshToken)
# pylint: disable=no-self-use, unused-argument
def onCustomGrantTypeRequest(self, request, grantType):
"""
Gets called when a request with a custom grant type is encountered.
It is up to this method to extract the client from the request and authenticate him.
This method should get overwritten to handle the request.
:raises OAuth2Error: If an error occurred.
:param request: The request.
:param grantType: The custom grant type.
:return: The result of the POST request.
"""
raise UnsupportedGrantTypeError(grantType)
def _getScope(self, request):
"""
Get the scope from the request.
:param request: The request that might contain the scope.
:return: The list of scopes.
"""
if b'scope' in request.args:
if len(request.args[b'scope']) != 1:
raise MultipleParameterError('scope')
try:
return request.args[b'scope'][0].decode('utf-8').split()
except UnicodeDecodeError:
raise InvalidScopeError(request.args[b'scope'][0])
else:
if self.defaultScope is None:
raise MissingParameterError('scope')
return self.defaultScope
def _shouldExpireRefreshToken(self, refreshToken):
"""
:param refreshToken: A valid refresh token.
:return: Whether or not to expire the refresh token.
"""
tokenLifetime = self.refreshTokenStorage.getTokenLifetime(refreshToken)
return tokenLifetime >= self.minRefreshTokenLifeTime
def _storeNewRefreshToken(self, client, scope, additionalData):
"""
Create and store a new refresh token.
:raises ValueError: If the scope is invalid.
:param client: The client the refresh token belongs to.
:param scope: The scope of the refresh token.
:param additionalData: Additional data of the refresh token.
:return: The new refresh token.
"""
refreshToken = self.tokenFactory.generateToken(
None, client, scope=scope, additionalData=additionalData)
if not self.isValidToken(refreshToken):
raise RuntimeError('Generated token is invalid: {token}'.format(token=refreshToken))
self.refreshTokenStorage.store(refreshToken, client, scope=scope,
additionalData=additionalData)
return refreshToken
def _storeNewAccessToken(self, client, scope, additionalData):
"""
Create and store a new access token.
:raises ValueError: If the scope is invalid.
:param client: The client the access token belongs to.
:param scope: The scope of the access token.
:param additionalData: Additional data of the access token.
:return: The new access token.
"""
accessToken = self.tokenFactory.generateToken(
self.authTokenLifeTime, client, scope=scope, additionalData=additionalData)
if not self.isValidToken(accessToken):
raise RuntimeError('Generated token is invalid: {token}'.format(token=accessToken))
expireTime = None
if self.authTokenLifeTime is not None:
expireTime = time.time() + self.authTokenLifeTime
self.getTokenStorageSingleton().store(
accessToken, client, scope=scope,
additionalData=additionalData, expireTime=expireTime)
return accessToken
def _buildResponse(self, request, accessToken, scope, refreshToken=None):
"""
Helper method for render_POST to generate a response
with an access token and an optional refresh token.
:param request: The POST request.
:param accessToken: The access token to send back.
:param scope: The scope of the access token to send back.
:param refreshToken: An optional refresh token to send back.
:return: A response as as a json string.
"""
result = {
'access_token': accessToken,
'token_type': 'Bearer',
'scope': ' '.join(scope)
}
if self.authTokenLifeTime is not None:
result['expires_in'] = int(self.authTokenLifeTime)
if refreshToken is not None:
result['refresh_token'] = refreshToken
request.setHeader('Content-Type', 'application/json;charset=UTF-8')
request.setHeader('Cache-Control', 'no-store')
request.setHeader('Pragma', 'no-cache')
request.setResponseCode(OK)
return json.dumps(result).encode('utf-8')
def _authenticateClient(self, request, grantType):
"""
Identify and authenticate a client by the credentials in the request.
:raises OAuth2Error: If the client could not be authenticated.
:param request: The request.
:param grantType: The grant type extracted from the request.
:return: The authenticated client.
"""
clientId, secret = self._getClientCredentials(request)
if clientId is None:
raise NoClientAuthenticationError()
try:
clientId = clientId.decode('utf-8')
except UnicodeDecodeError:
raise MalformedParameterError('client_id')
if secret is not None:
try:
secret = secret.decode('utf-8')
except UnicodeDecodeError:
raise MalformedParameterError('client_secret')
try:
client = self.clientStorage.getClient(clientId)
except KeyError:
raise InvalidClientIdError()
if not isinstance(client, PublicClient):
result = self.clientStorage.authenticateClient(client, request, secret)
if isinstance(result, OAuth2Error):
warnings.warn('Returning an error from authenticateClient is deprecated, '
'it should be raised instead.', DeprecationWarning)
raise result
client = result
if grantType not in client.authorizedGrantTypes:
raise UnauthorizedClientError(grantType)
return client
@staticmethod
def _getClientCredentials(request):
"""
Parse the client id and secret from the request, if the request contains them.
:raises OAuth2Error: If an error occurred.
:param request: The request that may contain client credentials.
:return: An optional user id and an optional client secret.
"""
clientId = None
secret = None
authorizationHeader = request.getHeader(b'Authorization')
if authorizationHeader is not None and \
authorizationHeader.strip().lower().startswith(b'basic'):
clientId = request.getUser()
clientId = None if clientId == '' else clientId
if clientId is not None:
secret = request.getPassword()
secret = None if secret == '' else secret
if b'client_id' in request.args:
if len(request.args[b'client_id']) != 1:
raise MultipleClientCredentialsError()
if clientId is not None and clientId != request.args[b'client_id'][0]:
raise MultipleClientCredentialsError()
clientId = request.args[b'client_id'][0]
if b'client_secret' in request.args:
if secret is not None:
raise MultipleClientAuthenticationError()
if len(request.args[b'client_secret']) != 1:
raise MultipleParameterError('client_secret')
secret = request.args[b'client_secret'][0]
return clientId, secret
@classmethod
def isValidToken(cls, token):
"""
Check if a token conforms tho the OAuth2 specification.
See https://www.oauth.com/oauth2-servers/access-tokens/access-token-response/#token
:param token: The token to check.
:return: True, if the token conforms to the specification, False otherwise
"""
return all(char in cls.VALID_TOKEN_CHARS for char in token)
@staticmethod
def getTokenStorageSingleton():
"""
Access the static access token storage singleton.
The singleton must have been previously initialized
by instantiating a TokenResource.
:return: The access token storage.
"""
if TokenResource._OAuthTokenStorage is None:
raise RuntimeError('The access token storage is not initialized')
return TokenResource._OAuthTokenStorage