blob: 165c9b197e5cfdce22f121489deaa81bca8b7ca9 [file] [log] [blame]
Joe Gregorio695fdc12011-01-16 16:46:55 -05001# 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.
14
15"""Utilities for Google App Engine
16
Joe Gregorio7c22ab22011-02-16 15:32:39 -050017Utilities for making it easier to use OAuth 2.0 on Google App Engine.
Joe Gregorio695fdc12011-01-16 16:46:55 -050018"""
19
20__author__ = 'jcgregorio@google.com (Joe Gregorio)'
21
Joe Gregorio1daa71b2011-09-15 18:12:14 -040022import base64
Joe Gregorio77254c12012-08-27 14:13:22 -040023import cgi
Joe Gregorio432f17e2011-05-22 23:18:00 -040024import httplib2
Joe Gregorio1daa71b2011-09-15 18:12:14 -040025import logging
Joe Gregorio6ceea2d2012-08-24 11:57:58 -040026import os
Joe Gregorio695fdc12011-01-16 16:46:55 -050027import pickle
JacobMoshenko8e905102011-06-20 09:53:10 -040028import time
JacobMoshenko8e905102011-06-20 09:53:10 -040029
Joe Gregoriod84d6b82012-02-28 14:53:00 -050030from google.appengine.api import app_identity
Joe Gregorio6ceea2d2012-08-24 11:57:58 -040031from google.appengine.api import memcache
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040032from google.appengine.api import users
Joe Gregorio432f17e2011-05-22 23:18:00 -040033from google.appengine.ext import db
34from google.appengine.ext import webapp
35from google.appengine.ext.webapp.util import login_required
36from google.appengine.ext.webapp.util import run_wsgi_app
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -080037from oauth2client import GOOGLE_AUTH_URI
38from oauth2client import GOOGLE_REVOKE_URI
39from oauth2client import GOOGLE_TOKEN_URI
Joe Gregorio6ceea2d2012-08-24 11:57:58 -040040from oauth2client import clientsecrets
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040041from oauth2client import util
Joe Gregorio6ceea2d2012-08-24 11:57:58 -040042from oauth2client import xsrfutil
Joe Gregorio68a8cfe2012-08-03 16:17:40 -040043from oauth2client.anyjson import simplejson
44from oauth2client.client import AccessTokenRefreshError
45from oauth2client.client import AssertionCredentials
46from oauth2client.client import Credentials
47from oauth2client.client import Flow
48from oauth2client.client import OAuth2WebServerFlow
49from oauth2client.client import Storage
Joe Gregorioa19f3a72012-07-11 15:35:35 -040050
Joe Gregorio78787b62013-02-08 15:36:21 -050051# TODO(dhermes): Resolve import issue.
52# This is a temporary fix for a Google internal issue.
53try:
54 from google.appengine.ext import ndb
55except ImportError:
56 ndb = None
57
Joe Gregorioa19f3a72012-07-11 15:35:35 -040058logger = logging.getLogger(__name__)
59
Joe Gregorio432f17e2011-05-22 23:18:00 -040060OAUTH2CLIENT_NAMESPACE = 'oauth2client#ns'
Joe Gregorio695fdc12011-01-16 16:46:55 -050061
Joe Gregorio6ceea2d2012-08-24 11:57:58 -040062XSRF_MEMCACHE_ID = 'xsrf_secret_key'
63
JacobMoshenko8e905102011-06-20 09:53:10 -040064
Joe Gregorio77254c12012-08-27 14:13:22 -040065def _safe_html(s):
66 """Escape text to make it safe to display.
67
68 Args:
69 s: string, The text to escape.
70
71 Returns:
72 The escaped text as a string.
73 """
74 return cgi.escape(s, quote=1).replace("'", ''')
75
76
Joe Gregoriof08a4982011-10-07 13:11:16 -040077class InvalidClientSecretsError(Exception):
78 """The client_secrets.json file is malformed or missing required fields."""
Joe Gregorio6ceea2d2012-08-24 11:57:58 -040079
80
81class InvalidXsrfTokenError(Exception):
82 """The XSRF token is invalid or expired."""
83
84
85class SiteXsrfSecretKey(db.Model):
86 """Storage for the sites XSRF secret key.
87
88 There will only be one instance stored of this model, the one used for the
dhermes@google.com47154822012-11-26 10:44:09 -080089 site.
90 """
Joe Gregorio6ceea2d2012-08-24 11:57:58 -040091 secret = db.StringProperty()
92
Joe Gregorio78787b62013-02-08 15:36:21 -050093if ndb is not None:
94 class SiteXsrfSecretKeyNDB(ndb.Model):
95 """NDB Model for storage for the sites XSRF secret key.
Joe Gregorio6ceea2d2012-08-24 11:57:58 -040096
Joe Gregorio78787b62013-02-08 15:36:21 -050097 Since this model uses the same kind as SiteXsrfSecretKey, it can be used
98 interchangeably. This simply provides an NDB model for interacting with the
99 same data the DB model interacts with.
dhermes@google.com47154822012-11-26 10:44:09 -0800100
Joe Gregorio78787b62013-02-08 15:36:21 -0500101 There should only be one instance stored of this model, the one used for the
102 site.
103 """
104 secret = ndb.StringProperty()
dhermes@google.com47154822012-11-26 10:44:09 -0800105
Joe Gregorio78787b62013-02-08 15:36:21 -0500106 @classmethod
107 def _get_kind(cls):
108 """Return the kind name for this class."""
109 return 'SiteXsrfSecretKey'
dhermes@google.com47154822012-11-26 10:44:09 -0800110
111
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400112def _generate_new_xsrf_secret_key():
113 """Returns a random XSRF secret key.
114 """
115 return os.urandom(16).encode("hex")
116
117
118def xsrf_secret_key():
119 """Return the secret key for use for XSRF protection.
120
121 If the Site entity does not have a secret key, this method will also create
122 one and persist it.
123
124 Returns:
125 The secret key.
126 """
127 secret = memcache.get(XSRF_MEMCACHE_ID, namespace=OAUTH2CLIENT_NAMESPACE)
128 if not secret:
129 # Load the one and only instance of SiteXsrfSecretKey.
130 model = SiteXsrfSecretKey.get_or_insert(key_name='site')
131 if not model.secret:
132 model.secret = _generate_new_xsrf_secret_key()
133 model.put()
134 secret = model.secret
135 memcache.add(XSRF_MEMCACHE_ID, secret, namespace=OAUTH2CLIENT_NAMESPACE)
136
137 return str(secret)
Joe Gregoriof08a4982011-10-07 13:11:16 -0400138
139
JacobMoshenko8e905102011-06-20 09:53:10 -0400140class AppAssertionCredentials(AssertionCredentials):
141 """Credentials object for App Engine Assertion Grants
142
143 This object will allow an App Engine application to identify itself to Google
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400144 and other OAuth 2.0 servers that can verify assertions. It can be used for the
145 purpose of accessing data stored under an account assigned to the App Engine
146 application itself.
JacobMoshenko8e905102011-06-20 09:53:10 -0400147
148 This credential does not require a flow to instantiate because it represents
149 a two legged flow, and therefore has all of the required information to
150 generate and refresh its own access tokens.
JacobMoshenko8e905102011-06-20 09:53:10 -0400151 """
152
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400153 @util.positional(2)
Joe Gregoriod84d6b82012-02-28 14:53:00 -0500154 def __init__(self, scope, **kwargs):
JacobMoshenko8e905102011-06-20 09:53:10 -0400155 """Constructor for AppAssertionCredentials
156
157 Args:
Joe Gregorio5cf5d122012-11-16 16:36:12 -0500158 scope: string or iterable of strings, scope(s) of the credentials being
Joe Gregoriofd08e432012-08-09 14:17:41 -0400159 requested.
JacobMoshenko8e905102011-06-20 09:53:10 -0400160 """
Joe Gregorio5cf5d122012-11-16 16:36:12 -0500161 self.scope = util.scopes_to_string(scope)
JacobMoshenko8e905102011-06-20 09:53:10 -0400162
163 super(AppAssertionCredentials, self).__init__(
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400164 'ignored' # assertion_type is ignore in this subclass.
165 )
JacobMoshenko8e905102011-06-20 09:53:10 -0400166
Joe Gregorio562b7312011-09-15 09:06:38 -0400167 @classmethod
168 def from_json(cls, json):
169 data = simplejson.loads(json)
Joe Gregoriod84d6b82012-02-28 14:53:00 -0500170 return AppAssertionCredentials(data['scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400171
Joe Gregoriod84d6b82012-02-28 14:53:00 -0500172 def _refresh(self, http_request):
173 """Refreshes the access_token.
JacobMoshenko8e905102011-06-20 09:53:10 -0400174
Joe Gregoriod84d6b82012-02-28 14:53:00 -0500175 Since the underlying App Engine app_identity implementation does its own
176 caching we can skip all the storage hoops and just to a refresh using the
177 API.
JacobMoshenko8e905102011-06-20 09:53:10 -0400178
Joe Gregoriod84d6b82012-02-28 14:53:00 -0500179 Args:
180 http_request: callable, a callable that matches the method signature of
181 httplib2.Http.request, used to make the refresh request.
JacobMoshenko8e905102011-06-20 09:53:10 -0400182
Joe Gregoriod84d6b82012-02-28 14:53:00 -0500183 Raises:
184 AccessTokenRefreshError: When the refresh fails.
185 """
186 try:
Joe Gregorio5cf5d122012-11-16 16:36:12 -0500187 scopes = self.scope.split()
188 (token, _) = app_identity.get_access_token(scopes)
Joe Gregoriod84d6b82012-02-28 14:53:00 -0500189 except app_identity.Error, e:
190 raise AccessTokenRefreshError(str(e))
191 self.access_token = token
JacobMoshenko8e905102011-06-20 09:53:10 -0400192
193
Joe Gregorio695fdc12011-01-16 16:46:55 -0500194class FlowProperty(db.Property):
Joe Gregorio7c22ab22011-02-16 15:32:39 -0500195 """App Engine datastore Property for Flow.
196
dhermes@google.com47154822012-11-26 10:44:09 -0800197 Utility property that allows easy storage and retrieval of an
Joe Gregorio695fdc12011-01-16 16:46:55 -0500198 oauth2client.Flow"""
199
200 # Tell what the user type is.
201 data_type = Flow
202
203 # For writing to datastore.
204 def get_value_for_datastore(self, model_instance):
205 flow = super(FlowProperty,
206 self).get_value_for_datastore(model_instance)
207 return db.Blob(pickle.dumps(flow))
208
209 # For reading from datastore.
210 def make_value_from_datastore(self, value):
211 if value is None:
212 return None
213 return pickle.loads(value)
214
215 def validate(self, value):
216 if value is not None and not isinstance(value, Flow):
Joe Gregorio1daa71b2011-09-15 18:12:14 -0400217 raise db.BadValueError('Property %s must be convertible '
Joe Gregorio695fdc12011-01-16 16:46:55 -0500218 'to a FlowThreeLegged instance (%s)' %
219 (self.name, value))
220 return super(FlowProperty, self).validate(value)
221
222 def empty(self, value):
223 return not value
224
225
Joe Gregorio78787b62013-02-08 15:36:21 -0500226if ndb is not None:
227 class FlowNDBProperty(ndb.PickleProperty):
228 """App Engine NDB datastore Property for Flow.
dhermes@google.com47154822012-11-26 10:44:09 -0800229
Joe Gregorio78787b62013-02-08 15:36:21 -0500230 Serves the same purpose as the DB FlowProperty, but for NDB models. Since
231 PickleProperty inherits from BlobProperty, the underlying representation of
232 the data in the datastore will be the same as in the DB case.
dhermes@google.com47154822012-11-26 10:44:09 -0800233
Joe Gregorio78787b62013-02-08 15:36:21 -0500234 Utility property that allows easy storage and retrieval of an
235 oauth2client.Flow
dhermes@google.com47154822012-11-26 10:44:09 -0800236 """
Joe Gregorio78787b62013-02-08 15:36:21 -0500237
238 def _validate(self, value):
239 """Validates a value as a proper Flow object.
240
241 Args:
242 value: A value to be set on the property.
243
244 Raises:
245 TypeError if the value is not an instance of Flow.
246 """
247 logger.info('validate: Got type %s', type(value))
248 if value is not None and not isinstance(value, Flow):
249 raise TypeError('Property %s must be convertible to a flow '
250 'instance; received: %s.' % (self._name, value))
dhermes@google.com47154822012-11-26 10:44:09 -0800251
252
Joe Gregorio695fdc12011-01-16 16:46:55 -0500253class CredentialsProperty(db.Property):
Joe Gregorio7c22ab22011-02-16 15:32:39 -0500254 """App Engine datastore Property for Credentials.
255
256 Utility property that allows easy storage and retrieval of
Joe Gregorio695fdc12011-01-16 16:46:55 -0500257 oath2client.Credentials
258 """
259
260 # Tell what the user type is.
261 data_type = Credentials
262
263 # For writing to datastore.
264 def get_value_for_datastore(self, model_instance):
Joe Gregorioa19f3a72012-07-11 15:35:35 -0400265 logger.info("get: Got type " + str(type(model_instance)))
Joe Gregorio695fdc12011-01-16 16:46:55 -0500266 cred = super(CredentialsProperty,
267 self).get_value_for_datastore(model_instance)
Joe Gregorio562b7312011-09-15 09:06:38 -0400268 if cred is None:
269 cred = ''
270 else:
271 cred = cred.to_json()
272 return db.Blob(cred)
Joe Gregorio695fdc12011-01-16 16:46:55 -0500273
274 # For reading from datastore.
275 def make_value_from_datastore(self, value):
Joe Gregorioa19f3a72012-07-11 15:35:35 -0400276 logger.info("make: Got type " + str(type(value)))
Joe Gregorio695fdc12011-01-16 16:46:55 -0500277 if value is None:
278 return None
Joe Gregorio562b7312011-09-15 09:06:38 -0400279 if len(value) == 0:
280 return None
Joe Gregorio562b7312011-09-15 09:06:38 -0400281 try:
282 credentials = Credentials.new_from_json(value)
283 except ValueError:
Joe Gregorioec555842011-10-27 11:10:39 -0400284 credentials = None
Joe Gregorio562b7312011-09-15 09:06:38 -0400285 return credentials
Joe Gregorio695fdc12011-01-16 16:46:55 -0500286
287 def validate(self, value):
Joe Gregorio1daa71b2011-09-15 18:12:14 -0400288 value = super(CredentialsProperty, self).validate(value)
Joe Gregorioa19f3a72012-07-11 15:35:35 -0400289 logger.info("validate: Got type " + str(type(value)))
Joe Gregorio695fdc12011-01-16 16:46:55 -0500290 if value is not None and not isinstance(value, Credentials):
Joe Gregorio562b7312011-09-15 09:06:38 -0400291 raise db.BadValueError('Property %s must be convertible '
Joe Gregorio1daa71b2011-09-15 18:12:14 -0400292 'to a Credentials instance (%s)' %
293 (self.name, value))
294 #if value is not None and not isinstance(value, Credentials):
295 # return None
296 return value
Joe Gregorio695fdc12011-01-16 16:46:55 -0500297
298
Joe Gregorio78787b62013-02-08 15:36:21 -0500299if ndb is not None:
300 # TODO(dhermes): Turn this into a JsonProperty and overhaul the Credentials
301 # and subclass mechanics to use new_from_dict, to_dict,
302 # from_dict, etc.
303 class CredentialsNDBProperty(ndb.BlobProperty):
304 """App Engine NDB datastore Property for Credentials.
Joe Gregorio695fdc12011-01-16 16:46:55 -0500305
Joe Gregorio78787b62013-02-08 15:36:21 -0500306 Serves the same purpose as the DB CredentialsProperty, but for NDB models.
307 Since CredentialsProperty stores data as a blob and this inherits from
308 BlobProperty, the data in the datastore will be the same as in the DB case.
dhermes@google.com47154822012-11-26 10:44:09 -0800309
Joe Gregorio78787b62013-02-08 15:36:21 -0500310 Utility property that allows easy storage and retrieval of Credentials and
311 subclasses.
dhermes@google.com47154822012-11-26 10:44:09 -0800312 """
Joe Gregorio78787b62013-02-08 15:36:21 -0500313 def _validate(self, value):
314 """Validates a value as a proper credentials object.
dhermes@google.com47154822012-11-26 10:44:09 -0800315
Joe Gregorio78787b62013-02-08 15:36:21 -0500316 Args:
317 value: A value to be set on the property.
dhermes@google.com47154822012-11-26 10:44:09 -0800318
Joe Gregorio78787b62013-02-08 15:36:21 -0500319 Raises:
320 TypeError if the value is not an instance of Credentials.
321 """
322 logger.info('validate: Got type %s', type(value))
323 if value is not None and not isinstance(value, Credentials):
324 raise TypeError('Property %s must be convertible to a credentials '
325 'instance; received: %s.' % (self._name, value))
dhermes@google.com47154822012-11-26 10:44:09 -0800326
Joe Gregorio78787b62013-02-08 15:36:21 -0500327 def _to_base_type(self, value):
328 """Converts our validated value to a JSON serialized string.
dhermes@google.com47154822012-11-26 10:44:09 -0800329
Joe Gregorio78787b62013-02-08 15:36:21 -0500330 Args:
331 value: A value to be set in the datastore.
dhermes@google.com47154822012-11-26 10:44:09 -0800332
Joe Gregorio78787b62013-02-08 15:36:21 -0500333 Returns:
334 A JSON serialized version of the credential, else '' if value is None.
335 """
336 if value is None:
337 return ''
338 else:
339 return value.to_json()
dhermes@google.com47154822012-11-26 10:44:09 -0800340
Joe Gregorio78787b62013-02-08 15:36:21 -0500341 def _from_base_type(self, value):
342 """Converts our stored JSON string back to the desired type.
343
344 Args:
345 value: A value from the datastore to be converted to the desired type.
346
347 Returns:
348 A deserialized Credentials (or subclass) object, else None if the
349 value can't be parsed.
350 """
351 if not value:
352 return None
353 try:
354 # Uses the from_json method of the implied class of value
355 credentials = Credentials.new_from_json(value)
356 except ValueError:
357 credentials = None
358 return credentials
dhermes@google.com47154822012-11-26 10:44:09 -0800359
360
361class StorageByKeyName(Storage):
362 """Store and retrieve a credential to and from the App Engine datastore.
363
364 This Storage helper presumes the Credentials have been stored as a
365 CredentialsProperty or CredentialsNDBProperty on a datastore model class, and
366 that entities are stored by key_name.
Joe Gregorio695fdc12011-01-16 16:46:55 -0500367 """
368
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400369 @util.positional(4)
Joe Gregorio432f17e2011-05-22 23:18:00 -0400370 def __init__(self, model, key_name, property_name, cache=None):
Joe Gregorio695fdc12011-01-16 16:46:55 -0500371 """Constructor for Storage.
372
373 Args:
dhermes@google.com47154822012-11-26 10:44:09 -0800374 model: db.Model or ndb.Model, model class
Joe Gregorio695fdc12011-01-16 16:46:55 -0500375 key_name: string, key name for the entity that has the credentials
JacobMoshenko8e905102011-06-20 09:53:10 -0400376 property_name: string, name of the property that is a CredentialsProperty
dhermes@google.com47154822012-11-26 10:44:09 -0800377 or CredentialsNDBProperty.
378 cache: memcache, a write-through cache to put in front of the datastore.
379 If the model you are using is an NDB model, using a cache will be
380 redundant since the model uses an instance cache and memcache for you.
Joe Gregorio695fdc12011-01-16 16:46:55 -0500381 """
Joe Gregorio7c22ab22011-02-16 15:32:39 -0500382 self._model = model
383 self._key_name = key_name
384 self._property_name = property_name
Joe Gregorio432f17e2011-05-22 23:18:00 -0400385 self._cache = cache
Joe Gregorio695fdc12011-01-16 16:46:55 -0500386
dhermes@google.com47154822012-11-26 10:44:09 -0800387 def _is_ndb(self):
388 """Determine whether the model of the instance is an NDB model.
389
390 Returns:
391 Boolean indicating whether or not the model is an NDB or DB model.
392 """
393 # issubclass will fail if one of the arguments is not a class, only need
394 # worry about new-style classes since ndb and db models are new-style
395 if isinstance(self._model, type):
Joe Gregorio78787b62013-02-08 15:36:21 -0500396 if ndb is not None and issubclass(self._model, ndb.Model):
dhermes@google.com47154822012-11-26 10:44:09 -0800397 return True
398 elif issubclass(self._model, db.Model):
399 return False
400
401 raise TypeError('Model class not an NDB or DB model: %s.' % (self._model,))
402
403 def _get_entity(self):
404 """Retrieve entity from datastore.
405
406 Uses a different model method for db or ndb models.
407
408 Returns:
409 Instance of the model corresponding to the current storage object
410 and stored using the key name of the storage object.
411 """
412 if self._is_ndb():
413 return self._model.get_by_id(self._key_name)
414 else:
415 return self._model.get_by_key_name(self._key_name)
416
417 def _delete_entity(self):
418 """Delete entity from datastore.
419
420 Attempts to delete using the key_name stored on the object, whether or not
421 the given key is in the datastore.
422 """
423 if self._is_ndb():
424 ndb.Key(self._model, self._key_name).delete()
425 else:
426 entity_key = db.Key.from_path(self._model.kind(), self._key_name)
427 db.delete(entity_key)
428
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400429 def locked_get(self):
Joe Gregorio695fdc12011-01-16 16:46:55 -0500430 """Retrieve Credential from datastore.
431
432 Returns:
433 oauth2client.Credentials
434 """
Joe Gregorio432f17e2011-05-22 23:18:00 -0400435 if self._cache:
Joe Gregorio562b7312011-09-15 09:06:38 -0400436 json = self._cache.get(self._key_name)
437 if json:
438 return Credentials.new_from_json(json)
Joe Gregorio9fa077c2011-11-18 08:16:52 -0500439
dhermes@google.com47154822012-11-26 10:44:09 -0800440 credentials = None
441 entity = self._get_entity()
Joe Gregorio9fa077c2011-11-18 08:16:52 -0500442 if entity is not None:
dhermes@google.com47154822012-11-26 10:44:09 -0800443 credentials = getattr(entity, self._property_name)
444 if credentials and hasattr(credentials, 'set_store'):
445 credentials.set_store(self)
Joe Gregorio9fa077c2011-11-18 08:16:52 -0500446 if self._cache:
dhermes@google.com47154822012-11-26 10:44:09 -0800447 self._cache.set(self._key_name, credentials.to_json())
Joe Gregorio432f17e2011-05-22 23:18:00 -0400448
dhermes@google.com47154822012-11-26 10:44:09 -0800449 return credentials
Joe Gregorio695fdc12011-01-16 16:46:55 -0500450
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400451 def locked_put(self, credentials):
Joe Gregorio695fdc12011-01-16 16:46:55 -0500452 """Write a Credentials to the datastore.
453
454 Args:
455 credentials: Credentials, the credentials to store.
456 """
Joe Gregorio7c22ab22011-02-16 15:32:39 -0500457 entity = self._model.get_or_insert(self._key_name)
458 setattr(entity, self._property_name, credentials)
Joe Gregorio695fdc12011-01-16 16:46:55 -0500459 entity.put()
Joe Gregorio432f17e2011-05-22 23:18:00 -0400460 if self._cache:
Joe Gregorio562b7312011-09-15 09:06:38 -0400461 self._cache.set(self._key_name, credentials.to_json())
Joe Gregorio432f17e2011-05-22 23:18:00 -0400462
Joe Gregorioec75dc12012-02-06 13:40:42 -0500463 def locked_delete(self):
464 """Delete Credential from datastore."""
465
466 if self._cache:
467 self._cache.delete(self._key_name)
468
dhermes@google.com47154822012-11-26 10:44:09 -0800469 self._delete_entity()
Joe Gregorioec75dc12012-02-06 13:40:42 -0500470
Joe Gregorio432f17e2011-05-22 23:18:00 -0400471
472class CredentialsModel(db.Model):
473 """Storage for OAuth 2.0 Credentials
474
475 Storage of the model is keyed by the user.user_id().
476 """
477 credentials = CredentialsProperty()
478
479
Joe Gregorio78787b62013-02-08 15:36:21 -0500480if ndb is not None:
481 class CredentialsNDBModel(ndb.Model):
482 """NDB Model for storage of OAuth 2.0 Credentials
dhermes@google.com47154822012-11-26 10:44:09 -0800483
Joe Gregorio78787b62013-02-08 15:36:21 -0500484 Since this model uses the same kind as CredentialsModel and has a property
485 which can serialize and deserialize Credentials correctly, it can be used
486 interchangeably with a CredentialsModel to access, insert and delete the
487 same entities. This simply provides an NDB model for interacting with the
488 same data the DB model interacts with.
dhermes@google.com47154822012-11-26 10:44:09 -0800489
Joe Gregorio78787b62013-02-08 15:36:21 -0500490 Storage of the model is keyed by the user.user_id().
491 """
492 credentials = CredentialsNDBProperty()
dhermes@google.com47154822012-11-26 10:44:09 -0800493
Joe Gregorio78787b62013-02-08 15:36:21 -0500494 @classmethod
495 def _get_kind(cls):
496 """Return the kind name for this class."""
497 return 'CredentialsModel'
dhermes@google.com47154822012-11-26 10:44:09 -0800498
499
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400500def _build_state_value(request_handler, user):
501 """Composes the value for the 'state' parameter.
502
503 Packs the current request URI and an XSRF token into an opaque string that
504 can be passed to the authentication server via the 'state' parameter.
505
506 Args:
507 request_handler: webapp.RequestHandler, The request.
508 user: google.appengine.api.users.User, The current user.
509
510 Returns:
511 The state value as a string.
512 """
513 uri = request_handler.request.url
514 token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
515 action_id=str(uri))
516 return uri + ':' + token
517
518
519def _parse_state_value(state, user):
520 """Parse the value of the 'state' parameter.
521
522 Parses the value and validates the XSRF token in the state parameter.
523
524 Args:
525 state: string, The value of the state parameter.
526 user: google.appengine.api.users.User, The current user.
527
528 Raises:
529 InvalidXsrfTokenError: if the XSRF token is invalid.
530
531 Returns:
532 The redirect URI.
533 """
534 uri, token = state.rsplit(':', 1)
535 if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
536 action_id=uri):
537 raise InvalidXsrfTokenError()
538
539 return uri
540
541
Joe Gregorio432f17e2011-05-22 23:18:00 -0400542class OAuth2Decorator(object):
543 """Utility for making OAuth 2.0 easier.
544
545 Instantiate and then use with oauth_required or oauth_aware
546 as decorators on webapp.RequestHandler methods.
547
548 Example:
549
550 decorator = OAuth2Decorator(
551 client_id='837...ent.com',
552 client_secret='Qh...wwI',
Joe Gregorioc4fc0952011-11-09 12:21:11 -0500553 scope='https://www.googleapis.com/auth/plus')
Joe Gregorio432f17e2011-05-22 23:18:00 -0400554
555
556 class MainHandler(webapp.RequestHandler):
557
558 @decorator.oauth_required
559 def get(self):
560 http = decorator.http()
561 # http is authorized with the user's Credentials and can be used
562 # in API calls
563
564 """
JacobMoshenko8e905102011-06-20 09:53:10 -0400565
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400566 @util.positional(4)
JacobMoshenkocb6d8912011-07-08 13:35:15 -0400567 def __init__(self, client_id, client_secret, scope,
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -0800568 auth_uri=GOOGLE_AUTH_URI,
569 token_uri=GOOGLE_TOKEN_URI,
570 revoke_uri=GOOGLE_REVOKE_URI,
Johan Euphrosineacf517f2012-02-13 21:08:33 +0100571 user_agent=None,
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400572 message=None,
573 callback_path='/oauth2callback',
574 **kwargs):
Joe Gregorio432f17e2011-05-22 23:18:00 -0400575
576 """Constructor for OAuth2Decorator
577
578 Args:
579 client_id: string, client identifier.
580 client_secret: string client secret.
Joe Gregorio5cf5d122012-11-16 16:36:12 -0500581 scope: string or iterable of strings, scope(s) of the credentials being
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400582 requested.
Joe Gregorio432f17e2011-05-22 23:18:00 -0400583 auth_uri: string, URI for authorization endpoint. For convenience
584 defaults to Google's endpoints but any OAuth 2.0 provider can be used.
585 token_uri: string, URI for token endpoint. For convenience
586 defaults to Google's endpoints but any OAuth 2.0 provider can be used.
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -0800587 revoke_uri: string, URI for revoke endpoint. For convenience
588 defaults to Google's endpoints but any OAuth 2.0 provider can be used.
Johan Euphrosineacf517f2012-02-13 21:08:33 +0100589 user_agent: string, User agent of your application, default to None.
Joe Gregoriof08a4982011-10-07 13:11:16 -0400590 message: Message to display if there are problems with the OAuth 2.0
591 configuration. The message may contain HTML and will be presented on the
592 web interface for any method that uses the decorator.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400593 callback_path: string, The absolute path to use as the callback URI. Note
594 that this must match up with the URI given when registering the
595 application in the APIs Console.
Joe Gregorio1adde1a2012-01-06 12:30:35 -0500596 **kwargs: dict, Keyword arguments are be passed along as kwargs to the
597 OAuth2WebServerFlow constructor.
Joe Gregorio432f17e2011-05-22 23:18:00 -0400598 """
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400599 self.flow = None
Joe Gregorio432f17e2011-05-22 23:18:00 -0400600 self.credentials = None
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400601 self._client_id = client_id
602 self._client_secret = client_secret
Joe Gregorio5cf5d122012-11-16 16:36:12 -0500603 self._scope = util.scopes_to_string(scope)
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400604 self._auth_uri = auth_uri
605 self._token_uri = token_uri
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -0800606 self._revoke_uri = revoke_uri
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400607 self._user_agent = user_agent
608 self._kwargs = kwargs
Joe Gregoriof08a4982011-10-07 13:11:16 -0400609 self._message = message
610 self._in_error = False
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400611 self._callback_path = callback_path
Joe Gregoriof08a4982011-10-07 13:11:16 -0400612
613 def _display_error_message(self, request_handler):
614 request_handler.response.out.write('<html><body>')
Joe Gregorio77254c12012-08-27 14:13:22 -0400615 request_handler.response.out.write(_safe_html(self._message))
Joe Gregoriof08a4982011-10-07 13:11:16 -0400616 request_handler.response.out.write('</body></html>')
Joe Gregorio432f17e2011-05-22 23:18:00 -0400617
618 def oauth_required(self, method):
619 """Decorator that starts the OAuth 2.0 dance.
620
621 Starts the OAuth dance for the logged in user if they haven't already
622 granted access for this application.
623
624 Args:
625 method: callable, to be decorated method of a webapp.RequestHandler
626 instance.
627 """
JacobMoshenko8e905102011-06-20 09:53:10 -0400628
Joe Gregorio17774972012-03-01 11:11:59 -0500629 def check_oauth(request_handler, *args, **kwargs):
Joe Gregoriof08a4982011-10-07 13:11:16 -0400630 if self._in_error:
631 self._display_error_message(request_handler)
632 return
633
Joe Gregoriof427c532011-06-13 09:35:26 -0400634 user = users.get_current_user()
635 # Don't use @login_decorator as this could be used in a POST request.
636 if not user:
637 request_handler.redirect(users.create_login_url(
638 request_handler.request.uri))
639 return
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400640
641 self._create_flow(request_handler)
642
Joe Gregorio432f17e2011-05-22 23:18:00 -0400643 # Store the request URI in 'state' so we can use it later
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400644 self.flow.params['state'] = _build_state_value(request_handler, user)
Joe Gregorio432f17e2011-05-22 23:18:00 -0400645 self.credentials = StorageByKeyName(
646 CredentialsModel, user.user_id(), 'credentials').get()
647
648 if not self.has_credentials():
649 return request_handler.redirect(self.authorize_url())
650 try:
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400651 return method(request_handler, *args, **kwargs)
Joe Gregorio432f17e2011-05-22 23:18:00 -0400652 except AccessTokenRefreshError:
653 return request_handler.redirect(self.authorize_url())
654
655 return check_oauth
656
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400657 def _create_flow(self, request_handler):
658 """Create the Flow object.
659
660 The Flow is calculated lazily since we don't know where this app is
661 running until it receives a request, at which point redirect_uri can be
662 calculated and then the Flow object can be constructed.
663
664 Args:
665 request_handler: webapp.RequestHandler, the request handler.
666 """
667 if self.flow is None:
668 redirect_uri = request_handler.request.relative_url(
669 self._callback_path) # Usually /oauth2callback
670 self.flow = OAuth2WebServerFlow(self._client_id, self._client_secret,
671 self._scope, redirect_uri=redirect_uri,
672 user_agent=self._user_agent,
673 auth_uri=self._auth_uri,
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -0800674 token_uri=self._token_uri,
675 revoke_uri=self._revoke_uri,
676 **self._kwargs)
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400677
Joe Gregorio432f17e2011-05-22 23:18:00 -0400678 def oauth_aware(self, method):
679 """Decorator that sets up for OAuth 2.0 dance, but doesn't do it.
680
681 Does all the setup for the OAuth dance, but doesn't initiate it.
682 This decorator is useful if you want to create a page that knows
683 whether or not the user has granted access to this application.
684 From within a method decorated with @oauth_aware the has_credentials()
685 and authorize_url() methods can be called.
686
687 Args:
688 method: callable, to be decorated method of a webapp.RequestHandler
689 instance.
690 """
JacobMoshenko8e905102011-06-20 09:53:10 -0400691
Joe Gregorio17774972012-03-01 11:11:59 -0500692 def setup_oauth(request_handler, *args, **kwargs):
Joe Gregoriof08a4982011-10-07 13:11:16 -0400693 if self._in_error:
694 self._display_error_message(request_handler)
695 return
696
Joe Gregoriof427c532011-06-13 09:35:26 -0400697 user = users.get_current_user()
698 # Don't use @login_decorator as this could be used in a POST request.
699 if not user:
700 request_handler.redirect(users.create_login_url(
701 request_handler.request.uri))
702 return
Joe Gregoriof08a4982011-10-07 13:11:16 -0400703
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400704 self._create_flow(request_handler)
Joe Gregoriof08a4982011-10-07 13:11:16 -0400705
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400706 self.flow.params['state'] = _build_state_value(request_handler, user)
Joe Gregorio432f17e2011-05-22 23:18:00 -0400707 self.credentials = StorageByKeyName(
708 CredentialsModel, user.user_id(), 'credentials').get()
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400709 return method(request_handler, *args, **kwargs)
Joe Gregorio432f17e2011-05-22 23:18:00 -0400710 return setup_oauth
711
712 def has_credentials(self):
713 """True if for the logged in user there are valid access Credentials.
714
715 Must only be called from with a webapp.RequestHandler subclassed method
716 that had been decorated with either @oauth_required or @oauth_aware.
717 """
718 return self.credentials is not None and not self.credentials.invalid
719
720 def authorize_url(self):
721 """Returns the URL to start the OAuth dance.
722
723 Must only be called from with a webapp.RequestHandler subclassed method
724 that had been decorated with either @oauth_required or @oauth_aware.
725 """
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400726 url = self.flow.step1_get_authorize_url()
Joe Gregorio853bcf32012-03-02 15:30:23 -0500727 return str(url)
Joe Gregorio432f17e2011-05-22 23:18:00 -0400728
729 def http(self):
730 """Returns an authorized http instance.
731
732 Must only be called from within an @oauth_required decorated method, or
733 from within an @oauth_aware decorated method where has_credentials()
734 returns True.
735 """
736 return self.credentials.authorize(httplib2.Http())
737
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400738 @property
739 def callback_path(self):
740 """The absolute path where the callback will occur.
741
742 Note this is the absolute path, not the absolute URI, that will be
743 calculated by the decorator at runtime. See callback_handler() for how this
744 should be used.
745
746 Returns:
747 The callback path as a string.
748 """
749 return self._callback_path
750
751
752 def callback_handler(self):
753 """RequestHandler for the OAuth 2.0 redirect callback.
754
755 Usage:
756 app = webapp.WSGIApplication([
757 ('/index', MyIndexHandler),
758 ...,
759 (decorator.callback_path, decorator.callback_handler())
760 ])
761
762 Returns:
763 A webapp.RequestHandler that handles the redirect back from the
764 server during the OAuth 2.0 dance.
765 """
766 decorator = self
767
768 class OAuth2Handler(webapp.RequestHandler):
769 """Handler for the redirect_uri of the OAuth 2.0 dance."""
770
771 @login_required
772 def get(self):
773 error = self.request.get('error')
774 if error:
775 errormsg = self.request.get('error_description', error)
776 self.response.out.write(
Joe Gregorio77254c12012-08-27 14:13:22 -0400777 'The authorization request failed: %s' % _safe_html(errormsg))
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400778 else:
779 user = users.get_current_user()
780 decorator._create_flow(self)
781 credentials = decorator.flow.step2_exchange(self.request.params)
782 StorageByKeyName(
783 CredentialsModel, user.user_id(), 'credentials').put(credentials)
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400784 redirect_uri = _parse_state_value(str(self.request.get('state')),
785 user)
786 self.redirect(redirect_uri)
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400787
788 return OAuth2Handler
789
790 def callback_application(self):
791 """WSGI application for handling the OAuth 2.0 redirect callback.
792
793 If you need finer grained control use `callback_handler` which returns just
794 the webapp.RequestHandler.
795
796 Returns:
797 A webapp.WSGIApplication that handles the redirect back from the
798 server during the OAuth 2.0 dance.
799 """
800 return webapp.WSGIApplication([
801 (self.callback_path, self.callback_handler())
802 ])
803
Joe Gregorio432f17e2011-05-22 23:18:00 -0400804
Joe Gregoriof08a4982011-10-07 13:11:16 -0400805class OAuth2DecoratorFromClientSecrets(OAuth2Decorator):
806 """An OAuth2Decorator that builds from a clientsecrets file.
807
808 Uses a clientsecrets file as the source for all the information when
809 constructing an OAuth2Decorator.
810
811 Example:
812
813 decorator = OAuth2DecoratorFromClientSecrets(
814 os.path.join(os.path.dirname(__file__), 'client_secrets.json')
Joe Gregorioc4fc0952011-11-09 12:21:11 -0500815 scope='https://www.googleapis.com/auth/plus')
Joe Gregoriof08a4982011-10-07 13:11:16 -0400816
817
818 class MainHandler(webapp.RequestHandler):
819
820 @decorator.oauth_required
821 def get(self):
822 http = decorator.http()
823 # http is authorized with the user's Credentials and can be used
824 # in API calls
825 """
826
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400827 @util.positional(3)
Joe Gregorioc29aaa92012-07-16 16:16:31 -0400828 def __init__(self, filename, scope, message=None, cache=None):
Joe Gregoriof08a4982011-10-07 13:11:16 -0400829 """Constructor
830
831 Args:
832 filename: string, File name of client secrets.
Joe Gregorio5cf5d122012-11-16 16:36:12 -0500833 scope: string or iterable of strings, scope(s) of the credentials being
Joe Gregorio08cdcb82012-03-14 00:09:33 -0400834 requested.
Joe Gregoriof08a4982011-10-07 13:11:16 -0400835 message: string, A friendly string to display to the user if the
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400836 clientsecrets file is missing or invalid. The message may contain HTML
837 and will be presented on the web interface for any method that uses the
Joe Gregoriof08a4982011-10-07 13:11:16 -0400838 decorator.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400839 cache: An optional cache service client that implements get() and set()
Joe Gregorioc29aaa92012-07-16 16:16:31 -0400840 methods. See clientsecrets.loadfile() for details.
Joe Gregoriof08a4982011-10-07 13:11:16 -0400841 """
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400842 client_type, client_info = clientsecrets.loadfile(filename, cache=cache)
843 if client_type not in [
844 clientsecrets.TYPE_WEB, clientsecrets.TYPE_INSTALLED]:
845 raise InvalidClientSecretsError(
846 'OAuth2Decorator doesn\'t support this OAuth 2.0 flow.')
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -0800847 constructor_kwargs = {
848 'auth_uri': client_info['auth_uri'],
849 'token_uri': client_info['token_uri'],
850 'message': message,
851 }
852 revoke_uri = client_info.get('revoke_uri')
853 if revoke_uri is not None:
854 constructor_kwargs['revoke_uri'] = revoke_uri
Joe Gregorio6ceea2d2012-08-24 11:57:58 -0400855 super(OAuth2DecoratorFromClientSecrets, self).__init__(
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -0800856 client_info['client_id'], client_info['client_secret'],
857 scope, **constructor_kwargs)
Joe Gregoriof08a4982011-10-07 13:11:16 -0400858 if message is not None:
859 self._message = message
860 else:
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -0800861 self._message = 'Please configure your application for OAuth 2.0.'
Joe Gregoriof08a4982011-10-07 13:11:16 -0400862
863
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400864@util.positional(2)
865def oauth2decorator_from_clientsecrets(filename, scope,
Joe Gregorioc29aaa92012-07-16 16:16:31 -0400866 message=None, cache=None):
Joe Gregoriof08a4982011-10-07 13:11:16 -0400867 """Creates an OAuth2Decorator populated from a clientsecrets file.
868
869 Args:
870 filename: string, File name of client secrets.
Joe Gregorio08cdcb82012-03-14 00:09:33 -0400871 scope: string or list of strings, scope(s) of the credentials being
872 requested.
Joe Gregoriof08a4982011-10-07 13:11:16 -0400873 message: string, A friendly string to display to the user if the
874 clientsecrets file is missing or invalid. The message may contain HTML and
875 will be presented on the web interface for any method that uses the
876 decorator.
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400877 cache: An optional cache service client that implements get() and set()
Joe Gregorioc29aaa92012-07-16 16:16:31 -0400878 methods. See clientsecrets.loadfile() for details.
Joe Gregoriof08a4982011-10-07 13:11:16 -0400879
880 Returns: An OAuth2Decorator
881
882 """
Joe Gregorio68a8cfe2012-08-03 16:17:40 -0400883 return OAuth2DecoratorFromClientSecrets(filename, scope,
dhermes@google.coma9eb0bb2013-02-06 09:19:01 -0800884 message=message, cache=cache)