david / django-oauth (http://oauth.net/)
Support of OAuth in Django. Note that http://code.welldev.org/django-oauth-plus will use python-oauth2 if you're interested in it.
| commit 32: | 338320d4634a |
| parent 31: | 4f12d70ae49d |
| branch: | default |
Avoid saving the request token twice, suggested by Toby White's fork, thanks.
12 months ago
Changed (Δ632 bytes):
raw changeset »
oauth_provider/managers.py (5 lines added, 2 lines removed)
oauth_provider/models.py (3 lines added, 9 lines removed)
oauth_provider/stores.py (12 lines added, 7 lines removed)
Up to file-list oauth_provider/managers.py:
| … | … | @@ -31,13 +31,16 @@ class ResourceManager(models.Manager): |
31 |
31 |
|
32 |
32 |
|
33 |
33 |
class TokenManager(models.Manager): |
34 |
def create_token(self, consumer, token_type, timestamp, resource, |
|
34 |
def create_token(self, consumer, token_type, timestamp, resource, |
|
35 |
user=None, callback=None, callback_confirmed=False): |
|
35 |
36 |
"""Shortcut to create a token with random key/secret.""" |
36 |
37 |
token, created = self.get_or_create(consumer=consumer, |
37 |
38 |
token_type=token_type, |
38 |
39 |
timestamp=timestamp, |
39 |
40 |
resource=resource, |
40 |
user=user |
|
41 |
user=user, |
|
42 |
callback=callback, |
|
43 |
callback_confirmed=callback_confirmed) |
|
41 |
44 |
if created: |
42 |
45 |
token.generate_random_codes() |
43 |
46 |
return token |
Up to file-list oauth_provider/models.py:
| … | … | @@ -113,11 +113,11 @@ class Token(models.Model): |
113 |
113 |
self.secret = secret |
114 |
114 |
self.save() |
115 |
115 |
|
116 |
## OAuth 1.0a stuff |
|
117 |
||
118 |
116 |
def get_callback_url(self): |
117 |
""" |
|
118 |
OAuth 1.0a, append the oauth_verifier. |
|
119 |
""" |
|
119 |
120 |
if self.callback and self.verifier: |
120 |
# Append the oauth_verifier. |
|
121 |
121 |
parts = urlparse.urlparse(self.callback) |
122 |
122 |
scheme, netloc, path, params, query, fragment = parts[:6] |
123 |
123 |
if query: |
| … | … | @@ -127,9 +127,3 @@ class Token(models.Model): |
127 |
127 |
return urlparse.urlunparse((scheme, netloc, path, params, |
128 |
128 |
query, fragment)) |
129 |
129 |
return self.callback |
130 |
||
131 |
def set_callback(self, callback): |
|
132 |
if callback != "oob": # out of band, says "we can't do this!" |
|
133 |
self.callback = callback |
|
134 |
self.callback_confirmed = True |
|
135 |
self.save() |
Up to file-list oauth_provider/stores.py:
| … | … | @@ -48,9 +48,15 @@ class DataStore(OAuthDataStore): |
48 |
48 |
raise OAuthError('Consumer key does not match.') |
49 |
49 |
|
50 |
50 |
# OAuth 1.0a: if there is a callback, check its validity |
51 |
if oauth_callback and \ |
|
52 |
not (oauth_callback == "oob" or check_valid_callback(oauth_callback)): |
|
53 |
|
|
51 |
callback = None |
|
52 |
callback_confirmed = False |
|
53 |
if oauth_callback: |
|
54 |
if oauth_callback != "oob": |
|
55 |
if check_valid_callback(oauth_callback): |
|
56 |
callback = oauth_callback |
|
57 |
callback_confirmed = True |
|
58 |
else: |
|
59 |
raise OAuthError('Invalid callback URL.') |
|
54 |
60 |
|
55 |
61 |
try: |
56 |
62 |
resource = Resource.objects.get(name=self.scope) |
| … | … | @@ -59,10 +65,9 @@ class DataStore(OAuthDataStore): |
59 |
65 |
self.request_token = Token.objects.create_token(consumer=self.consumer, |
60 |
66 |
token_type=Token.REQUEST, |
61 |
67 |
timestamp=self.timestamp, |
62 |
resource=resource) |
|
63 |
# OAuth 1.0a: if there is a callback, set it |
|
64 |
if oauth_callback: |
|
65 |
self.request_token.set_callback(oauth_callback) |
|
68 |
resource=resource, |
|
69 |
callback=callback, |
|
70 |
callback_confirmed=callback_confirmed) |
|
66 |
71 |
|
67 |
72 |
return self.request_token |
68 |
73 |
