root / snf-astakos-app / astakos / oa2 / backends / base.py @ 2f8a7c0e
History | View | Annotate | Download (21.9 kB)
1 |
import urllib |
---|---|
2 |
import urlparse |
3 |
import uuid |
4 |
import datetime |
5 |
import json |
6 |
|
7 |
from base64 import b64encode, b64decode |
8 |
from hashlib import sha512 |
9 |
|
10 |
import logging |
11 |
logger = logging.getLogger(__name__) |
12 |
|
13 |
|
14 |
def handles_oa2_requests(func): |
15 |
def wrapper(self, *args, **kwargs): |
16 |
if not self._errors_to_http: |
17 |
return func(self, *args, **kwargs) |
18 |
try:
|
19 |
return func(self, *args, **kwargs) |
20 |
except OA2Error, e:
|
21 |
return self.build_response_from_error(e) |
22 |
return wrapper
|
23 |
|
24 |
|
25 |
class OA2Error(Exception): |
26 |
error = None
|
27 |
|
28 |
|
29 |
class InvalidClientID(OA2Error): |
30 |
pass
|
31 |
|
32 |
|
33 |
class NotAuthenticatedError(OA2Error): |
34 |
pass
|
35 |
|
36 |
|
37 |
class InvalidClientRedirectUrl(OA2Error): |
38 |
pass
|
39 |
|
40 |
|
41 |
class InvalidAuthorizationRequest(OA2Error): |
42 |
pass
|
43 |
|
44 |
|
45 |
class Response(object): |
46 |
|
47 |
def __init__(self, status, body='', headers=None, |
48 |
content_type='plain/text'):
|
49 |
if not body: |
50 |
body = ''
|
51 |
if not headers: |
52 |
headers = {} |
53 |
|
54 |
self.status = status
|
55 |
self.body = body
|
56 |
self.headers = headers
|
57 |
self.content_type = content_type
|
58 |
|
59 |
def __repr__(self): |
60 |
return "%d RESPONSE (BODY: %r, HEADERS: %r)" % (self.status, |
61 |
self.body,
|
62 |
self.headers)
|
63 |
|
64 |
|
65 |
class Request(object): |
66 |
|
67 |
def __init__(self, method, path, GET=None, POST=None, META=None, |
68 |
secure=False, user=None): |
69 |
self.method = method
|
70 |
self.path = path
|
71 |
|
72 |
if not GET: |
73 |
GET = {} |
74 |
if not POST: |
75 |
POST = {} |
76 |
if not META: |
77 |
META = {} |
78 |
|
79 |
self.secure = secure
|
80 |
self.GET = GET
|
81 |
self.POST = POST
|
82 |
self.META = META
|
83 |
self.user = user
|
84 |
|
85 |
def __repr__(self): |
86 |
prepend = ""
|
87 |
if self.secure: |
88 |
prepend = "SECURE "
|
89 |
return "%s%s REQUEST (POST: %r, GET:%r, HEADERS:%r, " % (prepend, |
90 |
self.method,
|
91 |
self.POST,
|
92 |
self.GET,
|
93 |
self.META)
|
94 |
|
95 |
|
96 |
class ORMAbstractBase(type): |
97 |
|
98 |
def __new__(cls, name, bases, attrs): |
99 |
attrs['ENTRIES'] = {}
|
100 |
return super(ORMAbstractBase, cls).__new__(cls, name, bases, attrs) |
101 |
|
102 |
|
103 |
class ORMAbstract(object): |
104 |
|
105 |
ENTRIES = {} |
106 |
|
107 |
__metaclass__ = ORMAbstractBase |
108 |
|
109 |
def __init__(self, **kwargs): |
110 |
for key, value in kwargs.iteritems(): |
111 |
setattr(self, key, value) |
112 |
|
113 |
@classmethod
|
114 |
def create(cls, id, **params): |
115 |
params = cls.clean_params(params) |
116 |
params['id'] = id |
117 |
cls.ENTRIES[id] = cls(**params)
|
118 |
return cls.get(id) |
119 |
|
120 |
@classmethod
|
121 |
def get(cls, pk): |
122 |
return cls.ENTRIES.get(pk)
|
123 |
|
124 |
@classmethod
|
125 |
def clean_params(cls, params): |
126 |
return params
|
127 |
|
128 |
|
129 |
class Client(ORMAbstract): |
130 |
|
131 |
def get_id(self): |
132 |
return self.id |
133 |
|
134 |
def get_redirect_uris(self): |
135 |
return self.uris |
136 |
|
137 |
def get_default_redirect_uri(self): |
138 |
return self.uris[0] |
139 |
|
140 |
def redirect_uri_is_valid(self, redirect_uri): |
141 |
split = urlparse.urlsplit(redirect_uri) |
142 |
if split.scheme not in urlparse.uses_query: |
143 |
raise OA2Error("Invalid redirect url scheme") |
144 |
uris = self.get_redirect_uris()
|
145 |
return redirect_uri in uris |
146 |
|
147 |
def requires_auth(self): |
148 |
if self.client_type == 'confidential': |
149 |
return True |
150 |
return 'secret' in dir(self) |
151 |
|
152 |
def check_credentials(self, username, secret): |
153 |
return username == self.id and secret == self.secret |
154 |
|
155 |
|
156 |
class Token(ORMAbstract): |
157 |
|
158 |
def to_dict(self): |
159 |
params = { |
160 |
'access_token': self.token, |
161 |
'token_type': self.token_type, |
162 |
'expires_in': self.expires, |
163 |
} |
164 |
if self.refresh_token: |
165 |
params['refresh_token'] = self.refresh_token |
166 |
return params
|
167 |
|
168 |
|
169 |
class AuthorizationCode(ORMAbstract): |
170 |
pass
|
171 |
|
172 |
|
173 |
class User(ORMAbstract): |
174 |
pass
|
175 |
|
176 |
|
177 |
class BackendBase(type): |
178 |
|
179 |
def __new__(cls, name, bases, attrs): |
180 |
super_new = super(BackendBase, cls).__new__
|
181 |
#parents = [b for b in bases if isinstance(b, BackendBase)]
|
182 |
#meta = attrs.pop('Meta', None)
|
183 |
return super_new(cls, name, bases, attrs)
|
184 |
|
185 |
@classmethod
|
186 |
def get_orm_options(cls, attrs): |
187 |
meta = attrs.pop('ORM', None) |
188 |
orm = {} |
189 |
if meta:
|
190 |
for attr in dir(meta): |
191 |
orm[attr] = getattr(meta, attr)
|
192 |
return orm
|
193 |
|
194 |
|
195 |
class SimpleBackend(object): |
196 |
|
197 |
__metaclass__ = BackendBase |
198 |
|
199 |
base_url = ''
|
200 |
endpoints_prefix = 'oa2/'
|
201 |
|
202 |
token_endpoint = 'token/'
|
203 |
token_length = 30
|
204 |
token_expires = 20
|
205 |
|
206 |
authorization_endpoint = 'auth/'
|
207 |
authorization_code_length = 60
|
208 |
authorization_response_types = ['code', 'token'] |
209 |
|
210 |
grant_types = ['authorization_code']
|
211 |
|
212 |
response_cls = Response |
213 |
request_cls = Request |
214 |
|
215 |
client_model = Client |
216 |
token_model = Token |
217 |
code_model = AuthorizationCode |
218 |
user_model = User |
219 |
|
220 |
def __init__(self, base_url='', endpoints_prefix='oa2/', id='oa2', |
221 |
token_endpoint='token/', token_length=30, |
222 |
token_expires=20, authorization_endpoint='auth/', |
223 |
authorization_code_length=60, **kwargs):
|
224 |
self.base_url = base_url
|
225 |
self.endpoints_prefix = endpoints_prefix
|
226 |
self.token_endpoint = token_endpoint
|
227 |
self.token_length = token_length
|
228 |
self.token_expires = token_expires
|
229 |
self.authorization_endpoint = authorization_endpoint
|
230 |
self.authorization_code_length = authorization_code_length
|
231 |
self.id = id |
232 |
self._errors_to_http = kwargs.get('errors_to_http', True) |
233 |
|
234 |
# Request/response builders
|
235 |
def build_request(self, method, get, post, meta): |
236 |
return self.request_cls(method=method, GET=get, POST=post, META=meta) |
237 |
|
238 |
def build_response(self, status, headers=None, body=''): |
239 |
return self.response_cls(status=status, headers=headers, body=body) |
240 |
|
241 |
# ORM Methods
|
242 |
def create_authorization_code(self, user, client, code, redirect_uri, |
243 |
scope, state, **kwargs): |
244 |
code_params = { |
245 |
'code': code,
|
246 |
'redirect_uri': redirect_uri,
|
247 |
'client': client,
|
248 |
'scope': scope,
|
249 |
'state': state,
|
250 |
'user': user
|
251 |
} |
252 |
code_params.update(kwargs) |
253 |
code_instance = self.code_model.create(**code_params)
|
254 |
logger.info('%r created' % code_instance)
|
255 |
return code_instance
|
256 |
|
257 |
def _token_params(self, value, token_type, authorization, scope): |
258 |
created_at = datetime.datetime.now() |
259 |
expires = self.token_expires
|
260 |
expires_at = created_at + datetime.timedelta(seconds=expires) |
261 |
token_params = { |
262 |
'code': value,
|
263 |
'token_type': token_type,
|
264 |
'created_at': created_at,
|
265 |
'expires_at': expires_at,
|
266 |
'user': authorization.user,
|
267 |
'redirect_uri': authorization.redirect_uri,
|
268 |
'client': authorization.client,
|
269 |
'scope': authorization.scope,
|
270 |
} |
271 |
return token_params
|
272 |
|
273 |
def create_token(self, value, token_type, authorization, scope, |
274 |
refresh=False):
|
275 |
params = self._token_params(value, token_type, authorization, scope)
|
276 |
if refresh:
|
277 |
refresh_token = self.generate_token()
|
278 |
params['refresh_token'] = refresh_token
|
279 |
# TODO: refresh token expires ???
|
280 |
token = self.token_model.create(**params)
|
281 |
logger.info('%r created' % token)
|
282 |
return token
|
283 |
|
284 |
# def delete_authorization_code(self, code):
|
285 |
# del self.code_model.ENTRIES[code]
|
286 |
|
287 |
def get_client_by_id(self, client_id): |
288 |
return self.client_model.get(client_id) |
289 |
|
290 |
def get_client_by_credentials(self, username, password): |
291 |
return None |
292 |
|
293 |
def get_authorization_code(self, code): |
294 |
return self.code_model.get(code) |
295 |
|
296 |
def get_client_authorization_code(self, client, code): |
297 |
code_instance = self.get_authorization_code(code)
|
298 |
if not code_instance: |
299 |
raise OA2Error("Invalid code") |
300 |
|
301 |
if client.get_id() != code_instance.client.get_id():
|
302 |
raise OA2Error("Mismatching client with code client") |
303 |
return code_instance
|
304 |
|
305 |
def client_id_exists(self, client_id): |
306 |
return bool(self.get_client_by_id(client_id)) |
307 |
|
308 |
def build_site_url(self, prefix='', **params): |
309 |
params = urllib.urlencode(params) |
310 |
return "%s%s%s%s" % (self.base_url, self.endpoints_prefix, prefix, |
311 |
params) |
312 |
|
313 |
def _get_uri_base(self, uri): |
314 |
split = urlparse.urlsplit(uri) |
315 |
return "%s://%s%s" % (split.scheme, split.netloc, split.path) |
316 |
|
317 |
def build_client_redirect_uri(self, client, uri, **params): |
318 |
if not client.redirect_uri_is_valid(uri): |
319 |
raise OA2Error("Invalid redirect uri") |
320 |
params = urllib.urlencode(params) |
321 |
uri = self._get_uri_base(uri)
|
322 |
return "%s?%s" % (uri, params) |
323 |
|
324 |
def generate_authorization_code(self): |
325 |
dg64 = b64encode(sha512(str(uuid.uuid4())).hexdigest())
|
326 |
return dg64[:self.authorization_code_length] |
327 |
|
328 |
def generate_token(self, *args, **kwargs): |
329 |
dg64 = b64encode(sha512(str(uuid.uuid4())).hexdigest())
|
330 |
return dg64[:self.token_length] |
331 |
|
332 |
def add_authorization_code(self, user, client, redirect_uri, scope, state, |
333 |
**kwargs): |
334 |
code = self.generate_authorization_code()
|
335 |
self.create_authorization_code(user, client, code, redirect_uri, scope,
|
336 |
state, **kwargs) |
337 |
return code
|
338 |
|
339 |
def add_token_for_client(self, token_type, authorization, refresh=False): |
340 |
token = self.generate_token()
|
341 |
self.create_token(token, token_type, authorization, refresh)
|
342 |
return token
|
343 |
|
344 |
#
|
345 |
# Response helpers
|
346 |
#
|
347 |
|
348 |
def grant_accept_response(self, client, redirect_uri, scope, state): |
349 |
context = {'client': client.get_id(), 'redirect_uri': redirect_uri, |
350 |
'scope': scope, 'state': state, |
351 |
#'url': url,
|
352 |
} |
353 |
json_content = json.dumps(context) |
354 |
return self.response_cls(status=200, body=json_content) |
355 |
|
356 |
def grant_token_response(self, token, token_type): |
357 |
context = {'access_token': token, 'token_type': token_type, |
358 |
'expires_in': self.token_expires} |
359 |
json_content = json.dumps(context) |
360 |
return self.response_cls(status=200, body=json_content) |
361 |
|
362 |
def redirect_to_login_response(self, request, params): |
363 |
parts = list(urlparse.urlsplit(request.path))
|
364 |
parts[3] = urllib.urlencode(params)
|
365 |
query = {'next': urlparse.urlunsplit(parts)}
|
366 |
return Response(302, |
367 |
headers={'Location': '%s?%s' % |
368 |
(self.get_login_uri(),
|
369 |
urllib.urlencode(query))}) |
370 |
|
371 |
def redirect_to_uri(self, redirect_uri, code, state=None): |
372 |
parts = list(urlparse.urlsplit(redirect_uri))
|
373 |
params = dict(urlparse.parse_qsl(parts[3], keep_blank_values=True)) |
374 |
params['code'] = code
|
375 |
if state is not None: |
376 |
params['state'] = state
|
377 |
parts[3] = urllib.urlencode(params)
|
378 |
return Response(302, |
379 |
headers={'Location': '%s' % |
380 |
urlparse.urlunsplit(parts)}) |
381 |
|
382 |
def build_response_from_error(self, exception): |
383 |
response = Response(400)
|
384 |
logger.exception(exception) |
385 |
error = 'generic_error'
|
386 |
if exception.error:
|
387 |
error = exception.error |
388 |
body = { |
389 |
'error': error,
|
390 |
'exception': exception.message,
|
391 |
} |
392 |
response.body = json.dumps(body) |
393 |
response.content_type = "application/json"
|
394 |
return response
|
395 |
|
396 |
#
|
397 |
# Processor methods
|
398 |
#
|
399 |
|
400 |
def process_code_request(self, user, client, uri, scope, state): |
401 |
code = self.add_authorization_code(user, client, uri, scope, state)
|
402 |
return self.redirect_to_uri(uri, code, state) |
403 |
|
404 |
#
|
405 |
# Helpers
|
406 |
#
|
407 |
|
408 |
def grant_authorization_code(self, client, code_instance, redirect_uri, |
409 |
scope=None, token_type="Bearer"): |
410 |
if scope and code_instance.scope != scope: |
411 |
raise OA2Error("Invalid scope") |
412 |
if redirect_uri != code_instance.redirect_uri:
|
413 |
raise OA2Error("The redirect uri does not match " |
414 |
"the one used during authorization")
|
415 |
token = self.add_token_for_client(token_type, code_instance)
|
416 |
self.delete_authorization_code(code_instance) # use only once |
417 |
return token, token_type
|
418 |
|
419 |
def consume_token(self, token): |
420 |
token_instance = self.get_token(token)
|
421 |
if datetime.datetime.now() > token_instance.expires_at:
|
422 |
self.delete_token(token_instance) # delete expired token |
423 |
raise OA2Error("Token has expired") |
424 |
# TODO: delete token?
|
425 |
return token_instance
|
426 |
|
427 |
def _get_credentials(self, params, headers): |
428 |
if 'HTTP_AUTHORIZATION' in headers: |
429 |
scheme, b64credentials = headers.get( |
430 |
'HTTP_AUTHORIZATION').split(" ") |
431 |
if scheme != 'Basic': |
432 |
# TODO: raise 401 + WWW-Authenticate
|
433 |
raise OA2Error("Unsupported authorization scheme") |
434 |
credentials = b64decode(b64credentials).split(":")
|
435 |
return scheme, credentials
|
436 |
else:
|
437 |
return None, None |
438 |
pass
|
439 |
|
440 |
def _get_authorization(self, params, headers): |
441 |
scheme, client_credentials = self._get_credentials(params, headers)
|
442 |
no_authorization = scheme is None and client_credentials is None |
443 |
if no_authorization:
|
444 |
raise OA2Error("Missing authorization header") |
445 |
return client_credentials
|
446 |
|
447 |
def get_redirect_uri_from_params(self, client, params, default=True): |
448 |
"""
|
449 |
Accepts a client instance and request parameters.
|
450 |
"""
|
451 |
redirect_uri = params.get('redirect_uri', None) |
452 |
if not redirect_uri and default: |
453 |
redirect_uri = client.get_default_redirect_uri() |
454 |
else:
|
455 |
# TODO: sanitize redirect_uri (self.clean_redirect_uri ???)
|
456 |
# clean and validate
|
457 |
if not client.redirect_uri_is_valid(redirect_uri): |
458 |
raise OA2Error("Invalid client redirect uri") |
459 |
return redirect_uri
|
460 |
|
461 |
#
|
462 |
# Request identifiers
|
463 |
#
|
464 |
|
465 |
def identify_authorize_request(self, params, headers): |
466 |
return params.get('response_type'), params |
467 |
|
468 |
def identify_token_request(self, headers, params): |
469 |
content_type = headers.get('CONTENT_TYPE')
|
470 |
if content_type != 'application/x-www-form-urlencoded': |
471 |
raise OA2Error("Invalid Content-Type header") |
472 |
return params.get('grant_type') |
473 |
|
474 |
#
|
475 |
# Parameters validation methods
|
476 |
#
|
477 |
|
478 |
def validate_client(self, params, meta, requires_auth=True, |
479 |
client_id_required=True):
|
480 |
client_id = params.get('client_id')
|
481 |
if client_id is None and client_id_required: |
482 |
raise OA2Error("Client identification is required") |
483 |
|
484 |
client_credentials = None
|
485 |
try: # check authorization header |
486 |
client_credentials = self._get_authorization(params, meta)
|
487 |
if client_credentials is not None: |
488 |
_client_id = client_credentials[0]
|
489 |
if client_id is not None and client_id != _client_id: |
490 |
raise OA2Error("Client identification conflicts " |
491 |
"with client authorization")
|
492 |
client_id = _client_id |
493 |
except:
|
494 |
pass
|
495 |
|
496 |
if client_id is None: |
497 |
raise OA2Error("Missing client identification") |
498 |
|
499 |
client = self.get_client_by_id(client_id)
|
500 |
|
501 |
if requires_auth and client.requires_auth(): |
502 |
if client_credentials is None: |
503 |
raise OA2Error("Client authentication is required") |
504 |
|
505 |
if client_credentials is not None: |
506 |
self.check_credentials(client, *client_credentials)
|
507 |
return client
|
508 |
|
509 |
def validate_redirect_uri(self, client, params, headers, |
510 |
allow_default=True, is_required=False, |
511 |
expected_value=None):
|
512 |
redirect_uri = params.get('redirect_uri')
|
513 |
if is_required and redirect_uri is None: |
514 |
raise OA2Error("Missing redirect uri") |
515 |
if redirect_uri is not None: |
516 |
if not bool(urlparse.urlparse(redirect_uri).scheme): |
517 |
raise OA2Error("Redirect uri should be an absolute URI") |
518 |
if not client.redirect_uri_is_valid(redirect_uri): |
519 |
raise OA2Error("Mismatching redirect uri") |
520 |
if expected_value is not None and redirect_uri != expected_value: |
521 |
raise OA2Error("Invalid redirect uri") |
522 |
else:
|
523 |
try:
|
524 |
redirect_uri = client.redirecturl_set.values_list('url',
|
525 |
flat=True)[0] |
526 |
except IndexError: |
527 |
raise OA2Error("Unable to fallback to client redirect URI") |
528 |
return redirect_uri
|
529 |
|
530 |
def validate_state(self, client, params, headers): |
531 |
return params.get('state') |
532 |
raise OA2Error("Invalid state") |
533 |
|
534 |
def validate_scope(self, client, params, headers): |
535 |
scope = params.get('scope')
|
536 |
if scope is not None: |
537 |
scope = scope.split(' ')[0] # keep only the first |
538 |
# TODO: check for invalid characters
|
539 |
return scope
|
540 |
|
541 |
def validate_code(self, client, params, headers): |
542 |
code = params.get('code')
|
543 |
if code is None: |
544 |
raise OA2Error("Missing authorization code") |
545 |
return self.get_client_authorization_code(client, code) |
546 |
|
547 |
#
|
548 |
# Requests validation methods
|
549 |
#
|
550 |
|
551 |
def validate_code_request(self, params, headers): |
552 |
client = self.validate_client(params, headers, requires_auth=False) |
553 |
redirect_uri = self.validate_redirect_uri(client, params, headers)
|
554 |
scope = self.validate_scope(client, params, headers)
|
555 |
scope = scope or redirect_uri # set default |
556 |
state = self.validate_state(client, params, headers)
|
557 |
return client, redirect_uri, scope, state
|
558 |
|
559 |
def validate_token_request(self, params, headers, requires_auth=False): |
560 |
client = self.validate_client(params, headers)
|
561 |
redirect_uri = self.validate_redirect_uri(client, params, headers)
|
562 |
scope = self.validate_scope(client, params, headers)
|
563 |
scope = scope or redirect_uri # set default |
564 |
state = self.validate_state(client, params, headers)
|
565 |
return client, redirect_uri, scope, state
|
566 |
|
567 |
def validate_code_grant(self, params, headers): |
568 |
client = self.validate_client(params, headers,
|
569 |
client_id_required=False)
|
570 |
code_instance = self.validate_code(client, params, headers)
|
571 |
redirect_uri = self.validate_redirect_uri(
|
572 |
client, params, headers, |
573 |
expected_value=code_instance.redirect_uri) |
574 |
return client, redirect_uri, code_instance
|
575 |
|
576 |
#
|
577 |
# Endpoint methods
|
578 |
#
|
579 |
|
580 |
@handles_oa2_requests
|
581 |
def authorize(self, request, **extra): |
582 |
"""
|
583 |
Used in the following cases
|
584 |
"""
|
585 |
if not request.secure: |
586 |
raise OA2Error("Secure request required") |
587 |
|
588 |
# identify
|
589 |
request_params = request.GET |
590 |
if request.method == "POST": |
591 |
request_params = request.POST |
592 |
|
593 |
auth_type, params = self.identify_authorize_request(request_params,
|
594 |
request.META) |
595 |
|
596 |
if auth_type is None: |
597 |
raise OA2Error("Missing authorization type") |
598 |
if auth_type == 'code': |
599 |
client, uri, scope, state = \ |
600 |
self.validate_code_request(params, request.META)
|
601 |
elif auth_type == 'token': |
602 |
raise OA2Error("Unsupported authorization type") |
603 |
# client, uri, scope, state = \
|
604 |
# self.validate_token_request(params, request.META)
|
605 |
else:
|
606 |
#TODO: handle custom type
|
607 |
raise OA2Error("Invalid authorization type") |
608 |
|
609 |
user = getattr(request, 'user', None) |
610 |
if not user: |
611 |
return self.redirect_to_login_response(request, params) |
612 |
|
613 |
if request.method == 'POST': |
614 |
if auth_type == 'code': |
615 |
return self.process_code_request(user, client, uri, scope, |
616 |
state) |
617 |
elif auth_type == 'token': |
618 |
raise OA2Error("Unsupported response type") |
619 |
# return self.process_token_request(user, client, uri, scope,
|
620 |
# state)
|
621 |
else:
|
622 |
#TODO: handle custom type
|
623 |
raise OA2Error("Invalid authorization type") |
624 |
else:
|
625 |
if client.is_trusted:
|
626 |
return self.process_code_request(user, client, uri, scope, |
627 |
state) |
628 |
else:
|
629 |
return self.grant_accept_response(client, uri, scope, state) |
630 |
|
631 |
@handles_oa2_requests
|
632 |
def grant_token(self, request, **extra): |
633 |
"""
|
634 |
Used in the following cases
|
635 |
"""
|
636 |
if not request.secure: |
637 |
raise OA2Error("Secure request required") |
638 |
|
639 |
grant_type = self.identify_token_request(request.META, request.POST)
|
640 |
|
641 |
if grant_type is None: |
642 |
raise OA2Error("Missing grant type") |
643 |
elif grant_type == 'authorization_code': |
644 |
client, redirect_uri, code = \ |
645 |
self.validate_code_grant(request.POST, request.META)
|
646 |
token, token_type = \ |
647 |
self.grant_authorization_code(client, code, redirect_uri)
|
648 |
return self.grant_token_response(token, token_type) |
649 |
elif (grant_type in ['client_credentials', 'token'] or |
650 |
self.is_uri(grant_type)):
|
651 |
raise OA2Error("Unsupported grant type") |
652 |
else:
|
653 |
#TODO: handle custom type
|
654 |
raise OA2Error("Invalid grant type") |