X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/29f694eea94d09bd66976321ddfd80ac30b73d38..5abc88f0d2ad3036dd526fbff5f0241f1d6480e1:/src/api/views.py?ds=inline diff --git a/src/api/views.py b/src/api/views.py index 518445dd0..be1491b6f 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -1,22 +1,24 @@ -# -*- coding: utf-8 -*- # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from django.contrib.auth.decorators import login_required +from django import forms +from django.http import HttpResponse from django.http import Http404 +from django.shortcuts import render from django.views.generic.base import View from oauthlib.common import urlencode from oauthlib.oauth1 import RequestTokenEndpoint, AccessTokenEndpoint -from piston.models import KEY_SIZE, SECRET_SIZE +from oauthlib.oauth1 import AuthorizationEndpoint, OAuth1Error from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView -from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404 -from migdal.models import Entry +from rest_framework.generics import RetrieveAPIView, get_object_or_404 from catalogue.models import Book -from .models import BookUserData +from .models import BookUserData, KEY_SIZE, SECRET_SIZE from . import serializers from .request_validator import PistonRequestValidator -from .utils import oauthlib_request, oauthlib_response +from .utils import oauthlib_request, oauthlib_response, vary_on_auth class OAuth1RequestTokenEndpoint(RequestTokenEndpoint): @@ -35,6 +37,7 @@ class OAuth1RequestTokenEndpoint(RequestTokenEndpoint): return urlencode(token.items()) +# Never Cache class OAuth1RequestTokenView(View): def __init__(self): self.endpoint = OAuth1RequestTokenEndpoint(PistonRequestValidator()) @@ -47,6 +50,55 @@ class OAuth1RequestTokenView(View): ) +class OAuthAuthenticationForm(forms.Form): + oauth_token = forms.CharField(widget=forms.HiddenInput) + oauth_callback = forms.CharField(widget=forms.HiddenInput) # changed from URLField - too strict + # removed authorize_access - redundant + + +class OAuth1AuthorizationEndpoint(AuthorizationEndpoint): + def create_verifier(self, request, credentials): + verifier = super(OAuth1AuthorizationEndpoint, self).create_verifier(request, credentials) + return { + 'oauth_token': verifier['oauth_token'], + } + + +@login_required +def oauth_user_auth(request): + endpoint = OAuth1AuthorizationEndpoint(PistonRequestValidator()) + + if request.method == "GET": + # Why not just get oauth_token here? + # This is fairly straightforward, in't? + try: + realms, credentials = endpoint.get_realms_and_credentials( + **oauthlib_request(request)) + except OAuth1Error as e: + return HttpResponse(str(e), status=400) + callback = request.GET.get('oauth_callback') + + form = OAuthAuthenticationForm(initial={ + 'oauth_token': credentials['resource_owner_key'], + 'oauth_callback': callback, + }) + + return render(request, 'oauth/authorize_token.html', {'form': form}) + + if request.method == "POST": + try: + response = oauthlib_response( + endpoint.create_authorization_response( + credentials={"user": request.user}, + **oauthlib_request(request) + ) + ) + except OAuth1Error as e: + return HttpResponse(e.message, status=400) + else: + return response + + class OAuth1AccessTokenEndpoint(AccessTokenEndpoint): def _create_request(self, *args, **kwargs): r = super(OAuth1AccessTokenEndpoint, self)._create_request(*args, **kwargs) @@ -66,6 +118,7 @@ class OAuth1AccessTokenEndpoint(AccessTokenEndpoint): return urlencode(token.items()) +# Never cache class OAuth1AccessTokenView(View): def __init__(self): self.endpoint = OAuth1AccessTokenEndpoint(PistonRequestValidator()) @@ -78,6 +131,7 @@ class OAuth1AccessTokenView(View): ) +@vary_on_auth class UserView(RetrieveAPIView): permission_classes = [IsAuthenticated] serializer_class = serializers.UserSerializer @@ -86,6 +140,7 @@ class UserView(RetrieveAPIView): return self.request.user +@vary_on_auth class BookUserDataView(RetrieveAPIView): permission_classes = [IsAuthenticated] serializer_class = serializers.BookUserDataSerializer @@ -111,15 +166,6 @@ class BookUserDataView(RetrieveAPIView): return Response(serializer.data) -class BlogView(ListAPIView): - serializer_class = serializers.BlogSerializer - - def get_queryset(self): - after = self.request.query_params.get('after') - count = int(self.request.query_params.get('count', 20)) - entries = Entry.published_objects.filter(in_stream=True).order_by('-first_published_at') - if after: - entries = entries.filter(first_published_at__lt=after) - if count: - entries = entries[:count] - return entries +class BlogView(APIView): + def get(self, request): + return Response([])