Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 1 | # Copyright (C) 2010 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 14 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 15 | """An OAuth 2.0 client. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 16 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 17 | Tools for interacting with OAuth 2.0 protected resources. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 18 | """ |
| 19 | |
| 20 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 21 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 22 | import base64 |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 23 | import clientsecrets |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 24 | import copy |
| 25 | import datetime |
| 26 | import httplib2 |
| 27 | import logging |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 28 | import os |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 29 | import sys |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 30 | import time |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 31 | import urllib |
| 32 | import urlparse |
| 33 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 34 | from oauth2client import GOOGLE_AUTH_URI |
| 35 | from oauth2client import GOOGLE_REVOKE_URI |
| 36 | from oauth2client import GOOGLE_TOKEN_URI |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 37 | from oauth2client import util |
| 38 | from oauth2client.anyjson import simplejson |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 39 | |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 40 | HAS_CRYPTO = False |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 41 | try: |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 42 | from oauth2client import crypt |
| 43 | HAS_CRYPTO = True |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 44 | except ImportError: |
| 45 | pass |
| 46 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 47 | try: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 48 | from urlparse import parse_qsl |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 49 | except ImportError: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 50 | from cgi import parse_qsl |
| 51 | |
| 52 | logger = logging.getLogger(__name__) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 53 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 54 | # Expiry is stored in RFC3339 UTC format |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 55 | EXPIRY_FORMAT = '%Y-%m-%dT%H:%M:%SZ' |
| 56 | |
| 57 | # Which certs to use to validate id_tokens received. |
| 58 | ID_TOKEN_VERIFICATON_CERTS = 'https://www.googleapis.com/oauth2/v1/certs' |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 59 | |
Joe Gregorio | f2326c0 | 2012-02-09 12:18:44 -0500 | [diff] [blame] | 60 | # Constant to use for the out of band OAuth 2.0 flow. |
| 61 | OOB_CALLBACK_URN = 'urn:ietf:wg:oauth:2.0:oob' |
| 62 | |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame] | 63 | # Google Data client libraries may need to set this to [401, 403]. |
| 64 | REFRESH_STATUS_CODES = [401] |
| 65 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 66 | |
| 67 | class Error(Exception): |
| 68 | """Base error for this module.""" |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 69 | |
| 70 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 71 | class FlowExchangeError(Error): |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 72 | """Error trying to exchange an authorization grant for an access token.""" |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 73 | |
| 74 | |
| 75 | class AccessTokenRefreshError(Error): |
Joe Gregorio | ca876e4 | 2011-02-22 19:39:42 -0500 | [diff] [blame] | 76 | """Error trying to refresh an expired access token.""" |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 77 | |
| 78 | |
| 79 | class TokenRevokeError(Error): |
| 80 | """Error trying to revoke a token.""" |
| 81 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 82 | |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 83 | class UnknownClientSecretsFlowError(Error): |
| 84 | """The client secrets file called for an unknown type of OAuth 2.0 flow. """ |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 85 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 86 | |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 87 | class AccessTokenCredentialsError(Error): |
| 88 | """Having only the access_token means no refresh is possible.""" |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 89 | |
| 90 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 91 | class VerifyJwtTokenError(Error): |
| 92 | """Could on retrieve certificates for validation.""" |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 93 | |
| 94 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 95 | class NonAsciiHeaderError(Error): |
| 96 | """Header names and values must be ASCII strings.""" |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 97 | |
| 98 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 99 | def _abstract(): |
| 100 | raise NotImplementedError('You need to override this function') |
| 101 | |
| 102 | |
Joe Gregorio | 9f2f38f | 2012-02-06 12:53:00 -0500 | [diff] [blame] | 103 | class MemoryCache(object): |
| 104 | """httplib2 Cache implementation which only caches locally.""" |
| 105 | |
| 106 | def __init__(self): |
| 107 | self.cache = {} |
| 108 | |
| 109 | def get(self, key): |
| 110 | return self.cache.get(key) |
| 111 | |
| 112 | def set(self, key, value): |
| 113 | self.cache[key] = value |
| 114 | |
| 115 | def delete(self, key): |
| 116 | self.cache.pop(key, None) |
| 117 | |
| 118 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 119 | class Credentials(object): |
| 120 | """Base class for all Credentials objects. |
| 121 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 122 | Subclasses must define an authorize() method that applies the credentials to |
| 123 | an HTTP transport. |
| 124 | |
| 125 | Subclasses must also specify a classmethod named 'from_json' that takes a JSON |
Joe Gregorio | fa8cd9f | 2012-02-23 14:00:40 -0500 | [diff] [blame] | 126 | string as input and returns an instaniated Credentials object. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 127 | """ |
| 128 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 129 | NON_SERIALIZED_MEMBERS = ['store'] |
| 130 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 131 | def authorize(self, http): |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 132 | """Take an httplib2.Http instance (or equivalent) and authorizes it. |
| 133 | |
| 134 | Authorizes it for the set of credentials, usually by replacing |
| 135 | http.request() with a method that adds in the appropriate headers and then |
| 136 | delegates to the original Http.request() method. |
| 137 | |
| 138 | Args: |
| 139 | http: httplib2.Http, an http object to be used to make the refresh |
| 140 | request. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 141 | """ |
| 142 | _abstract() |
| 143 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 144 | def refresh(self, http): |
| 145 | """Forces a refresh of the access_token. |
| 146 | |
| 147 | Args: |
| 148 | http: httplib2.Http, an http object to be used to make the refresh |
| 149 | request. |
| 150 | """ |
| 151 | _abstract() |
| 152 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 153 | def revoke(self, http): |
| 154 | """Revokes a refresh_token and makes the credentials void. |
| 155 | |
| 156 | Args: |
| 157 | http: httplib2.Http, an http object to be used to make the revoke |
| 158 | request. |
| 159 | """ |
| 160 | _abstract() |
| 161 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 162 | def apply(self, headers): |
| 163 | """Add the authorization to the headers. |
| 164 | |
| 165 | Args: |
| 166 | headers: dict, the headers to add the Authorization header to. |
| 167 | """ |
| 168 | _abstract() |
| 169 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 170 | def _to_json(self, strip): |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 171 | """Utility function that creates JSON repr. of a Credentials object. |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 172 | |
| 173 | Args: |
| 174 | strip: array, An array of names of members to not include in the JSON. |
| 175 | |
| 176 | Returns: |
| 177 | string, a JSON representation of this instance, suitable to pass to |
| 178 | from_json(). |
| 179 | """ |
| 180 | t = type(self) |
| 181 | d = copy.copy(self.__dict__) |
| 182 | for member in strip: |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 183 | if member in d: |
| 184 | del d[member] |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 185 | if 'token_expiry' in d and isinstance(d['token_expiry'], datetime.datetime): |
| 186 | d['token_expiry'] = d['token_expiry'].strftime(EXPIRY_FORMAT) |
| 187 | # Add in information we will need later to reconsistitue this instance. |
| 188 | d['_class'] = t.__name__ |
| 189 | d['_module'] = t.__module__ |
| 190 | return simplejson.dumps(d) |
| 191 | |
| 192 | def to_json(self): |
| 193 | """Creating a JSON representation of an instance of Credentials. |
| 194 | |
| 195 | Returns: |
| 196 | string, a JSON representation of this instance, suitable to pass to |
| 197 | from_json(). |
| 198 | """ |
| 199 | return self._to_json(Credentials.NON_SERIALIZED_MEMBERS) |
| 200 | |
| 201 | @classmethod |
| 202 | def new_from_json(cls, s): |
| 203 | """Utility class method to instantiate a Credentials subclass from a JSON |
| 204 | representation produced by to_json(). |
| 205 | |
| 206 | Args: |
| 207 | s: string, JSON from to_json(). |
| 208 | |
| 209 | Returns: |
| 210 | An instance of the subclass of Credentials that was serialized with |
| 211 | to_json(). |
| 212 | """ |
| 213 | data = simplejson.loads(s) |
| 214 | # Find and call the right classmethod from_json() to restore the object. |
| 215 | module = data['_module'] |
Joe Gregorio | fa8cd9f | 2012-02-23 14:00:40 -0500 | [diff] [blame] | 216 | try: |
| 217 | m = __import__(module) |
| 218 | except ImportError: |
| 219 | # In case there's an object from the old package structure, update it |
| 220 | module = module.replace('.apiclient', '') |
| 221 | m = __import__(module) |
| 222 | |
Joe Gregorio | e9b40f1 | 2011-10-13 10:03:28 -0400 | [diff] [blame] | 223 | m = __import__(module, fromlist=module.split('.')[:-1]) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 224 | kls = getattr(m, data['_class']) |
| 225 | from_json = getattr(kls, 'from_json') |
| 226 | return from_json(s) |
| 227 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 228 | @classmethod |
| 229 | def from_json(cls, s): |
Joe Gregorio | 401b842 | 2012-05-03 16:35:35 -0400 | [diff] [blame] | 230 | """Instantiate a Credentials object from a JSON description of it. |
| 231 | |
| 232 | The JSON should have been produced by calling .to_json() on the object. |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 233 | |
| 234 | Args: |
| 235 | data: dict, A deserialized JSON object. |
| 236 | |
| 237 | Returns: |
| 238 | An instance of a Credentials subclass. |
| 239 | """ |
| 240 | return Credentials() |
| 241 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 242 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 243 | class Flow(object): |
| 244 | """Base class for all Flow objects.""" |
| 245 | pass |
| 246 | |
| 247 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 248 | class Storage(object): |
| 249 | """Base class for all Storage objects. |
| 250 | |
Joe Gregorio | e2233cd | 2013-01-24 15:46:23 -0500 | [diff] [blame] | 251 | Store and retrieve a single credential. This class supports locking |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 252 | such that multiple processes and threads can operate on a single |
| 253 | store. |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 254 | """ |
| 255 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 256 | def acquire_lock(self): |
| 257 | """Acquires any lock necessary to access this Storage. |
| 258 | |
Joe Gregorio | 401b842 | 2012-05-03 16:35:35 -0400 | [diff] [blame] | 259 | This lock is not reentrant. |
| 260 | """ |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 261 | pass |
| 262 | |
| 263 | def release_lock(self): |
| 264 | """Release the Storage lock. |
| 265 | |
| 266 | Trying to release a lock that isn't held will result in a |
| 267 | RuntimeError. |
| 268 | """ |
| 269 | pass |
| 270 | |
| 271 | def locked_get(self): |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 272 | """Retrieve credential. |
| 273 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 274 | The Storage lock must be held when this is called. |
| 275 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 276 | Returns: |
Joe Gregorio | 06d852b | 2011-03-25 15:03:10 -0400 | [diff] [blame] | 277 | oauth2client.client.Credentials |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 278 | """ |
| 279 | _abstract() |
| 280 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 281 | def locked_put(self, credentials): |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 282 | """Write a credential. |
| 283 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 284 | The Storage lock must be held when this is called. |
| 285 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 286 | Args: |
| 287 | credentials: Credentials, the credentials to store. |
| 288 | """ |
| 289 | _abstract() |
| 290 | |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame] | 291 | def locked_delete(self): |
| 292 | """Delete a credential. |
| 293 | |
| 294 | The Storage lock must be held when this is called. |
| 295 | """ |
| 296 | _abstract() |
| 297 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 298 | def get(self): |
| 299 | """Retrieve credential. |
| 300 | |
| 301 | The Storage lock must *not* be held when this is called. |
| 302 | |
| 303 | Returns: |
| 304 | oauth2client.client.Credentials |
| 305 | """ |
| 306 | self.acquire_lock() |
| 307 | try: |
| 308 | return self.locked_get() |
| 309 | finally: |
| 310 | self.release_lock() |
| 311 | |
| 312 | def put(self, credentials): |
| 313 | """Write a credential. |
| 314 | |
| 315 | The Storage lock must be held when this is called. |
| 316 | |
| 317 | Args: |
| 318 | credentials: Credentials, the credentials to store. |
| 319 | """ |
| 320 | self.acquire_lock() |
| 321 | try: |
| 322 | self.locked_put(credentials) |
| 323 | finally: |
| 324 | self.release_lock() |
| 325 | |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame] | 326 | def delete(self): |
| 327 | """Delete credential. |
| 328 | |
| 329 | Frees any resources associated with storing the credential. |
| 330 | The Storage lock must *not* be held when this is called. |
| 331 | |
| 332 | Returns: |
| 333 | None |
| 334 | """ |
| 335 | self.acquire_lock() |
| 336 | try: |
| 337 | return self.locked_delete() |
| 338 | finally: |
| 339 | self.release_lock() |
| 340 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 341 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 342 | def clean_headers(headers): |
| 343 | """Forces header keys and values to be strings, i.e not unicode. |
| 344 | |
| 345 | The httplib module just concats the header keys and values in a way that may |
| 346 | make the message header a unicode string, which, if it then tries to |
| 347 | contatenate to a binary request body may result in a unicode decode error. |
| 348 | |
| 349 | Args: |
| 350 | headers: dict, A dictionary of headers. |
| 351 | |
| 352 | Returns: |
| 353 | The same dictionary but with all the keys converted to strings. |
| 354 | """ |
| 355 | clean = {} |
| 356 | try: |
| 357 | for k, v in headers.iteritems(): |
| 358 | clean[str(k)] = str(v) |
| 359 | except UnicodeEncodeError: |
| 360 | raise NonAsciiHeaderError(k + ': ' + v) |
| 361 | return clean |
| 362 | |
| 363 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 364 | def _update_query_params(uri, params): |
| 365 | """Updates a URI with new query parameters. |
| 366 | |
| 367 | Args: |
| 368 | uri: string, A valid URI, with potential existing query parameters. |
| 369 | params: dict, A dictionary of query parameters. |
| 370 | |
| 371 | Returns: |
| 372 | The same URI but with the new query parameters added. |
| 373 | """ |
| 374 | parts = list(urlparse.urlparse(uri)) |
| 375 | query_params = dict(parse_qsl(parts[4])) # 4 is the index of the query part |
| 376 | query_params.update(params) |
| 377 | parts[4] = urllib.urlencode(query_params) |
| 378 | return urlparse.urlunparse(parts) |
| 379 | |
| 380 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 381 | class OAuth2Credentials(Credentials): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 382 | """Credentials object for OAuth 2.0. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 383 | |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 384 | Credentials can be applied to an httplib2.Http object using the authorize() |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 385 | method, which then adds the OAuth 2.0 access token to each request. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 386 | |
| 387 | OAuth2Credentials objects may be safely pickled and unpickled. |
| 388 | """ |
| 389 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 390 | @util.positional(8) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 391 | def __init__(self, access_token, client_id, client_secret, refresh_token, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 392 | token_expiry, token_uri, user_agent, revoke_uri=None, |
| 393 | id_token=None): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 394 | """Create an instance of OAuth2Credentials. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 395 | |
| 396 | This constructor is not usually called by the user, instead |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 397 | OAuth2Credentials objects are instantiated by the OAuth2WebServerFlow. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 398 | |
| 399 | Args: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 400 | access_token: string, access token. |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 401 | client_id: string, client identifier. |
| 402 | client_secret: string, client secret. |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 403 | refresh_token: string, refresh token. |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 404 | token_expiry: datetime, when the access_token expires. |
| 405 | token_uri: string, URI of token endpoint. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 406 | user_agent: string, The HTTP User-Agent to provide for this application. |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 407 | revoke_uri: string, URI for revoke endpoint. Defaults to None; a token |
| 408 | can't be revoked if this is None. |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 409 | id_token: object, The identity of the resource owner. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 410 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 411 | Notes: |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 412 | store: callable, A callable that when passed a Credential |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 413 | will store the credential back to where it came from. |
| 414 | This is needed to store the latest access_token if it |
| 415 | has expired and been refreshed. |
| 416 | """ |
| 417 | self.access_token = access_token |
| 418 | self.client_id = client_id |
| 419 | self.client_secret = client_secret |
| 420 | self.refresh_token = refresh_token |
| 421 | self.store = None |
| 422 | self.token_expiry = token_expiry |
| 423 | self.token_uri = token_uri |
| 424 | self.user_agent = user_agent |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 425 | self.revoke_uri = revoke_uri |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 426 | self.id_token = id_token |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 427 | |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 428 | # True if the credentials have been revoked or expired and can't be |
| 429 | # refreshed. |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 430 | self.invalid = False |
Joe Gregorio | 9ce4b62 | 2011-02-17 15:32:11 -0500 | [diff] [blame] | 431 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 432 | def authorize(self, http): |
| 433 | """Authorize an httplib2.Http instance with these credentials. |
| 434 | |
| 435 | The modified http.request method will add authentication headers to each |
| 436 | request and will refresh access_tokens when a 401 is received on a |
| 437 | request. In addition the http.request method has a credentials property, |
| 438 | http.request.credentials, which is the Credentials object that authorized |
| 439 | it. |
| 440 | |
| 441 | Args: |
| 442 | http: An instance of httplib2.Http |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 443 | or something that acts like it. |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 444 | |
| 445 | Returns: |
| 446 | A modified instance of http that was passed in. |
| 447 | |
| 448 | Example: |
| 449 | |
| 450 | h = httplib2.Http() |
| 451 | h = credentials.authorize(h) |
| 452 | |
| 453 | You can't create a new OAuth subclass of httplib2.Authenication |
| 454 | because it never gets passed the absolute URI, which is needed for |
| 455 | signing. So instead we have to overload 'request' with a closure |
| 456 | that adds in the Authorization header and then calls the original |
| 457 | version of 'request()'. |
| 458 | """ |
| 459 | request_orig = http.request |
| 460 | |
| 461 | # The closure that will replace 'httplib2.Http.request'. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 462 | @util.positional(1) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 463 | def new_request(uri, method='GET', body=None, headers=None, |
| 464 | redirections=httplib2.DEFAULT_MAX_REDIRECTS, |
| 465 | connection_type=None): |
| 466 | if not self.access_token: |
| 467 | logger.info('Attempting refresh to obtain initial access_token') |
| 468 | self._refresh(request_orig) |
| 469 | |
| 470 | # Modify the request headers to add the appropriate |
| 471 | # Authorization header. |
| 472 | if headers is None: |
| 473 | headers = {} |
| 474 | self.apply(headers) |
| 475 | |
| 476 | if self.user_agent is not None: |
| 477 | if 'user-agent' in headers: |
| 478 | headers['user-agent'] = self.user_agent + ' ' + headers['user-agent'] |
| 479 | else: |
| 480 | headers['user-agent'] = self.user_agent |
| 481 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 482 | resp, content = request_orig(uri, method, body, clean_headers(headers), |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 483 | redirections, connection_type) |
| 484 | |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame] | 485 | if resp.status in REFRESH_STATUS_CODES: |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 486 | logger.info('Refreshing due to a %s' % str(resp.status)) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 487 | self._refresh(request_orig) |
| 488 | self.apply(headers) |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 489 | return request_orig(uri, method, body, clean_headers(headers), |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 490 | redirections, connection_type) |
| 491 | else: |
| 492 | return (resp, content) |
| 493 | |
| 494 | # Replace the request method with our own closure. |
| 495 | http.request = new_request |
| 496 | |
| 497 | # Set credentials as a property of the request method. |
| 498 | setattr(http.request, 'credentials', self) |
| 499 | |
| 500 | return http |
| 501 | |
| 502 | def refresh(self, http): |
| 503 | """Forces a refresh of the access_token. |
| 504 | |
| 505 | Args: |
| 506 | http: httplib2.Http, an http object to be used to make the refresh |
| 507 | request. |
| 508 | """ |
| 509 | self._refresh(http.request) |
| 510 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 511 | def revoke(self, http): |
| 512 | """Revokes a refresh_token and makes the credentials void. |
| 513 | |
| 514 | Args: |
| 515 | http: httplib2.Http, an http object to be used to make the revoke |
| 516 | request. |
| 517 | """ |
| 518 | self._revoke(http.request) |
| 519 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 520 | def apply(self, headers): |
| 521 | """Add the authorization to the headers. |
| 522 | |
| 523 | Args: |
| 524 | headers: dict, the headers to add the Authorization header to. |
| 525 | """ |
| 526 | headers['Authorization'] = 'Bearer ' + self.access_token |
| 527 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 528 | def to_json(self): |
| 529 | return self._to_json(Credentials.NON_SERIALIZED_MEMBERS) |
| 530 | |
| 531 | @classmethod |
| 532 | def from_json(cls, s): |
| 533 | """Instantiate a Credentials object from a JSON description of it. The JSON |
| 534 | should have been produced by calling .to_json() on the object. |
| 535 | |
| 536 | Args: |
| 537 | data: dict, A deserialized JSON object. |
| 538 | |
| 539 | Returns: |
| 540 | An instance of a Credentials subclass. |
| 541 | """ |
| 542 | data = simplejson.loads(s) |
| 543 | if 'token_expiry' in data and not isinstance(data['token_expiry'], |
| 544 | datetime.datetime): |
Joe Gregorio | 1daa71b | 2011-09-15 18:12:14 -0400 | [diff] [blame] | 545 | try: |
| 546 | data['token_expiry'] = datetime.datetime.strptime( |
| 547 | data['token_expiry'], EXPIRY_FORMAT) |
| 548 | except: |
| 549 | data['token_expiry'] = None |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 550 | retval = OAuth2Credentials( |
| 551 | data['access_token'], |
| 552 | data['client_id'], |
| 553 | data['client_secret'], |
| 554 | data['refresh_token'], |
| 555 | data['token_expiry'], |
| 556 | data['token_uri'], |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 557 | data['user_agent'], |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 558 | revoke_uri=data.get('revoke_uri', None), |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 559 | id_token=data.get('id_token', None)) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 560 | retval.invalid = data['invalid'] |
| 561 | return retval |
| 562 | |
Joe Gregorio | 9ce4b62 | 2011-02-17 15:32:11 -0500 | [diff] [blame] | 563 | @property |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 564 | def access_token_expired(self): |
| 565 | """True if the credential is expired or invalid. |
| 566 | |
| 567 | If the token_expiry isn't set, we assume the token doesn't expire. |
| 568 | """ |
| 569 | if self.invalid: |
| 570 | return True |
| 571 | |
| 572 | if not self.token_expiry: |
| 573 | return False |
| 574 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 575 | now = datetime.datetime.utcnow() |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 576 | if now >= self.token_expiry: |
| 577 | logger.info('access_token is expired. Now: %s, token_expiry: %s', |
| 578 | now, self.token_expiry) |
| 579 | return True |
| 580 | return False |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 581 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 582 | def set_store(self, store): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 583 | """Set the Storage for the credential. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 584 | |
| 585 | Args: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 586 | store: Storage, an implementation of Stroage object. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 587 | This is needed to store the latest access_token if it |
Joe Gregorio | e2233cd | 2013-01-24 15:46:23 -0500 | [diff] [blame] | 588 | has expired and been refreshed. This implementation uses |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 589 | locking to check for updates before updating the |
| 590 | access_token. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 591 | """ |
| 592 | self.store = store |
| 593 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 594 | def _updateFromCredential(self, other): |
| 595 | """Update this Credential from another instance.""" |
| 596 | self.__dict__.update(other.__getstate__()) |
| 597 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 598 | def __getstate__(self): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 599 | """Trim the state down to something that can be pickled.""" |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 600 | d = copy.copy(self.__dict__) |
| 601 | del d['store'] |
| 602 | return d |
| 603 | |
| 604 | def __setstate__(self, state): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 605 | """Reconstitute the state of the object from being pickled.""" |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 606 | self.__dict__.update(state) |
| 607 | self.store = None |
| 608 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 609 | def _generate_refresh_request_body(self): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 610 | """Generate the body that will be used in the refresh request.""" |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 611 | body = urllib.urlencode({ |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 612 | 'grant_type': 'refresh_token', |
| 613 | 'client_id': self.client_id, |
| 614 | 'client_secret': self.client_secret, |
| 615 | 'refresh_token': self.refresh_token, |
| 616 | }) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 617 | return body |
| 618 | |
| 619 | def _generate_refresh_request_headers(self): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 620 | """Generate the headers that will be used in the refresh request.""" |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 621 | headers = { |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 622 | 'content-type': 'application/x-www-form-urlencoded', |
| 623 | } |
JacobMoshenko | cb6d891 | 2011-07-08 13:35:15 -0400 | [diff] [blame] | 624 | |
| 625 | if self.user_agent is not None: |
| 626 | headers['user-agent'] = self.user_agent |
| 627 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 628 | return headers |
| 629 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 630 | def _refresh(self, http_request): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 631 | """Refreshes the access_token. |
| 632 | |
| 633 | This method first checks by reading the Storage object if available. |
| 634 | If a refresh is still needed, it holds the Storage lock until the |
| 635 | refresh is completed. |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 636 | |
| 637 | Args: |
| 638 | http_request: callable, a callable that matches the method signature of |
| 639 | httplib2.Http.request, used to make the refresh request. |
| 640 | |
| 641 | Raises: |
| 642 | AccessTokenRefreshError: When the refresh fails. |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 643 | """ |
| 644 | if not self.store: |
| 645 | self._do_refresh_request(http_request) |
| 646 | else: |
| 647 | self.store.acquire_lock() |
| 648 | try: |
| 649 | new_cred = self.store.locked_get() |
| 650 | if (new_cred and not new_cred.invalid and |
| 651 | new_cred.access_token != self.access_token): |
| 652 | logger.info('Updated access_token read from Storage') |
| 653 | self._updateFromCredential(new_cred) |
| 654 | else: |
| 655 | self._do_refresh_request(http_request) |
| 656 | finally: |
| 657 | self.store.release_lock() |
| 658 | |
| 659 | def _do_refresh_request(self, http_request): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 660 | """Refresh the access_token using the refresh_token. |
| 661 | |
| 662 | Args: |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 663 | http_request: callable, a callable that matches the method signature of |
| 664 | httplib2.Http.request, used to make the refresh request. |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 665 | |
| 666 | Raises: |
| 667 | AccessTokenRefreshError: When the refresh fails. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 668 | """ |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 669 | body = self._generate_refresh_request_body() |
| 670 | headers = self._generate_refresh_request_headers() |
| 671 | |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 672 | logger.info('Refreshing access_token') |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 673 | resp, content = http_request( |
| 674 | self.token_uri, method='POST', body=body, headers=headers) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 675 | if resp.status == 200: |
| 676 | # TODO(jcgregorio) Raise an error if loads fails? |
| 677 | d = simplejson.loads(content) |
| 678 | self.access_token = d['access_token'] |
| 679 | self.refresh_token = d.get('refresh_token', self.refresh_token) |
| 680 | if 'expires_in' in d: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 681 | self.token_expiry = datetime.timedelta( |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 682 | seconds=int(d['expires_in'])) + datetime.datetime.utcnow() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 683 | else: |
| 684 | self.token_expiry = None |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 685 | if self.store: |
| 686 | self.store.locked_put(self) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 687 | else: |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 688 | # An {'error':...} response body means the token is expired or revoked, |
| 689 | # so we flag the credentials as such. |
Joe Gregorio | e78621a | 2012-03-09 15:47:23 -0500 | [diff] [blame] | 690 | logger.info('Failed to retrieve access token: %s' % content) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 691 | error_msg = 'Invalid response %s.' % resp['status'] |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 692 | try: |
| 693 | d = simplejson.loads(content) |
| 694 | if 'error' in d: |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 695 | error_msg = d['error'] |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 696 | self.invalid = True |
| 697 | if self.store: |
| 698 | self.store.locked_put(self) |
Joe Gregorio | fd08e43 | 2012-08-09 14:17:41 -0400 | [diff] [blame] | 699 | except StandardError: |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 700 | pass |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 701 | raise AccessTokenRefreshError(error_msg) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 702 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 703 | def _revoke(self, http_request): |
| 704 | """Revokes the refresh_token and deletes the store if available. |
| 705 | |
| 706 | Args: |
| 707 | http_request: callable, a callable that matches the method signature of |
| 708 | httplib2.Http.request, used to make the revoke request. |
| 709 | """ |
| 710 | self._do_revoke(http_request, self.refresh_token) |
| 711 | |
| 712 | def _do_revoke(self, http_request, token): |
| 713 | """Revokes the credentials and deletes the store if available. |
| 714 | |
| 715 | Args: |
| 716 | http_request: callable, a callable that matches the method signature of |
| 717 | httplib2.Http.request, used to make the refresh request. |
| 718 | token: A string used as the token to be revoked. Can be either an |
| 719 | access_token or refresh_token. |
| 720 | |
| 721 | Raises: |
| 722 | TokenRevokeError: If the revoke request does not return with a 200 OK. |
| 723 | """ |
| 724 | logger.info('Revoking token') |
| 725 | query_params = {'token': token} |
| 726 | token_revoke_uri = _update_query_params(self.revoke_uri, query_params) |
| 727 | resp, content = http_request(token_revoke_uri) |
| 728 | if resp.status == 200: |
| 729 | self.invalid = True |
| 730 | else: |
| 731 | error_msg = 'Invalid response %s.' % resp.status |
| 732 | try: |
| 733 | d = simplejson.loads(content) |
| 734 | if 'error' in d: |
| 735 | error_msg = d['error'] |
| 736 | except StandardError: |
| 737 | pass |
| 738 | raise TokenRevokeError(error_msg) |
| 739 | |
| 740 | if self.store: |
| 741 | self.store.delete() |
| 742 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 743 | |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 744 | class AccessTokenCredentials(OAuth2Credentials): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 745 | """Credentials object for OAuth 2.0. |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 746 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 747 | Credentials can be applied to an httplib2.Http object using the |
| 748 | authorize() method, which then signs each request from that object |
Joe Gregorio | e2233cd | 2013-01-24 15:46:23 -0500 | [diff] [blame] | 749 | with the OAuth 2.0 access token. This set of credentials is for the |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 750 | use case where you have acquired an OAuth 2.0 access_token from |
| 751 | another place such as a JavaScript client or another web |
| 752 | application, and wish to use it from Python. Because only the |
| 753 | access_token is present it can not be refreshed and will in time |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 754 | expire. |
| 755 | |
Joe Gregorio | 9ce4b62 | 2011-02-17 15:32:11 -0500 | [diff] [blame] | 756 | AccessTokenCredentials objects may be safely pickled and unpickled. |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 757 | |
| 758 | Usage: |
| 759 | credentials = AccessTokenCredentials('<an access token>', |
| 760 | 'my-user-agent/1.0') |
| 761 | http = httplib2.Http() |
| 762 | http = credentials.authorize(http) |
| 763 | |
| 764 | Exceptions: |
| 765 | AccessTokenCredentialsExpired: raised when the access_token expires or is |
| 766 | revoked. |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 767 | """ |
| 768 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 769 | def __init__(self, access_token, user_agent, revoke_uri=None): |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 770 | """Create an instance of OAuth2Credentials |
| 771 | |
| 772 | This is one of the few types if Credentials that you should contrust, |
| 773 | Credentials objects are usually instantiated by a Flow. |
| 774 | |
| 775 | Args: |
ade@google.com | 93a7f7c | 2011-02-23 16:00:37 +0000 | [diff] [blame] | 776 | access_token: string, access token. |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 777 | user_agent: string, The HTTP User-Agent to provide for this application. |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 778 | revoke_uri: string, URI for revoke endpoint. Defaults to None; a token |
| 779 | can't be revoked if this is None. |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 780 | """ |
| 781 | super(AccessTokenCredentials, self).__init__( |
| 782 | access_token, |
| 783 | None, |
| 784 | None, |
| 785 | None, |
| 786 | None, |
| 787 | None, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 788 | user_agent, |
| 789 | revoke_uri=revoke_uri) |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 790 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 791 | |
| 792 | @classmethod |
| 793 | def from_json(cls, s): |
| 794 | data = simplejson.loads(s) |
| 795 | retval = AccessTokenCredentials( |
| 796 | data['access_token'], |
| 797 | data['user_agent']) |
| 798 | return retval |
| 799 | |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 800 | def _refresh(self, http_request): |
| 801 | raise AccessTokenCredentialsError( |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 802 | 'The access_token is expired or invalid and can\'t be refreshed.') |
| 803 | |
| 804 | def _revoke(self, http_request): |
| 805 | """Revokes the access_token and deletes the store if available. |
| 806 | |
| 807 | Args: |
| 808 | http_request: callable, a callable that matches the method signature of |
| 809 | httplib2.Http.request, used to make the revoke request. |
| 810 | """ |
| 811 | self._do_revoke(http_request, self.access_token) |
Joe Gregorio | 3b79fa8 | 2011-02-17 11:47:17 -0500 | [diff] [blame] | 812 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 813 | |
| 814 | class AssertionCredentials(OAuth2Credentials): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 815 | """Abstract Credentials object used for OAuth 2.0 assertion grants. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 816 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 817 | This credential does not require a flow to instantiate because it |
| 818 | represents a two legged flow, and therefore has all of the required |
Joe Gregorio | e2233cd | 2013-01-24 15:46:23 -0500 | [diff] [blame] | 819 | information to generate and refresh its own access tokens. It must |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 820 | be subclassed to generate the appropriate assertion string. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 821 | |
| 822 | AssertionCredentials objects may be safely pickled and unpickled. |
| 823 | """ |
| 824 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 825 | @util.positional(2) |
| 826 | def __init__(self, assertion_type, user_agent=None, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 827 | token_uri=GOOGLE_TOKEN_URI, |
| 828 | revoke_uri=GOOGLE_REVOKE_URI, |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 829 | **unused_kwargs): |
| 830 | """Constructor for AssertionFlowCredentials. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 831 | |
| 832 | Args: |
| 833 | assertion_type: string, assertion type that will be declared to the auth |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 834 | server |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 835 | user_agent: string, The HTTP User-Agent to provide for this application. |
| 836 | token_uri: string, URI for token endpoint. For convenience |
| 837 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 838 | revoke_uri: string, URI for revoke endpoint. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 839 | """ |
| 840 | super(AssertionCredentials, self).__init__( |
| 841 | None, |
| 842 | None, |
| 843 | None, |
| 844 | None, |
| 845 | None, |
| 846 | token_uri, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 847 | user_agent, |
| 848 | revoke_uri=revoke_uri) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 849 | self.assertion_type = assertion_type |
| 850 | |
| 851 | def _generate_refresh_request_body(self): |
| 852 | assertion = self._generate_assertion() |
| 853 | |
| 854 | body = urllib.urlencode({ |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 855 | 'assertion': assertion, |
Joe Gregorio | cdc350f | 2013-02-07 10:52:26 -0500 | [diff] [blame] | 856 | 'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer', |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 857 | }) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 858 | |
| 859 | return body |
| 860 | |
| 861 | def _generate_assertion(self): |
| 862 | """Generate the assertion string that will be used in the access token |
| 863 | request. |
| 864 | """ |
| 865 | _abstract() |
| 866 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 867 | def _revoke(self, http_request): |
| 868 | """Revokes the access_token and deletes the store if available. |
| 869 | |
| 870 | Args: |
| 871 | http_request: callable, a callable that matches the method signature of |
| 872 | httplib2.Http.request, used to make the revoke request. |
| 873 | """ |
| 874 | self._do_revoke(http_request, self.access_token) |
| 875 | |
| 876 | |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 877 | if HAS_CRYPTO: |
| 878 | # PyOpenSSL and PyCrypto are not prerequisites for oauth2client, so if it is |
| 879 | # missing then don't create the SignedJwtAssertionCredentials or the |
| 880 | # verify_id_token() method. |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 881 | |
| 882 | class SignedJwtAssertionCredentials(AssertionCredentials): |
| 883 | """Credentials object used for OAuth 2.0 Signed JWT assertion grants. |
| 884 | |
Joe Gregorio | 672051e | 2012-07-10 09:11:45 -0400 | [diff] [blame] | 885 | This credential does not require a flow to instantiate because it represents |
| 886 | a two legged flow, and therefore has all of the required information to |
| 887 | generate and refresh its own access tokens. |
| 888 | |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 889 | SignedJwtAssertionCredentials requires either PyOpenSSL, or PyCrypto 2.6 or |
| 890 | later. For App Engine you may also consider using AppAssertionCredentials. |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 891 | """ |
| 892 | |
| 893 | MAX_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds |
| 894 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 895 | @util.positional(4) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 896 | def __init__(self, |
| 897 | service_account_name, |
| 898 | private_key, |
| 899 | scope, |
| 900 | private_key_password='notasecret', |
| 901 | user_agent=None, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 902 | token_uri=GOOGLE_TOKEN_URI, |
| 903 | revoke_uri=GOOGLE_REVOKE_URI, |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 904 | **kwargs): |
| 905 | """Constructor for SignedJwtAssertionCredentials. |
| 906 | |
| 907 | Args: |
| 908 | service_account_name: string, id for account, usually an email address. |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 909 | private_key: string, private key in PKCS12 or PEM format. |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 910 | scope: string or iterable of strings, scope(s) of the credentials being |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 911 | requested. |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 912 | private_key_password: string, password for private_key, unused if |
| 913 | private_key is in PEM format. |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 914 | user_agent: string, HTTP User-Agent to provide for this application. |
| 915 | token_uri: string, URI for token endpoint. For convenience |
| 916 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 917 | revoke_uri: string, URI for revoke endpoint. |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 918 | kwargs: kwargs, Additional parameters to add to the JWT token, for |
| 919 | example prn=joe@xample.org.""" |
| 920 | |
| 921 | super(SignedJwtAssertionCredentials, self).__init__( |
dhermes@google.com | 2cc0938 | 2013-02-11 08:42:18 -0800 | [diff] [blame^] | 922 | None, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 923 | user_agent=user_agent, |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 924 | token_uri=token_uri, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 925 | revoke_uri=revoke_uri, |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 926 | ) |
| 927 | |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 928 | self.scope = util.scopes_to_string(scope) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 929 | |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 930 | # Keep base64 encoded so it can be stored in JSON. |
| 931 | self.private_key = base64.b64encode(private_key) |
| 932 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 933 | self.private_key_password = private_key_password |
| 934 | self.service_account_name = service_account_name |
| 935 | self.kwargs = kwargs |
| 936 | |
| 937 | @classmethod |
| 938 | def from_json(cls, s): |
| 939 | data = simplejson.loads(s) |
| 940 | retval = SignedJwtAssertionCredentials( |
| 941 | data['service_account_name'], |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 942 | base64.b64decode(data['private_key']), |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 943 | data['scope'], |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 944 | private_key_password=data['private_key_password'], |
| 945 | user_agent=data['user_agent'], |
| 946 | token_uri=data['token_uri'], |
| 947 | **data['kwargs'] |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 948 | ) |
| 949 | retval.invalid = data['invalid'] |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 950 | retval.access_token = data['access_token'] |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 951 | return retval |
| 952 | |
| 953 | def _generate_assertion(self): |
| 954 | """Generate the assertion that will be used in the request.""" |
| 955 | now = long(time.time()) |
| 956 | payload = { |
| 957 | 'aud': self.token_uri, |
| 958 | 'scope': self.scope, |
| 959 | 'iat': now, |
| 960 | 'exp': now + SignedJwtAssertionCredentials.MAX_TOKEN_LIFETIME_SECS, |
| 961 | 'iss': self.service_account_name |
| 962 | } |
| 963 | payload.update(self.kwargs) |
Joe Gregorio | e78621a | 2012-03-09 15:47:23 -0500 | [diff] [blame] | 964 | logger.debug(str(payload)) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 965 | |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 966 | private_key = base64.b64decode(self.private_key) |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 967 | return crypt.make_signed_jwt(crypt.Signer.from_string( |
| 968 | private_key, self.private_key_password), payload) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 969 | |
Joe Gregorio | 9f2f38f | 2012-02-06 12:53:00 -0500 | [diff] [blame] | 970 | # Only used in verify_id_token(), which is always calling to the same URI |
| 971 | # for the certs. |
| 972 | _cached_http = httplib2.Http(MemoryCache()) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 973 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 974 | @util.positional(2) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 975 | def verify_id_token(id_token, audience, http=None, |
| 976 | cert_uri=ID_TOKEN_VERIFICATON_CERTS): |
| 977 | """Verifies a signed JWT id_token. |
| 978 | |
Joe Gregorio | 672051e | 2012-07-10 09:11:45 -0400 | [diff] [blame] | 979 | This function requires PyOpenSSL and because of that it does not work on |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 980 | App Engine. |
Joe Gregorio | 672051e | 2012-07-10 09:11:45 -0400 | [diff] [blame] | 981 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 982 | Args: |
| 983 | id_token: string, A Signed JWT. |
| 984 | audience: string, The audience 'aud' that the token should be for. |
| 985 | http: httplib2.Http, instance to use to make the HTTP request. Callers |
| 986 | should supply an instance that has caching enabled. |
| 987 | cert_uri: string, URI of the certificates in JSON format to |
| 988 | verify the JWT against. |
| 989 | |
| 990 | Returns: |
| 991 | The deserialized JSON in the JWT. |
| 992 | |
| 993 | Raises: |
| 994 | oauth2client.crypt.AppIdentityError if the JWT fails to verify. |
| 995 | """ |
| 996 | if http is None: |
Joe Gregorio | 9f2f38f | 2012-02-06 12:53:00 -0500 | [diff] [blame] | 997 | http = _cached_http |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 998 | |
| 999 | resp, content = http.request(cert_uri) |
| 1000 | |
| 1001 | if resp.status == 200: |
| 1002 | certs = simplejson.loads(content) |
Joe Gregorio | 0b723c2 | 2013-01-03 15:00:50 -0500 | [diff] [blame] | 1003 | return crypt.verify_signed_jwt_with_certs(id_token, certs, audience) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 1004 | else: |
| 1005 | raise VerifyJwtTokenError('Status code: %d' % resp.status) |
| 1006 | |
| 1007 | |
| 1008 | def _urlsafe_b64decode(b64string): |
Joe Gregorio | bd512b5 | 2011-12-06 15:39:26 -0500 | [diff] [blame] | 1009 | # Guard against unicode strings, which base64 can't handle. |
| 1010 | b64string = b64string.encode('ascii') |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 1011 | padded = b64string + '=' * (4 - len(b64string) % 4) |
| 1012 | return base64.urlsafe_b64decode(padded) |
| 1013 | |
| 1014 | |
| 1015 | def _extract_id_token(id_token): |
| 1016 | """Extract the JSON payload from a JWT. |
| 1017 | |
| 1018 | Does the extraction w/o checking the signature. |
| 1019 | |
| 1020 | Args: |
| 1021 | id_token: string, OAuth 2.0 id_token. |
| 1022 | |
| 1023 | Returns: |
| 1024 | object, The deserialized JSON payload. |
| 1025 | """ |
| 1026 | segments = id_token.split('.') |
| 1027 | |
| 1028 | if (len(segments) != 3): |
| 1029 | raise VerifyJwtTokenError( |
| 1030 | 'Wrong number of segments in token: %s' % id_token) |
| 1031 | |
| 1032 | return simplejson.loads(_urlsafe_b64decode(segments[1])) |
| 1033 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1034 | |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 1035 | def _parse_exchange_token_response(content): |
| 1036 | """Parses response of an exchange token request. |
| 1037 | |
| 1038 | Most providers return JSON but some (e.g. Facebook) return a |
| 1039 | url-encoded string. |
| 1040 | |
| 1041 | Args: |
| 1042 | content: The body of a response |
| 1043 | |
| 1044 | Returns: |
| 1045 | Content as a dictionary object. Note that the dict could be empty, |
| 1046 | i.e. {}. That basically indicates a failure. |
| 1047 | """ |
| 1048 | resp = {} |
| 1049 | try: |
| 1050 | resp = simplejson.loads(content) |
| 1051 | except StandardError: |
| 1052 | # different JSON libs raise different exceptions, |
| 1053 | # so we just do a catch-all here |
| 1054 | resp = dict(parse_qsl(content)) |
| 1055 | |
| 1056 | # some providers respond with 'expires', others with 'expires_in' |
| 1057 | if resp and 'expires' in resp: |
| 1058 | resp['expires_in'] = resp.pop('expires') |
| 1059 | |
| 1060 | return resp |
| 1061 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1062 | |
| 1063 | @util.positional(4) |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1064 | def credentials_from_code(client_id, client_secret, scope, code, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1065 | redirect_uri='postmessage', http=None, |
| 1066 | user_agent=None, token_uri=GOOGLE_TOKEN_URI, |
| 1067 | auth_uri=GOOGLE_AUTH_URI, |
| 1068 | revoke_uri=GOOGLE_REVOKE_URI): |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1069 | """Exchanges an authorization code for an OAuth2Credentials object. |
| 1070 | |
| 1071 | Args: |
| 1072 | client_id: string, client identifier. |
| 1073 | client_secret: string, client secret. |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 1074 | scope: string or iterable of strings, scope(s) to request. |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1075 | code: string, An authroization code, most likely passed down from |
| 1076 | the client |
| 1077 | redirect_uri: string, this is generally set to 'postmessage' to match the |
| 1078 | redirect_uri that the client specified |
| 1079 | http: httplib2.Http, optional http instance to use to do the fetch |
| 1080 | token_uri: string, URI for token endpoint. For convenience |
| 1081 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1082 | auth_uri: string, URI for authorization endpoint. For convenience |
| 1083 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
| 1084 | revoke_uri: string, URI for revoke endpoint. For convenience |
| 1085 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
| 1086 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1087 | Returns: |
| 1088 | An OAuth2Credentials object. |
| 1089 | |
| 1090 | Raises: |
| 1091 | FlowExchangeError if the authorization code cannot be exchanged for an |
| 1092 | access token |
| 1093 | """ |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1094 | flow = OAuth2WebServerFlow(client_id, client_secret, scope, |
| 1095 | redirect_uri=redirect_uri, user_agent=user_agent, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1096 | auth_uri=auth_uri, token_uri=token_uri, |
| 1097 | revoke_uri=revoke_uri) |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1098 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1099 | credentials = flow.step2_exchange(code, http=http) |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1100 | return credentials |
| 1101 | |
| 1102 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1103 | @util.positional(3) |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1104 | def credentials_from_clientsecrets_and_code(filename, scope, code, |
| 1105 | message = None, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1106 | redirect_uri='postmessage', |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 1107 | http=None, |
| 1108 | cache=None): |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1109 | """Returns OAuth2Credentials from a clientsecrets file and an auth code. |
| 1110 | |
| 1111 | Will create the right kind of Flow based on the contents of the clientsecrets |
| 1112 | file or will raise InvalidClientSecretsError for unknown types of Flows. |
| 1113 | |
| 1114 | Args: |
| 1115 | filename: string, File name of clientsecrets. |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 1116 | scope: string or iterable of strings, scope(s) to request. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1117 | code: string, An authorization code, most likely passed down from |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1118 | the client |
| 1119 | message: string, A friendly string to display to the user if the |
| 1120 | clientsecrets file is missing or invalid. If message is provided then |
| 1121 | sys.exit will be called in the case of an error. If message in not |
| 1122 | provided then clientsecrets.InvalidClientSecretsError will be raised. |
| 1123 | redirect_uri: string, this is generally set to 'postmessage' to match the |
| 1124 | redirect_uri that the client specified |
| 1125 | http: httplib2.Http, optional http instance to use to do the fetch |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1126 | cache: An optional cache service client that implements get() and set() |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 1127 | methods. See clientsecrets.loadfile() for details. |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1128 | |
| 1129 | Returns: |
| 1130 | An OAuth2Credentials object. |
| 1131 | |
| 1132 | Raises: |
| 1133 | FlowExchangeError if the authorization code cannot be exchanged for an |
| 1134 | access token |
| 1135 | UnknownClientSecretsFlowError if the file describes an unknown kind of Flow. |
| 1136 | clientsecrets.InvalidClientSecretsError if the clientsecrets file is |
| 1137 | invalid. |
| 1138 | """ |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1139 | flow = flow_from_clientsecrets(filename, scope, message=message, cache=cache, |
| 1140 | redirect_uri=redirect_uri) |
| 1141 | credentials = flow.step2_exchange(code, http=http) |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 1142 | return credentials |
| 1143 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 1144 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1145 | class OAuth2WebServerFlow(Flow): |
| 1146 | """Does the Web Server Flow for OAuth 2.0. |
| 1147 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1148 | OAuth2WebServerFlow objects may be safely pickled and unpickled. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1149 | """ |
| 1150 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1151 | @util.positional(4) |
| 1152 | def __init__(self, client_id, client_secret, scope, |
| 1153 | redirect_uri=None, |
| 1154 | user_agent=None, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1155 | auth_uri=GOOGLE_AUTH_URI, |
| 1156 | token_uri=GOOGLE_TOKEN_URI, |
| 1157 | revoke_uri=GOOGLE_REVOKE_URI, |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 1158 | **kwargs): |
| 1159 | """Constructor for OAuth2WebServerFlow. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1160 | |
Joe Gregorio | 32f7319 | 2012-10-23 16:13:44 -0400 | [diff] [blame] | 1161 | The kwargs argument is used to set extra query parameters on the |
| 1162 | auth_uri. For example, the access_type and approval_prompt |
| 1163 | query parameters can be set via kwargs. |
| 1164 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1165 | Args: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 1166 | client_id: string, client identifier. |
| 1167 | client_secret: string client secret. |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 1168 | scope: string or iterable of strings, scope(s) of the credentials being |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 1169 | requested. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1170 | redirect_uri: string, Either the string 'urn:ietf:wg:oauth:2.0:oob' for |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1171 | a non-web-based application, or a URI that handles the callback from |
| 1172 | the authorization server. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1173 | user_agent: string, HTTP User-Agent to provide for this application. |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 1174 | auth_uri: string, URI for authorization endpoint. For convenience |
| 1175 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
| 1176 | token_uri: string, URI for token endpoint. For convenience |
| 1177 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1178 | revoke_uri: string, URI for revoke endpoint. For convenience |
| 1179 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1180 | **kwargs: dict, The keyword arguments are all optional and required |
| 1181 | parameters for the OAuth calls. |
| 1182 | """ |
| 1183 | self.client_id = client_id |
| 1184 | self.client_secret = client_secret |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 1185 | self.scope = util.scopes_to_string(scope) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1186 | self.redirect_uri = redirect_uri |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1187 | self.user_agent = user_agent |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 1188 | self.auth_uri = auth_uri |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1189 | self.token_uri = token_uri |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1190 | self.revoke_uri = revoke_uri |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 1191 | self.params = { |
| 1192 | 'access_type': 'offline', |
Joe Gregorio | 32f7319 | 2012-10-23 16:13:44 -0400 | [diff] [blame] | 1193 | 'response_type': 'code', |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1194 | } |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 1195 | self.params.update(kwargs) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1196 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1197 | @util.positional(1) |
| 1198 | def step1_get_authorize_url(self, redirect_uri=None): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1199 | """Returns a URI to redirect to the provider. |
| 1200 | |
| 1201 | Args: |
Joe Gregorio | f2326c0 | 2012-02-09 12:18:44 -0500 | [diff] [blame] | 1202 | redirect_uri: string, Either the string 'urn:ietf:wg:oauth:2.0:oob' for |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1203 | a non-web-based application, or a URI that handles the callback from |
| 1204 | the authorization server. This parameter is deprecated, please move to |
| 1205 | passing the redirect_uri in via the constructor. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1206 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1207 | Returns: |
| 1208 | A URI as a string to redirect the user to begin the authorization flow. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1209 | """ |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1210 | if redirect_uri is not None: |
| 1211 | logger.warning(('The redirect_uri parameter for' |
| 1212 | 'OAuth2WebServerFlow.step1_get_authorize_url is deprecated. Please' |
| 1213 | 'move to passing the redirect_uri in via the constructor.')) |
| 1214 | self.redirect_uri = redirect_uri |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1215 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1216 | if self.redirect_uri is None: |
| 1217 | raise ValueError('The value of redirect_uri must not be None.') |
| 1218 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1219 | query_params = { |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 1220 | 'client_id': self.client_id, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1221 | 'redirect_uri': self.redirect_uri, |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 1222 | 'scope': self.scope, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1223 | } |
| 1224 | query_params.update(self.params) |
| 1225 | return _update_query_params(self.auth_uri, query_params) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1226 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1227 | @util.positional(2) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1228 | def step2_exchange(self, code, http=None): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1229 | """Exhanges a code for OAuth2Credentials. |
| 1230 | |
| 1231 | Args: |
| 1232 | code: string or dict, either the code as a string, or a dictionary |
| 1233 | of the query parameters to the redirect_uri, which contains |
| 1234 | the code. |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1235 | http: httplib2.Http, optional http instance to use to do the fetch |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 1236 | |
| 1237 | Returns: |
| 1238 | An OAuth2Credentials object that can be used to authorize requests. |
| 1239 | |
| 1240 | Raises: |
| 1241 | FlowExchangeError if a problem occured exchanging the code for a |
| 1242 | refresh_token. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1243 | """ |
| 1244 | |
| 1245 | if not (isinstance(code, str) or isinstance(code, unicode)): |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 1246 | if 'code' not in code: |
| 1247 | if 'error' in code: |
| 1248 | error_msg = code['error'] |
| 1249 | else: |
| 1250 | error_msg = 'No code was supplied in the query parameters.' |
| 1251 | raise FlowExchangeError(error_msg) |
| 1252 | else: |
| 1253 | code = code['code'] |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1254 | |
| 1255 | body = urllib.urlencode({ |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 1256 | 'grant_type': 'authorization_code', |
| 1257 | 'client_id': self.client_id, |
| 1258 | 'client_secret': self.client_secret, |
| 1259 | 'code': code, |
| 1260 | 'redirect_uri': self.redirect_uri, |
| 1261 | 'scope': self.scope, |
| 1262 | }) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1263 | headers = { |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 1264 | 'content-type': 'application/x-www-form-urlencoded', |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1265 | } |
JacobMoshenko | cb6d891 | 2011-07-08 13:35:15 -0400 | [diff] [blame] | 1266 | |
| 1267 | if self.user_agent is not None: |
| 1268 | headers['user-agent'] = self.user_agent |
| 1269 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1270 | if http is None: |
| 1271 | http = httplib2.Http() |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 1272 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 1273 | resp, content = http.request(self.token_uri, method='POST', body=body, |
| 1274 | headers=headers) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 1275 | d = _parse_exchange_token_response(content) |
| 1276 | if resp.status == 200 and 'access_token' in d: |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1277 | access_token = d['access_token'] |
| 1278 | refresh_token = d.get('refresh_token', None) |
| 1279 | token_expiry = None |
| 1280 | if 'expires_in' in d: |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 1281 | token_expiry = datetime.datetime.utcnow() + datetime.timedelta( |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 1282 | seconds=int(d['expires_in'])) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1283 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 1284 | if 'id_token' in d: |
| 1285 | d['id_token'] = _extract_id_token(d['id_token']) |
| 1286 | |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 1287 | logger.info('Successfully retrieved access token') |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 1288 | return OAuth2Credentials(access_token, self.client_id, |
| 1289 | self.client_secret, refresh_token, token_expiry, |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 1290 | self.token_uri, self.user_agent, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1291 | revoke_uri=self.revoke_uri, |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 1292 | id_token=d.get('id_token', None)) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1293 | else: |
Joe Gregorio | e78621a | 2012-03-09 15:47:23 -0500 | [diff] [blame] | 1294 | logger.info('Failed to retrieve access token: %s' % content) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 1295 | if 'error' in d: |
| 1296 | # you never know what those providers got to say |
| 1297 | error_msg = unicode(d['error']) |
| 1298 | else: |
| 1299 | error_msg = 'Invalid response: %s.' % str(resp.status) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1300 | raise FlowExchangeError(error_msg) |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 1301 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1302 | |
| 1303 | @util.positional(2) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1304 | def flow_from_clientsecrets(filename, scope, redirect_uri=None, |
| 1305 | message=None, cache=None): |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 1306 | """Create a Flow from a clientsecrets file. |
| 1307 | |
| 1308 | Will create the right kind of Flow based on the contents of the clientsecrets |
| 1309 | file or will raise InvalidClientSecretsError for unknown types of Flows. |
| 1310 | |
| 1311 | Args: |
| 1312 | filename: string, File name of client secrets. |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 1313 | scope: string or iterable of strings, scope(s) to request. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1314 | redirect_uri: string, Either the string 'urn:ietf:wg:oauth:2.0:oob' for |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1315 | a non-web-based application, or a URI that handles the callback from |
| 1316 | the authorization server. |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 1317 | message: string, A friendly string to display to the user if the |
| 1318 | clientsecrets file is missing or invalid. If message is provided then |
| 1319 | sys.exit will be called in the case of an error. If message in not |
| 1320 | provided then clientsecrets.InvalidClientSecretsError will be raised. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1321 | cache: An optional cache service client that implements get() and set() |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 1322 | methods. See clientsecrets.loadfile() for details. |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 1323 | |
| 1324 | Returns: |
| 1325 | A Flow object. |
| 1326 | |
| 1327 | Raises: |
| 1328 | UnknownClientSecretsFlowError if the file describes an unknown kind of Flow. |
| 1329 | clientsecrets.InvalidClientSecretsError if the clientsecrets file is |
| 1330 | invalid. |
| 1331 | """ |
Joe Gregorio | 0984ef2 | 2011-10-14 13:17:43 -0400 | [diff] [blame] | 1332 | try: |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 1333 | client_type, client_info = clientsecrets.loadfile(filename, cache=cache) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1334 | if client_type in (clientsecrets.TYPE_WEB, clientsecrets.TYPE_INSTALLED): |
| 1335 | constructor_kwargs = { |
| 1336 | 'redirect_uri': redirect_uri, |
| 1337 | 'auth_uri': client_info['auth_uri'], |
| 1338 | 'token_uri': client_info['token_uri'], |
| 1339 | } |
| 1340 | revoke_uri = client_info.get('revoke_uri') |
| 1341 | if revoke_uri is not None: |
| 1342 | constructor_kwargs['revoke_uri'] = revoke_uri |
| 1343 | return OAuth2WebServerFlow( |
| 1344 | client_info['client_id'], client_info['client_secret'], |
| 1345 | scope, **constructor_kwargs) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 1346 | |
Joe Gregorio | 0984ef2 | 2011-10-14 13:17:43 -0400 | [diff] [blame] | 1347 | except clientsecrets.InvalidClientSecretsError: |
| 1348 | if message: |
| 1349 | sys.exit(message) |
| 1350 | else: |
| 1351 | raise |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 1352 | else: |
| 1353 | raise UnknownClientSecretsFlowError( |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 1354 | 'This OAuth 2.0 flow is unsupported: %r' % client_type) |