Move authorize endpoint to OAuthlib.
[wolnelektury.git] / src / api / utils.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.http import HttpResponse
6 from django.utils.encoding import iri_to_uri
7
8
9 def oauthlib_request(request):
10     """Creates parameters for OAuthlib's Request from a Django Request."""
11     headers = {}
12     # We don't have request.content_type yet in 2015,
13     # while test client has no META['CONTENT_TYPE'].
14     ct = request.META.get('CONTENT_TYPE', getattr(request, 'content_type', None))
15     if ct:
16         headers["Content-Type"] = ct
17     if 'HTTP_AUTHORIZATION' in request.META:
18         headers["Authorization"] = request.META['HTTP_AUTHORIZATION']
19     return {
20         "uri": request.build_absolute_uri(),
21         "http_method": request.method,
22         "body": request.body,
23         "headers": headers,
24     }
25
26 def oauthlib_response((headers, body, status)):
27     """Creates a django.http.HttpResponse from (headers, body, status) tuple from OAuthlib."""
28     response = HttpResponse(body, status=status)
29     for k, v in headers.items():
30         if k == 'Location':
31             v = iri_to_uri(v)
32         response[k] = v
33     return response