Mercurial > public > sg101
comparison gcalendar/oauth.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/gcalendar/oauth.py@9a4bffdf37c3 |
children | 8743c566f712 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
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; callback url='%s'", callback_url) | |
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() | |
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 called as '%s'", request.get_full_path()) | |
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 | |
83 | |
84 | |
85 def serialize_token(token): | |
86 """ | |
87 This function turns a token into a string and returns it. | |
88 | |
89 """ | |
90 return gdata.gauth.TokenToBlob(token) | |
91 | |
92 | |
93 def deserialize_token(s): | |
94 """ | |
95 This function turns a string into a token returns it. The string must have | |
96 previously been created with serialize_token(). | |
97 | |
98 """ | |
99 return gdata.gauth.TokenFromBlob(s) |