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.
6 from oauthlib.oauth1 import RequestValidator
7 from piston.models import Consumer, Nonce, Token
10 class PistonRequestValidator(RequestValidator):
11 timestamp_threshold = 300
13 dummy_access_token = '!'
17 # It'd be a little more kosher to use test client with secure=True.
20 # iOS app generates 8-char nonces.
23 # Because piston.models.Token.key is char(18).
24 access_token_length = 18, 32
26 def check_client_key(self, client_key):
27 """We control the keys anyway."""
30 def get_access_token_secret(self, client_key, token, request):
31 return request.token.secret
33 def get_default_realms(self, client_key, request):
36 def validate_access_token(self, client_key, token, request):
38 token = Token.objects.get(
39 token_type=Token.ACCESS,
40 consumer__key=client_key,
43 except Token.DoesNotExist:
49 def validate_timestamp_and_nonce(self, client_key, timestamp, nonce,
50 request, request_token=None, access_token=None):
51 if abs(time.time() - int(timestamp)) > self.timestamp_threshold:
53 token = request_token or access_token
54 # Yes, this is what Piston did.
58 nonce, created = Nonce.objects.get_or_create(consumer_key=client_key,
63 def validate_client_key(self, client_key, request):
65 request.oauth_consumer = Consumer.objects.get(key=client_key)
66 except Consumer.DoesNotExist:
70 def validate_realms(self, client_key, token, request, uri=None, realms=None):
73 def validate_requested_realms(self, *args, **kwargs):
76 def validate_redirect_uri(self, *args, **kwargs):
79 def get_client_secret(self, client_key, request):
80 return request.oauth_consumer.secret
82 def save_request_token(self, token, request):
84 token_type=Token.REQUEST,
85 timestamp=request.timestamp,
86 key=token['oauth_token'],
87 secret=token['oauth_token_secret'],
88 consumer=request.oauth_consumer,
91 def verify_request_token(self, token, request):
92 return Token.objects.filter(
93 token_type=Token.REQUEST, key=token, is_approved=False
96 def get_realms(self, *args, **kwargs):
99 def save_verifier(self, token, verifier, request):
100 Token.objects.filter(
101 token_type=Token.REQUEST,
106 user=verifier['user']
109 def get_redirect_uri(self, token, request):
110 return request.redirect_uri