3 from models import Nonce, Token, Consumer
4 from models import generate_random, VERIFIER_SIZE
6 class DataStore(oauth.OAuthDataStore):
7 """Layer between Python OAuth and Django database."""
8 def __init__(self, oauth_request):
9 self.signature = oauth_request.parameters.get('oauth_signature', None)
10 self.timestamp = oauth_request.parameters.get('oauth_timestamp', None)
11 self.scope = oauth_request.parameters.get('scope', 'all')
13 def lookup_consumer(self, key):
15 self.consumer = Consumer.objects.get(key=key)
17 except Consumer.DoesNotExist:
20 def lookup_token(self, token_type, token):
21 if token_type == 'request':
22 token_type = Token.REQUEST
23 elif token_type == 'access':
24 token_type = Token.ACCESS
26 self.request_token = Token.objects.get(key=token,
27 token_type=token_type)
28 return self.request_token
29 except Token.DoesNotExist:
32 def lookup_nonce(self, oauth_consumer, oauth_token, nonce):
33 if oauth_token is None:
35 nonce, created = Nonce.objects.get_or_create(consumer_key=oauth_consumer.key,
36 token_key=oauth_token.key,
43 def fetch_request_token(self, oauth_consumer, oauth_callback):
44 if oauth_consumer.key == self.consumer.key:
45 self.request_token = Token.objects.create_token(consumer=self.consumer,
46 token_type=Token.REQUEST,
47 timestamp=self.timestamp)
50 self.request_token.set_callback(oauth_callback)
52 return self.request_token
55 def fetch_access_token(self, oauth_consumer, oauth_token, oauth_verifier):
56 if oauth_consumer.key == self.consumer.key \
57 and oauth_token.key == self.request_token.key \
58 and oauth_verifier == self.request_token.verifier \
59 and self.request_token.is_approved:
60 self.access_token = Token.objects.create_token(consumer=self.consumer,
61 token_type=Token.ACCESS,
62 timestamp=self.timestamp,
63 user=self.request_token.user)
64 return self.access_token
67 def authorize_request_token(self, oauth_token, user):
68 if oauth_token.key == self.request_token.key:
69 # authorize the request token in the store
70 self.request_token.is_approved = True
71 self.request_token.user = user
72 self.request_token.verifier = generate_random(VERIFIER_SIZE)
73 self.request_token.save()
74 return self.request_token