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 RequestValidator
6 from piston.models import Consumer, Nonce, Token
9 class PistonRequestValidator(RequestValidator):
10 dummy_access_token = '!'
14 # It'd be a little more kosher to use test client with secure=True.
17 # iOS app generates 8-char nonces.
20 # Because piston.models.Token.key is char(18).
21 access_token_length = 18, 32
23 def check_client_key(self, client_key):
24 """We control the keys anyway."""
27 def get_access_token_secret(self, client_key, token, request):
28 return request.token.secret
30 def get_default_realms(self, client_key, request):
33 def validate_access_token(self, client_key, token, request):
35 token = Token.objects.get(
36 token_type=Token.ACCESS,
37 consumer__key=client_key,
40 except Token.DoesNotExist:
46 def validate_timestamp_and_nonce(self, client_key, timestamp, nonce,
47 request, request_token=None, access_token=None):
48 # TODO: validate the timestamp
49 token = request_token or access_token
50 # Yes, this is what Piston did.
54 nonce, created = Nonce.objects.get_or_create(consumer_key=client_key,
59 def validate_client_key(self, client_key, request):
61 request.oauth_consumer = Consumer.objects.get(key=client_key)
62 except Consumer.DoesNotExist:
66 def validate_realms(self, client_key, token, request, uri=None, realms=None):
69 def validate_requested_realms(self, *args, **kwargs):
72 def validate_redirect_uri(self, *args, **kwargs):
75 def get_client_secret(self, client_key, request):
76 return request.oauth_consumer.secret
78 def save_request_token(self, token, request):
80 token_type=Token.REQUEST,
81 timestamp=request.timestamp,
82 key=token['oauth_token'],
83 secret=token['oauth_token_secret'],
84 consumer=request.oauth_consumer,