Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
| 5 | """Utilities for OAuth. |
| 6 | |
| 7 | Utilities for making it easier to work with OAuth. |
| 8 | """ |
| 9 | |
| 10 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 11 | |
| 12 | import copy |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 13 | import httplib2 |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 14 | import oauth2 as oauth |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 15 | import urllib |
| 16 | import logging |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 17 | |
| 18 | try: |
| 19 | from urlparse import parse_qs, parse_qsl |
| 20 | except ImportError: |
| 21 | from cgi import parse_qs, parse_qsl |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 22 | |
| 23 | |
Tom Miller | 05cd4f5 | 2010-10-06 11:09:12 -0700 | [diff] [blame] | 24 | class Error(Exception): |
| 25 | """Base error for this module.""" |
| 26 | pass |
| 27 | |
| 28 | |
| 29 | class RequestError(Error): |
| 30 | """Error occurred during request.""" |
| 31 | pass |
| 32 | |
| 33 | |
| 34 | class MissingParameter(Error): |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 35 | pass |
| 36 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 37 | |
| 38 | def _abstract(): |
Joe Gregorio | 7943c5d | 2010-09-08 16:11:43 -0400 | [diff] [blame] | 39 | raise NotImplementedError('You need to override this function') |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 40 | |
| 41 | |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 42 | def _oauth_uri(name, discovery, params): |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 43 | """Look up the OAuth UR from the discovery |
| 44 | document and add query parameters based on |
| 45 | params. |
| 46 | |
| 47 | name - The name of the OAuth URI to lookup, one |
| 48 | of 'request', 'access', or 'authorize'. |
| 49 | discovery - Portion of discovery document the describes |
| 50 | the OAuth endpoints. |
| 51 | params - Dictionary that is used to form the query parameters |
| 52 | for the specified URI. |
| 53 | """ |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 54 | if name not in ['request', 'access', 'authorize']: |
| 55 | raise KeyError(name) |
Joe Gregorio | 7943c5d | 2010-09-08 16:11:43 -0400 | [diff] [blame] | 56 | keys = discovery[name]['parameters'].keys() |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 57 | query = {} |
| 58 | for key in keys: |
| 59 | if key in params: |
| 60 | query[key] = params[key] |
| 61 | return discovery[name]['url'] + '?' + urllib.urlencode(query) |
| 62 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 63 | |
| 64 | class Credentials(object): |
| 65 | """Base class for all Credentials objects. |
| 66 | |
| 67 | Subclasses must define an authorize() method |
| 68 | that applies the credentials to an HTTP transport. |
| 69 | """ |
| 70 | |
| 71 | def authorize(self, http): |
| 72 | """Take an httplib2.Http instance (or equivalent) and |
| 73 | authorizes it for the set of credentials, usually by |
| 74 | replacing http.request() with a method that adds in |
| 75 | the appropriate headers and then delegates to the original |
| 76 | Http.request() method. |
| 77 | """ |
| 78 | _abstract() |
| 79 | |
| 80 | |
| 81 | class OAuthCredentials(Credentials): |
| 82 | """Credentials object for OAuth 1.0a |
| 83 | """ |
| 84 | |
| 85 | def __init__(self, consumer, token, user_agent): |
| 86 | """ |
| 87 | consumer - An instance of oauth.Consumer. |
| 88 | token - An instance of oauth.Token constructed with |
| 89 | the access token and secret. |
| 90 | user_agent - The HTTP User-Agent to provide for this application. |
| 91 | """ |
| 92 | self.consumer = consumer |
| 93 | self.token = token |
| 94 | self.user_agent = user_agent |
| 95 | |
| 96 | def authorize(self, http): |
| 97 | """ |
| 98 | Args: |
| 99 | http - An instance of httplib2.Http |
| 100 | or something that acts like it. |
| 101 | |
| 102 | Returns: |
| 103 | A modified instance of http that was passed in. |
| 104 | |
| 105 | Example: |
| 106 | |
| 107 | h = httplib2.Http() |
| 108 | h = credentials.authorize(h) |
| 109 | |
| 110 | You can't create a new OAuth |
| 111 | subclass of httplib2.Authenication because |
| 112 | it never gets passed the absolute URI, which is |
| 113 | needed for signing. So instead we have to overload |
| 114 | 'request' with a closure that adds in the |
| 115 | Authorization header and then calls the original version |
| 116 | of 'request()'. |
| 117 | """ |
| 118 | request_orig = http.request |
| 119 | signer = oauth.SignatureMethod_HMAC_SHA1() |
| 120 | |
| 121 | # The closure that will replace 'httplib2.Http.request'. |
Joe Gregorio | 7943c5d | 2010-09-08 16:11:43 -0400 | [diff] [blame] | 122 | def new_request(uri, method='GET', body=None, headers=None, |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 123 | redirections=httplib2.DEFAULT_MAX_REDIRECTS, |
| 124 | connection_type=None): |
| 125 | """Modify the request headers to add the appropriate |
| 126 | Authorization header.""" |
| 127 | req = oauth.Request.from_consumer_and_token( |
| 128 | self.consumer, self.token, http_method=method, http_url=uri) |
| 129 | req.sign_request(signer, self.consumer, self.token) |
| 130 | if headers == None: |
| 131 | headers = {} |
| 132 | headers.update(req.to_header()) |
Joe Gregorio | 46b0ff6 | 2010-10-09 22:13:12 -0400 | [diff] [blame^] | 133 | if 'user-agent' in headers: |
| 134 | headers['user-agent'] += ' ' |
| 135 | else: |
| 136 | headers['user-agent'] = '' |
| 137 | headers['user-agent'] += self.user_agent |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 138 | return request_orig(uri, method, body, headers, |
| 139 | redirections, connection_type) |
| 140 | |
| 141 | http.request = new_request |
| 142 | return http |
| 143 | |
| 144 | |
| 145 | class FlowThreeLegged(object): |
| 146 | """Does the Three Legged Dance for OAuth 1.0a. |
| 147 | """ |
| 148 | |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 149 | def __init__(self, discovery, consumer_key, consumer_secret, user_agent, |
| 150 | **kwargs): |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 151 | """ |
| 152 | discovery - Section of the API discovery document that describes |
| 153 | the OAuth endpoints. |
| 154 | consumer_key - OAuth consumer key |
| 155 | consumer_secret - OAuth consumer secret |
| 156 | user_agent - The HTTP User-Agent that identifies the application. |
| 157 | **kwargs - The keyword arguments are all optional and required |
| 158 | parameters for the OAuth calls. |
| 159 | """ |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 160 | self.discovery = discovery |
| 161 | self.consumer_key = consumer_key |
| 162 | self.consumer_secret = consumer_secret |
| 163 | self.user_agent = user_agent |
| 164 | self.params = kwargs |
| 165 | self.request_token = {} |
Joe Gregorio | 7943c5d | 2010-09-08 16:11:43 -0400 | [diff] [blame] | 166 | required = {} |
| 167 | for uriinfo in discovery.itervalues(): |
| 168 | for name, value in uriinfo['parameters'].iteritems(): |
| 169 | if value['required'] and not name.startswith('oauth_'): |
| 170 | required[name] = 1 |
| 171 | for key in required.iterkeys(): |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 172 | if key not in self.params: |
| 173 | raise MissingParameter('Required parameter %s not supplied' % key) |
| 174 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 175 | def step1_get_authorize_url(self, oauth_callback='oob'): |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 176 | """Returns a URI to redirect to the provider. |
| 177 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 178 | oauth_callback - Either the string 'oob' for a non-web-based application, |
| 179 | or a URI that handles the callback from the authorization |
| 180 | server. |
| 181 | |
| 182 | If oauth_callback is 'oob' then pass in the |
| 183 | generated verification code to step2_exchange, |
| 184 | otherwise pass in the query parameters received |
| 185 | at the callback uri to step2_exchange. |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 186 | """ |
| 187 | consumer = oauth.Consumer(self.consumer_key, self.consumer_secret) |
| 188 | client = oauth.Client(consumer) |
| 189 | |
| 190 | headers = { |
| 191 | 'user-agent': self.user_agent, |
| 192 | 'content-type': 'application/x-www-form-urlencoded' |
| 193 | } |
| 194 | body = urllib.urlencode({'oauth_callback': oauth_callback}) |
| 195 | uri = _oauth_uri('request', self.discovery, self.params) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 196 | |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 197 | resp, content = client.request(uri, 'POST', headers=headers, |
| 198 | body=body) |
| 199 | if resp['status'] != '200': |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 200 | logging.error('Failed to retrieve temporary authorization: %s' % content) |
Tom Miller | 05cd4f5 | 2010-10-06 11:09:12 -0700 | [diff] [blame] | 201 | raise RequestError('Invalid response %s.' % resp['status']) |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 202 | |
| 203 | self.request_token = dict(parse_qsl(content)) |
| 204 | |
| 205 | auth_params = copy.copy(self.params) |
| 206 | auth_params['oauth_token'] = self.request_token['oauth_token'] |
| 207 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 208 | return _oauth_uri('authorize', self.discovery, auth_params) |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 209 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 210 | def step2_exchange(self, verifier): |
| 211 | """Exhanges an authorized request token |
| 212 | for OAuthCredentials. |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 213 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 214 | verifier - either the verifier token, or a dictionary |
| 215 | of the query parameters to the callback, which contains |
| 216 | the oauth_verifier. |
| 217 | """ |
| 218 | |
| 219 | if not (isinstance(verifier, str) or isinstance(verifier, unicode)): |
| 220 | verifier = verifier['oauth_verifier'] |
| 221 | |
| 222 | token = oauth.Token( |
| 223 | self.request_token['oauth_token'], |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 224 | self.request_token['oauth_token_secret']) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 225 | token.set_verifier(verifier) |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 226 | consumer = oauth.Consumer(self.consumer_key, self.consumer_secret) |
| 227 | client = oauth.Client(consumer, token) |
| 228 | |
| 229 | headers = { |
| 230 | 'user-agent': self.user_agent, |
| 231 | 'content-type': 'application/x-www-form-urlencoded' |
| 232 | } |
| 233 | |
| 234 | uri = _oauth_uri('access', self.discovery, self.params) |
| 235 | resp, content = client.request(uri, 'POST', headers=headers) |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 236 | if resp['status'] != '200': |
| 237 | logging.error('Failed to retrieve access token: %s' % content) |
Tom Miller | 05cd4f5 | 2010-10-06 11:09:12 -0700 | [diff] [blame] | 238 | raise RequestError('Invalid response %s.' % resp['status']) |
Joe Gregorio | 67d7777 | 2010-09-01 16:45:45 -0400 | [diff] [blame] | 239 | |
Joe Gregorio | 845a545 | 2010-09-08 13:50:34 -0400 | [diff] [blame] | 240 | oauth_params = dict(parse_qsl(content)) |
| 241 | token = oauth.Token( |
| 242 | oauth_params['oauth_token'], |
| 243 | oauth_params['oauth_token_secret']) |
| 244 | |
| 245 | return OAuthCredentials(consumer, token, self.user_agent) |