comparison gpp/gcalendar/oauth.py @ 451:345825e6dcae

Working on #220. Can't test locally, so committing in increments.
author Brian Neal <bgneal@gmail.com>
date Thu, 30 Jun 2011 01:57:17 +0000
parents
children c77359d0d951
comparison
equal deleted inserted replaced
450:acc129bfd0d3 451:345825e6dcae
1 """
2 This module handles the OAuth integration with Google.
3
4 """
5 from __future__ import with_statement
6 import logging
7
8 import gdata.gauth
9 from gdata.calendar_resource.client import CalendarResourceClient
10
11 from django.conf import settings
12
13
14 logger = logging.getLogger(__name__)
15 USER_AGENT = 'surfguitar101-gcalendar-v1'
16 REQ_TOKEN_SESSION_KEY = 'gcalendar oauth request token'
17
18
19 def fetch_auth(request, scopes, callback_url):
20 """
21 This function fetches a request token from Google and stores it in the
22 session. It then returns the authorization URL as a string.
23
24 request - the HttpRequest object for the user requesting the token. The
25 token is stored in the session object attached to this request.
26
27 scopes - a list of scope strings that the request token is for. See
28 http://code.google.com/apis/gdata/faq.html#AuthScopes
29
30 callback_url - a string that is the URL that Google should redirect the user
31 to after the user has authorized our application access to their data.
32
33 This function only supports RSA-SHA1 authentication. Settings in the Django
34 settings module determine the consumer key and path to the RSA private key.
35 """
36 logger.info("fetch_auth started...")
37 client = CalendarResourceClient(None, source=USER_AGENT)
38
39 with open(settings.GOOGLE_OAUTH_PRIVATE_KEY_PATH, 'r') as f:
40 rsa_key = f.read()
41 logger.info("read RSA key; now getting request token")
42
43 request_token = client.GetOAuthToken(
44 scopes,
45 callback_url,
46 settings.GOOGLE_OAUTH_CONSUMER_KEY,
47 rsa_private_key=rsa_key)
48
49 logger.info("received token")
50 request.session[REQ_TOKEN_SESSION_KEY] = request_token
51
52 auth_url = request_token.generate_authorization_url(google_apps_domain=None)
53 logger.info("generated auth url '%s'", str(auth_url))
54
55 return str(auth_url)
56
57
58 def get_access_token(request):
59 """
60 This function should be called after Google has sent the user back to us
61 after the user authorized us. We retrieve the oauth token from the request
62 URL and then upgrade it to an access token. We then return the access token.
63
64 """
65 logger.info("get_access_token started; retrieving saved request_token...")
66
67 saved_token = request.session.get(REQ_TOKEN_SESSION_KEY)
68 if saved_token is None:
69 logger.error("saved request token not found in session!")
70 return None
71
72 logger.info("extracting token...")
73 request_token = gdata.gauth.AuthorizeRequestToken(saved_token,
74 request.build_absolute_uri())
75
76 logger.info("upgrading to access token...")
77
78 client = CalendarResourceClient(None, source=USER_AGENT)
79 access_token = client.GetAccessToken(request_token)
80
81 logger.info("upgraded to access token...")
82 return access_token