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.
5 from oauthlib.oauth1 import AuthorizationEndpoint, OAuth1Error
6 from django.contrib.auth.decorators import login_required
7 from django import forms
8 from django.http import HttpResponse, HttpResponseRedirect
9 from django.shortcuts import render
10 from .request_validator import PistonRequestValidator
11 from .utils import oauthlib_request, oauthlib_response
14 class HttpResponseAppRedirect(HttpResponseRedirect):
15 allowed_schemes = HttpResponseRedirect.allowed_schemes + ['wolnelekturyapp']
18 class OAuthAuthenticationForm(forms.Form):
19 oauth_token = forms.CharField(widget=forms.HiddenInput)
20 oauth_callback = forms.CharField(widget=forms.HiddenInput) # changed from URLField - too strict
21 # removed authorize_access - redundant
24 class OAuth1AuthorizationEndpoint(AuthorizationEndpoint):
25 def create_verifier(self, request, credentials):
26 verifier = super(OAuth1AuthorizationEndpoint, self).create_verifier(request, credentials)
28 'oauth_token': verifier['oauth_token'],
33 def oauth_user_auth(request):
34 endpoint = OAuth1AuthorizationEndpoint(PistonRequestValidator())
36 if request.method == "GET":
37 # Why not just get oauth_token here?
38 # This is fairly straightforward, in't?
40 realms, credentials = endpoint.get_realms_and_credentials(
41 **oauthlib_request(request))
42 except OAuth1Error as e:
43 return HttpResponse(e.message, status=400)
44 callback = request.GET.get('oauth_callback')
46 form = OAuthAuthenticationForm(initial={
47 'oauth_token': credentials['resource_owner_key'],
48 'oauth_callback': callback,
51 return render(request, 'piston/authorize_token.html', {'form': form})
53 elif request.method == "POST":
55 response = oauthlib_response(
56 endpoint.create_authorization_response(
57 credentials={"user": request.user},
58 **oauthlib_request(request)
61 except OAuth1Error as e:
62 return HttpResponse(e.message, status=400)