Skip to content

Commit

Permalink
implemented the callback
Browse files Browse the repository at this point in the history
  • Loading branch information
brosner committed Dec 17, 2009
1 parent 788d9dd commit 9f6482c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion contacts_import/oauth_consumer.py
Expand Up @@ -3,6 +3,7 @@

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.utils import simplejson as json

import oauth2 as oauth
Expand Down Expand Up @@ -63,11 +64,13 @@ def unauthorized_token(self):
return self._unauthorized_token

def fetch_unauthorized_token(self):
base_url = "http://contacts-import.pinaxproject.com"
callback_url = reverse("oauth_callback", kwargs={"service": self.service})
request = oauth.Request.from_consumer_and_token(self.consumer,
http_url = self.request_token_url,
parameters = {
# @@@ fixme
"oauth_callback": "http://contacts-import.pinaxproject.com/c/import_contacts/",
"oauth_callback": "%s%s" % (base_url, callback_url),
}
)
request.sign_request(self.signature_method, self.consumer, None)
Expand All @@ -89,6 +92,13 @@ def authorized_token(self, token):
except KeyError:
raise ServiceFail()

def check_token(self, unauth_token, given_token):
token = oauth.Token.from_string(unauth_token)
if token.key == given_token:
return self.authorized_token(token)
else:
return None

def authorization_url(self, token):
request = oauth.Request.from_consumer_and_token(
self.consumer,
Expand Down
1 change: 1 addition & 0 deletions contacts_import/urls.py
Expand Up @@ -10,4 +10,5 @@
"redirect_to": "/contacts/import_contacts/",
}, name="bbauth_login"),
url(r"oauth/login/(?P<service>\w+)/", "contacts_import.views.oauth_login", name="oauth_login"),
url(r"oauth/callback/(?P<service>\w+)/", "contacts_import.views.oauth_callback", name="oauth_callback"),
)
17 changes: 17 additions & 0 deletions contacts_import/views.py
Expand Up @@ -68,6 +68,7 @@ def import_contacts(request, runner_class=AsyncRunner):
"form": form,
"bbauth_token": request.session.get("bbauth_token"),
"authsub_token": request.session.get("authsub_token"),
"linkedin_token": request.session.get("linkedin_token"),
"page": page,
"task_id": request.session.pop("import_contacts_task_id", None),
}, context_instance=RequestContext(request))
Expand All @@ -92,3 +93,19 @@ def oauth_login(request, service):
token = consumer.unauthorized_token()
request.session["%s_unauth_token" % service] = token.to_string()
return HttpResponseRedirect(consumer.authorization_url(token))


def oauth_callback(request, service):
ctx = RequestContext(request)
consumer = oAuthConsumer(service)
unauth_token = request.session.get("%s_unauth_token" % service, None)
if unauth_token is None:
ctx.update({"error": "token_missing"})
else:
auth_token = consumer.check_token(unauth_token, request.GET.get("oauth_token", "no_token"))
if auth_token:
request.session["%s_token" % service] = (auth_token.key, auth_token.secret)
return HttpResponseRedirect(reverse("import_contacts"))
else:
ctx.update({"error": "token_mismatch"})
return render_to_response("oauth_error.html", ctx)

0 comments on commit 9f6482c

Please sign in to comment.