Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [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. |
| 14 | |
| 15 | """Utilities for Google App Engine |
| 16 | |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 17 | Utilities for making it easier to use OAuth 2.0 on Google App Engine. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 18 | """ |
| 19 | |
| 20 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 21 | |
Joe Gregorio | 1daa71b | 2011-09-15 18:12:14 -0400 | [diff] [blame] | 22 | import base64 |
Joe Gregorio | 77254c1 | 2012-08-27 14:13:22 -0400 | [diff] [blame] | 23 | import cgi |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 24 | import httplib2 |
Joe Gregorio | 1daa71b | 2011-09-15 18:12:14 -0400 | [diff] [blame] | 25 | import logging |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 26 | import os |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 27 | import pickle |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 28 | import time |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 29 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 30 | from google.appengine.api import app_identity |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 31 | from google.appengine.api import memcache |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 32 | from google.appengine.api import users |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 33 | from google.appengine.ext import db |
| 34 | from google.appengine.ext import webapp |
| 35 | from google.appengine.ext.webapp.util import login_required |
| 36 | from google.appengine.ext.webapp.util import run_wsgi_app |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 37 | from oauth2client import GOOGLE_AUTH_URI |
| 38 | from oauth2client import GOOGLE_REVOKE_URI |
| 39 | from oauth2client import GOOGLE_TOKEN_URI |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 40 | from oauth2client import clientsecrets |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 41 | from oauth2client import util |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 42 | from oauth2client import xsrfutil |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 43 | from oauth2client.anyjson import simplejson |
| 44 | from oauth2client.client import AccessTokenRefreshError |
| 45 | from oauth2client.client import AssertionCredentials |
| 46 | from oauth2client.client import Credentials |
| 47 | from oauth2client.client import Flow |
| 48 | from oauth2client.client import OAuth2WebServerFlow |
| 49 | from oauth2client.client import Storage |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 50 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 51 | # TODO(dhermes): Resolve import issue. |
| 52 | # This is a temporary fix for a Google internal issue. |
| 53 | try: |
| 54 | from google.appengine.ext import ndb |
| 55 | except ImportError: |
| 56 | ndb = None |
| 57 | |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 58 | logger = logging.getLogger(__name__) |
| 59 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 60 | OAUTH2CLIENT_NAMESPACE = 'oauth2client#ns' |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 61 | |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 62 | XSRF_MEMCACHE_ID = 'xsrf_secret_key' |
| 63 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 64 | |
Joe Gregorio | 77254c1 | 2012-08-27 14:13:22 -0400 | [diff] [blame] | 65 | def _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 Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 77 | class InvalidClientSecretsError(Exception): |
| 78 | """The client_secrets.json file is malformed or missing required fields.""" |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 79 | |
| 80 | |
| 81 | class InvalidXsrfTokenError(Exception): |
| 82 | """The XSRF token is invalid or expired.""" |
| 83 | |
| 84 | |
| 85 | class 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.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 89 | site. |
| 90 | """ |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 91 | secret = db.StringProperty() |
| 92 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 93 | if ndb is not None: |
| 94 | class SiteXsrfSecretKeyNDB(ndb.Model): |
| 95 | """NDB Model for storage for the sites XSRF secret key. |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 96 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 97 | 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.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 100 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 101 | There should only be one instance stored of this model, the one used for the |
| 102 | site. |
| 103 | """ |
| 104 | secret = ndb.StringProperty() |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 105 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 106 | @classmethod |
| 107 | def _get_kind(cls): |
| 108 | """Return the kind name for this class.""" |
| 109 | return 'SiteXsrfSecretKey' |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 110 | |
| 111 | |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 112 | def _generate_new_xsrf_secret_key(): |
| 113 | """Returns a random XSRF secret key. |
| 114 | """ |
| 115 | return os.urandom(16).encode("hex") |
| 116 | |
| 117 | |
| 118 | def 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 Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 138 | |
| 139 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 140 | class 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 Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 144 | 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. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 147 | |
| 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. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 151 | """ |
| 152 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 153 | @util.positional(2) |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 154 | def __init__(self, scope, **kwargs): |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 155 | """Constructor for AppAssertionCredentials |
| 156 | |
| 157 | Args: |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 158 | scope: string or iterable of strings, scope(s) of the credentials being |
Joe Gregorio | fd08e43 | 2012-08-09 14:17:41 -0400 | [diff] [blame] | 159 | requested. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 160 | """ |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 161 | self.scope = util.scopes_to_string(scope) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 162 | |
dhermes@google.com | 2cc0938 | 2013-02-11 08:42:18 -0800 | [diff] [blame] | 163 | # Assertion type is no longer used, but still in the parent class signature. |
| 164 | super(AppAssertionCredentials, self).__init__(None) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 165 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 166 | @classmethod |
| 167 | def from_json(cls, json): |
| 168 | data = simplejson.loads(json) |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 169 | return AppAssertionCredentials(data['scope']) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 170 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 171 | def _refresh(self, http_request): |
| 172 | """Refreshes the access_token. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 173 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 174 | Since the underlying App Engine app_identity implementation does its own |
| 175 | caching we can skip all the storage hoops and just to a refresh using the |
| 176 | API. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 177 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 178 | Args: |
| 179 | http_request: callable, a callable that matches the method signature of |
| 180 | httplib2.Http.request, used to make the refresh request. |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 181 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 182 | Raises: |
| 183 | AccessTokenRefreshError: When the refresh fails. |
| 184 | """ |
| 185 | try: |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 186 | scopes = self.scope.split() |
| 187 | (token, _) = app_identity.get_access_token(scopes) |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 188 | except app_identity.Error, e: |
| 189 | raise AccessTokenRefreshError(str(e)) |
| 190 | self.access_token = token |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 191 | |
| 192 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 193 | class FlowProperty(db.Property): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 194 | """App Engine datastore Property for Flow. |
| 195 | |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 196 | Utility property that allows easy storage and retrieval of an |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 197 | oauth2client.Flow""" |
| 198 | |
| 199 | # Tell what the user type is. |
| 200 | data_type = Flow |
| 201 | |
| 202 | # For writing to datastore. |
| 203 | def get_value_for_datastore(self, model_instance): |
| 204 | flow = super(FlowProperty, |
| 205 | self).get_value_for_datastore(model_instance) |
| 206 | return db.Blob(pickle.dumps(flow)) |
| 207 | |
| 208 | # For reading from datastore. |
| 209 | def make_value_from_datastore(self, value): |
| 210 | if value is None: |
| 211 | return None |
| 212 | return pickle.loads(value) |
| 213 | |
| 214 | def validate(self, value): |
| 215 | if value is not None and not isinstance(value, Flow): |
Joe Gregorio | 1daa71b | 2011-09-15 18:12:14 -0400 | [diff] [blame] | 216 | raise db.BadValueError('Property %s must be convertible ' |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 217 | 'to a FlowThreeLegged instance (%s)' % |
| 218 | (self.name, value)) |
| 219 | return super(FlowProperty, self).validate(value) |
| 220 | |
| 221 | def empty(self, value): |
| 222 | return not value |
| 223 | |
| 224 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 225 | if ndb is not None: |
| 226 | class FlowNDBProperty(ndb.PickleProperty): |
| 227 | """App Engine NDB datastore Property for Flow. |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 228 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 229 | Serves the same purpose as the DB FlowProperty, but for NDB models. Since |
| 230 | PickleProperty inherits from BlobProperty, the underlying representation of |
| 231 | the data in the datastore will be the same as in the DB case. |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 232 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 233 | Utility property that allows easy storage and retrieval of an |
| 234 | oauth2client.Flow |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 235 | """ |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 236 | |
| 237 | def _validate(self, value): |
| 238 | """Validates a value as a proper Flow object. |
| 239 | |
| 240 | Args: |
| 241 | value: A value to be set on the property. |
| 242 | |
| 243 | Raises: |
| 244 | TypeError if the value is not an instance of Flow. |
| 245 | """ |
| 246 | logger.info('validate: Got type %s', type(value)) |
| 247 | if value is not None and not isinstance(value, Flow): |
| 248 | raise TypeError('Property %s must be convertible to a flow ' |
| 249 | 'instance; received: %s.' % (self._name, value)) |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 250 | |
| 251 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 252 | class CredentialsProperty(db.Property): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 253 | """App Engine datastore Property for Credentials. |
| 254 | |
| 255 | Utility property that allows easy storage and retrieval of |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 256 | oath2client.Credentials |
| 257 | """ |
| 258 | |
| 259 | # Tell what the user type is. |
| 260 | data_type = Credentials |
| 261 | |
| 262 | # For writing to datastore. |
| 263 | def get_value_for_datastore(self, model_instance): |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 264 | logger.info("get: Got type " + str(type(model_instance))) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 265 | cred = super(CredentialsProperty, |
| 266 | self).get_value_for_datastore(model_instance) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 267 | if cred is None: |
| 268 | cred = '' |
| 269 | else: |
| 270 | cred = cred.to_json() |
| 271 | return db.Blob(cred) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 272 | |
| 273 | # For reading from datastore. |
| 274 | def make_value_from_datastore(self, value): |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 275 | logger.info("make: Got type " + str(type(value))) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 276 | if value is None: |
| 277 | return None |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 278 | if len(value) == 0: |
| 279 | return None |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 280 | try: |
| 281 | credentials = Credentials.new_from_json(value) |
| 282 | except ValueError: |
Joe Gregorio | ec55584 | 2011-10-27 11:10:39 -0400 | [diff] [blame] | 283 | credentials = None |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 284 | return credentials |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 285 | |
| 286 | def validate(self, value): |
Joe Gregorio | 1daa71b | 2011-09-15 18:12:14 -0400 | [diff] [blame] | 287 | value = super(CredentialsProperty, self).validate(value) |
Joe Gregorio | a19f3a7 | 2012-07-11 15:35:35 -0400 | [diff] [blame] | 288 | logger.info("validate: Got type " + str(type(value))) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 289 | if value is not None and not isinstance(value, Credentials): |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 290 | raise db.BadValueError('Property %s must be convertible ' |
Joe Gregorio | 1daa71b | 2011-09-15 18:12:14 -0400 | [diff] [blame] | 291 | 'to a Credentials instance (%s)' % |
| 292 | (self.name, value)) |
| 293 | #if value is not None and not isinstance(value, Credentials): |
| 294 | # return None |
| 295 | return value |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 296 | |
| 297 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 298 | if ndb is not None: |
| 299 | # TODO(dhermes): Turn this into a JsonProperty and overhaul the Credentials |
| 300 | # and subclass mechanics to use new_from_dict, to_dict, |
| 301 | # from_dict, etc. |
| 302 | class CredentialsNDBProperty(ndb.BlobProperty): |
| 303 | """App Engine NDB datastore Property for Credentials. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 304 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 305 | Serves the same purpose as the DB CredentialsProperty, but for NDB models. |
| 306 | Since CredentialsProperty stores data as a blob and this inherits from |
| 307 | BlobProperty, the data in the datastore will be the same as in the DB case. |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 308 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 309 | Utility property that allows easy storage and retrieval of Credentials and |
| 310 | subclasses. |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 311 | """ |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 312 | def _validate(self, value): |
| 313 | """Validates a value as a proper credentials object. |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 314 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 315 | Args: |
| 316 | value: A value to be set on the property. |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 317 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 318 | Raises: |
| 319 | TypeError if the value is not an instance of Credentials. |
| 320 | """ |
| 321 | logger.info('validate: Got type %s', type(value)) |
| 322 | if value is not None and not isinstance(value, Credentials): |
| 323 | raise TypeError('Property %s must be convertible to a credentials ' |
| 324 | 'instance; received: %s.' % (self._name, value)) |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 325 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 326 | def _to_base_type(self, value): |
| 327 | """Converts our validated value to a JSON serialized string. |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 328 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 329 | Args: |
| 330 | value: A value to be set in the datastore. |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 331 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 332 | Returns: |
| 333 | A JSON serialized version of the credential, else '' if value is None. |
| 334 | """ |
| 335 | if value is None: |
| 336 | return '' |
| 337 | else: |
| 338 | return value.to_json() |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 339 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 340 | def _from_base_type(self, value): |
| 341 | """Converts our stored JSON string back to the desired type. |
| 342 | |
| 343 | Args: |
| 344 | value: A value from the datastore to be converted to the desired type. |
| 345 | |
| 346 | Returns: |
| 347 | A deserialized Credentials (or subclass) object, else None if the |
| 348 | value can't be parsed. |
| 349 | """ |
| 350 | if not value: |
| 351 | return None |
| 352 | try: |
| 353 | # Uses the from_json method of the implied class of value |
| 354 | credentials = Credentials.new_from_json(value) |
| 355 | except ValueError: |
| 356 | credentials = None |
| 357 | return credentials |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 358 | |
| 359 | |
| 360 | class StorageByKeyName(Storage): |
| 361 | """Store and retrieve a credential to and from the App Engine datastore. |
| 362 | |
| 363 | This Storage helper presumes the Credentials have been stored as a |
| 364 | CredentialsProperty or CredentialsNDBProperty on a datastore model class, and |
| 365 | that entities are stored by key_name. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 366 | """ |
| 367 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 368 | @util.positional(4) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 369 | def __init__(self, model, key_name, property_name, cache=None): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 370 | """Constructor for Storage. |
| 371 | |
| 372 | Args: |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 373 | model: db.Model or ndb.Model, model class |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 374 | key_name: string, key name for the entity that has the credentials |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 375 | property_name: string, name of the property that is a CredentialsProperty |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 376 | or CredentialsNDBProperty. |
| 377 | cache: memcache, a write-through cache to put in front of the datastore. |
| 378 | If the model you are using is an NDB model, using a cache will be |
| 379 | redundant since the model uses an instance cache and memcache for you. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 380 | """ |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 381 | self._model = model |
| 382 | self._key_name = key_name |
| 383 | self._property_name = property_name |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 384 | self._cache = cache |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 385 | |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 386 | def _is_ndb(self): |
| 387 | """Determine whether the model of the instance is an NDB model. |
| 388 | |
| 389 | Returns: |
| 390 | Boolean indicating whether or not the model is an NDB or DB model. |
| 391 | """ |
| 392 | # issubclass will fail if one of the arguments is not a class, only need |
| 393 | # worry about new-style classes since ndb and db models are new-style |
| 394 | if isinstance(self._model, type): |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 395 | if ndb is not None and issubclass(self._model, ndb.Model): |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 396 | return True |
| 397 | elif issubclass(self._model, db.Model): |
| 398 | return False |
| 399 | |
| 400 | raise TypeError('Model class not an NDB or DB model: %s.' % (self._model,)) |
| 401 | |
| 402 | def _get_entity(self): |
| 403 | """Retrieve entity from datastore. |
| 404 | |
| 405 | Uses a different model method for db or ndb models. |
| 406 | |
| 407 | Returns: |
| 408 | Instance of the model corresponding to the current storage object |
| 409 | and stored using the key name of the storage object. |
| 410 | """ |
| 411 | if self._is_ndb(): |
| 412 | return self._model.get_by_id(self._key_name) |
| 413 | else: |
| 414 | return self._model.get_by_key_name(self._key_name) |
| 415 | |
| 416 | def _delete_entity(self): |
| 417 | """Delete entity from datastore. |
| 418 | |
| 419 | Attempts to delete using the key_name stored on the object, whether or not |
| 420 | the given key is in the datastore. |
| 421 | """ |
| 422 | if self._is_ndb(): |
| 423 | ndb.Key(self._model, self._key_name).delete() |
| 424 | else: |
| 425 | entity_key = db.Key.from_path(self._model.kind(), self._key_name) |
| 426 | db.delete(entity_key) |
| 427 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 428 | def locked_get(self): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 429 | """Retrieve Credential from datastore. |
| 430 | |
| 431 | Returns: |
| 432 | oauth2client.Credentials |
| 433 | """ |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 434 | if self._cache: |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 435 | json = self._cache.get(self._key_name) |
| 436 | if json: |
| 437 | return Credentials.new_from_json(json) |
Joe Gregorio | 9fa077c | 2011-11-18 08:16:52 -0500 | [diff] [blame] | 438 | |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 439 | credentials = None |
| 440 | entity = self._get_entity() |
Joe Gregorio | 9fa077c | 2011-11-18 08:16:52 -0500 | [diff] [blame] | 441 | if entity is not None: |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 442 | credentials = getattr(entity, self._property_name) |
| 443 | if credentials and hasattr(credentials, 'set_store'): |
| 444 | credentials.set_store(self) |
Joe Gregorio | 9fa077c | 2011-11-18 08:16:52 -0500 | [diff] [blame] | 445 | if self._cache: |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 446 | self._cache.set(self._key_name, credentials.to_json()) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 447 | |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 448 | return credentials |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 449 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 450 | def locked_put(self, credentials): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 451 | """Write a Credentials to the datastore. |
| 452 | |
| 453 | Args: |
| 454 | credentials: Credentials, the credentials to store. |
| 455 | """ |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 456 | entity = self._model.get_or_insert(self._key_name) |
| 457 | setattr(entity, self._property_name, credentials) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 458 | entity.put() |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 459 | if self._cache: |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 460 | self._cache.set(self._key_name, credentials.to_json()) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 461 | |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame] | 462 | def locked_delete(self): |
| 463 | """Delete Credential from datastore.""" |
| 464 | |
| 465 | if self._cache: |
| 466 | self._cache.delete(self._key_name) |
| 467 | |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 468 | self._delete_entity() |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame] | 469 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 470 | |
| 471 | class CredentialsModel(db.Model): |
| 472 | """Storage for OAuth 2.0 Credentials |
| 473 | |
| 474 | Storage of the model is keyed by the user.user_id(). |
| 475 | """ |
| 476 | credentials = CredentialsProperty() |
| 477 | |
| 478 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 479 | if ndb is not None: |
| 480 | class CredentialsNDBModel(ndb.Model): |
| 481 | """NDB Model for storage of OAuth 2.0 Credentials |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 482 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 483 | Since this model uses the same kind as CredentialsModel and has a property |
| 484 | which can serialize and deserialize Credentials correctly, it can be used |
| 485 | interchangeably with a CredentialsModel to access, insert and delete the |
| 486 | same entities. This simply provides an NDB model for interacting with the |
| 487 | same data the DB model interacts with. |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 488 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 489 | Storage of the model is keyed by the user.user_id(). |
| 490 | """ |
| 491 | credentials = CredentialsNDBProperty() |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 492 | |
Joe Gregorio | 78787b6 | 2013-02-08 15:36:21 -0500 | [diff] [blame] | 493 | @classmethod |
| 494 | def _get_kind(cls): |
| 495 | """Return the kind name for this class.""" |
| 496 | return 'CredentialsModel' |
dhermes@google.com | 4715482 | 2012-11-26 10:44:09 -0800 | [diff] [blame] | 497 | |
| 498 | |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 499 | def _build_state_value(request_handler, user): |
| 500 | """Composes the value for the 'state' parameter. |
| 501 | |
| 502 | Packs the current request URI and an XSRF token into an opaque string that |
| 503 | can be passed to the authentication server via the 'state' parameter. |
| 504 | |
| 505 | Args: |
| 506 | request_handler: webapp.RequestHandler, The request. |
| 507 | user: google.appengine.api.users.User, The current user. |
| 508 | |
| 509 | Returns: |
| 510 | The state value as a string. |
| 511 | """ |
| 512 | uri = request_handler.request.url |
| 513 | token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(), |
| 514 | action_id=str(uri)) |
| 515 | return uri + ':' + token |
| 516 | |
| 517 | |
| 518 | def _parse_state_value(state, user): |
| 519 | """Parse the value of the 'state' parameter. |
| 520 | |
| 521 | Parses the value and validates the XSRF token in the state parameter. |
| 522 | |
| 523 | Args: |
| 524 | state: string, The value of the state parameter. |
| 525 | user: google.appengine.api.users.User, The current user. |
| 526 | |
| 527 | Raises: |
| 528 | InvalidXsrfTokenError: if the XSRF token is invalid. |
| 529 | |
| 530 | Returns: |
| 531 | The redirect URI. |
| 532 | """ |
| 533 | uri, token = state.rsplit(':', 1) |
| 534 | if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(), |
| 535 | action_id=uri): |
| 536 | raise InvalidXsrfTokenError() |
| 537 | |
| 538 | return uri |
| 539 | |
| 540 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 541 | class OAuth2Decorator(object): |
| 542 | """Utility for making OAuth 2.0 easier. |
| 543 | |
| 544 | Instantiate and then use with oauth_required or oauth_aware |
| 545 | as decorators on webapp.RequestHandler methods. |
| 546 | |
| 547 | Example: |
| 548 | |
| 549 | decorator = OAuth2Decorator( |
| 550 | client_id='837...ent.com', |
| 551 | client_secret='Qh...wwI', |
Joe Gregorio | c4fc095 | 2011-11-09 12:21:11 -0500 | [diff] [blame] | 552 | scope='https://www.googleapis.com/auth/plus') |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 553 | |
| 554 | |
| 555 | class MainHandler(webapp.RequestHandler): |
| 556 | |
| 557 | @decorator.oauth_required |
| 558 | def get(self): |
| 559 | http = decorator.http() |
| 560 | # http is authorized with the user's Credentials and can be used |
| 561 | # in API calls |
| 562 | |
| 563 | """ |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 564 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 565 | @util.positional(4) |
JacobMoshenko | cb6d891 | 2011-07-08 13:35:15 -0400 | [diff] [blame] | 566 | def __init__(self, client_id, client_secret, scope, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 567 | auth_uri=GOOGLE_AUTH_URI, |
| 568 | token_uri=GOOGLE_TOKEN_URI, |
| 569 | revoke_uri=GOOGLE_REVOKE_URI, |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 570 | user_agent=None, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 571 | message=None, |
| 572 | callback_path='/oauth2callback', |
| 573 | **kwargs): |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 574 | |
| 575 | """Constructor for OAuth2Decorator |
| 576 | |
| 577 | Args: |
| 578 | client_id: string, client identifier. |
| 579 | client_secret: string client secret. |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 580 | scope: string or iterable of strings, scope(s) of the credentials being |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 581 | requested. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 582 | auth_uri: string, URI for authorization endpoint. For convenience |
| 583 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
| 584 | token_uri: string, URI for token endpoint. For convenience |
| 585 | 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] | 586 | revoke_uri: string, URI for revoke endpoint. For convenience |
| 587 | defaults to Google's endpoints but any OAuth 2.0 provider can be used. |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 588 | user_agent: string, User agent of your application, default to None. |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 589 | message: Message to display if there are problems with the OAuth 2.0 |
| 590 | configuration. The message may contain HTML and will be presented on the |
| 591 | web interface for any method that uses the decorator. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 592 | callback_path: string, The absolute path to use as the callback URI. Note |
| 593 | that this must match up with the URI given when registering the |
| 594 | application in the APIs Console. |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 595 | **kwargs: dict, Keyword arguments are be passed along as kwargs to the |
| 596 | OAuth2WebServerFlow constructor. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 597 | """ |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 598 | self.flow = None |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 599 | self.credentials = None |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 600 | self._client_id = client_id |
| 601 | self._client_secret = client_secret |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 602 | self._scope = util.scopes_to_string(scope) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 603 | self._auth_uri = auth_uri |
| 604 | self._token_uri = token_uri |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 605 | self._revoke_uri = revoke_uri |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 606 | self._user_agent = user_agent |
| 607 | self._kwargs = kwargs |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 608 | self._message = message |
| 609 | self._in_error = False |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 610 | self._callback_path = callback_path |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 611 | |
| 612 | def _display_error_message(self, request_handler): |
| 613 | request_handler.response.out.write('<html><body>') |
Joe Gregorio | 77254c1 | 2012-08-27 14:13:22 -0400 | [diff] [blame] | 614 | request_handler.response.out.write(_safe_html(self._message)) |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 615 | request_handler.response.out.write('</body></html>') |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 616 | |
| 617 | def oauth_required(self, method): |
| 618 | """Decorator that starts the OAuth 2.0 dance. |
| 619 | |
| 620 | Starts the OAuth dance for the logged in user if they haven't already |
| 621 | granted access for this application. |
| 622 | |
| 623 | Args: |
| 624 | method: callable, to be decorated method of a webapp.RequestHandler |
| 625 | instance. |
| 626 | """ |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 627 | |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 628 | def check_oauth(request_handler, *args, **kwargs): |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 629 | if self._in_error: |
| 630 | self._display_error_message(request_handler) |
| 631 | return |
| 632 | |
Joe Gregorio | f427c53 | 2011-06-13 09:35:26 -0400 | [diff] [blame] | 633 | user = users.get_current_user() |
| 634 | # Don't use @login_decorator as this could be used in a POST request. |
| 635 | if not user: |
| 636 | request_handler.redirect(users.create_login_url( |
| 637 | request_handler.request.uri)) |
| 638 | return |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 639 | |
| 640 | self._create_flow(request_handler) |
| 641 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 642 | # Store the request URI in 'state' so we can use it later |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 643 | self.flow.params['state'] = _build_state_value(request_handler, user) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 644 | self.credentials = StorageByKeyName( |
| 645 | CredentialsModel, user.user_id(), 'credentials').get() |
| 646 | |
| 647 | if not self.has_credentials(): |
| 648 | return request_handler.redirect(self.authorize_url()) |
| 649 | try: |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 650 | return method(request_handler, *args, **kwargs) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 651 | except AccessTokenRefreshError: |
| 652 | return request_handler.redirect(self.authorize_url()) |
| 653 | |
| 654 | return check_oauth |
| 655 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 656 | def _create_flow(self, request_handler): |
| 657 | """Create the Flow object. |
| 658 | |
| 659 | The Flow is calculated lazily since we don't know where this app is |
| 660 | running until it receives a request, at which point redirect_uri can be |
| 661 | calculated and then the Flow object can be constructed. |
| 662 | |
| 663 | Args: |
| 664 | request_handler: webapp.RequestHandler, the request handler. |
| 665 | """ |
| 666 | if self.flow is None: |
| 667 | redirect_uri = request_handler.request.relative_url( |
| 668 | self._callback_path) # Usually /oauth2callback |
| 669 | self.flow = OAuth2WebServerFlow(self._client_id, self._client_secret, |
| 670 | self._scope, redirect_uri=redirect_uri, |
| 671 | user_agent=self._user_agent, |
| 672 | auth_uri=self._auth_uri, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 673 | token_uri=self._token_uri, |
| 674 | revoke_uri=self._revoke_uri, |
| 675 | **self._kwargs) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 676 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 677 | def oauth_aware(self, method): |
| 678 | """Decorator that sets up for OAuth 2.0 dance, but doesn't do it. |
| 679 | |
| 680 | Does all the setup for the OAuth dance, but doesn't initiate it. |
| 681 | This decorator is useful if you want to create a page that knows |
| 682 | whether or not the user has granted access to this application. |
| 683 | From within a method decorated with @oauth_aware the has_credentials() |
| 684 | and authorize_url() methods can be called. |
| 685 | |
| 686 | Args: |
| 687 | method: callable, to be decorated method of a webapp.RequestHandler |
| 688 | instance. |
| 689 | """ |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 690 | |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 691 | def setup_oauth(request_handler, *args, **kwargs): |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 692 | if self._in_error: |
| 693 | self._display_error_message(request_handler) |
| 694 | return |
| 695 | |
Joe Gregorio | f427c53 | 2011-06-13 09:35:26 -0400 | [diff] [blame] | 696 | user = users.get_current_user() |
| 697 | # Don't use @login_decorator as this could be used in a POST request. |
| 698 | if not user: |
| 699 | request_handler.redirect(users.create_login_url( |
| 700 | request_handler.request.uri)) |
| 701 | return |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 702 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 703 | self._create_flow(request_handler) |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 704 | |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 705 | self.flow.params['state'] = _build_state_value(request_handler, user) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 706 | self.credentials = StorageByKeyName( |
| 707 | CredentialsModel, user.user_id(), 'credentials').get() |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 708 | return method(request_handler, *args, **kwargs) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 709 | return setup_oauth |
| 710 | |
| 711 | def has_credentials(self): |
| 712 | """True if for the logged in user there are valid access Credentials. |
| 713 | |
| 714 | Must only be called from with a webapp.RequestHandler subclassed method |
| 715 | that had been decorated with either @oauth_required or @oauth_aware. |
| 716 | """ |
| 717 | return self.credentials is not None and not self.credentials.invalid |
| 718 | |
| 719 | def authorize_url(self): |
| 720 | """Returns the URL to start the OAuth dance. |
| 721 | |
| 722 | Must only be called from with a webapp.RequestHandler subclassed method |
| 723 | that had been decorated with either @oauth_required or @oauth_aware. |
| 724 | """ |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 725 | url = self.flow.step1_get_authorize_url() |
Joe Gregorio | 853bcf3 | 2012-03-02 15:30:23 -0500 | [diff] [blame] | 726 | return str(url) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 727 | |
| 728 | def http(self): |
| 729 | """Returns an authorized http instance. |
| 730 | |
| 731 | Must only be called from within an @oauth_required decorated method, or |
| 732 | from within an @oauth_aware decorated method where has_credentials() |
| 733 | returns True. |
| 734 | """ |
| 735 | return self.credentials.authorize(httplib2.Http()) |
| 736 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 737 | @property |
| 738 | def callback_path(self): |
| 739 | """The absolute path where the callback will occur. |
| 740 | |
| 741 | Note this is the absolute path, not the absolute URI, that will be |
| 742 | calculated by the decorator at runtime. See callback_handler() for how this |
| 743 | should be used. |
| 744 | |
| 745 | Returns: |
| 746 | The callback path as a string. |
| 747 | """ |
| 748 | return self._callback_path |
| 749 | |
| 750 | |
| 751 | def callback_handler(self): |
| 752 | """RequestHandler for the OAuth 2.0 redirect callback. |
| 753 | |
| 754 | Usage: |
| 755 | app = webapp.WSGIApplication([ |
| 756 | ('/index', MyIndexHandler), |
| 757 | ..., |
| 758 | (decorator.callback_path, decorator.callback_handler()) |
| 759 | ]) |
| 760 | |
| 761 | Returns: |
| 762 | A webapp.RequestHandler that handles the redirect back from the |
| 763 | server during the OAuth 2.0 dance. |
| 764 | """ |
| 765 | decorator = self |
| 766 | |
| 767 | class OAuth2Handler(webapp.RequestHandler): |
| 768 | """Handler for the redirect_uri of the OAuth 2.0 dance.""" |
| 769 | |
| 770 | @login_required |
| 771 | def get(self): |
| 772 | error = self.request.get('error') |
| 773 | if error: |
| 774 | errormsg = self.request.get('error_description', error) |
| 775 | self.response.out.write( |
Joe Gregorio | 77254c1 | 2012-08-27 14:13:22 -0400 | [diff] [blame] | 776 | 'The authorization request failed: %s' % _safe_html(errormsg)) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 777 | else: |
| 778 | user = users.get_current_user() |
| 779 | decorator._create_flow(self) |
| 780 | credentials = decorator.flow.step2_exchange(self.request.params) |
| 781 | StorageByKeyName( |
| 782 | CredentialsModel, user.user_id(), 'credentials').put(credentials) |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 783 | redirect_uri = _parse_state_value(str(self.request.get('state')), |
| 784 | user) |
| 785 | self.redirect(redirect_uri) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 786 | |
| 787 | return OAuth2Handler |
| 788 | |
| 789 | def callback_application(self): |
| 790 | """WSGI application for handling the OAuth 2.0 redirect callback. |
| 791 | |
| 792 | If you need finer grained control use `callback_handler` which returns just |
| 793 | the webapp.RequestHandler. |
| 794 | |
| 795 | Returns: |
| 796 | A webapp.WSGIApplication that handles the redirect back from the |
| 797 | server during the OAuth 2.0 dance. |
| 798 | """ |
| 799 | return webapp.WSGIApplication([ |
| 800 | (self.callback_path, self.callback_handler()) |
| 801 | ]) |
| 802 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 803 | |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 804 | class OAuth2DecoratorFromClientSecrets(OAuth2Decorator): |
| 805 | """An OAuth2Decorator that builds from a clientsecrets file. |
| 806 | |
| 807 | Uses a clientsecrets file as the source for all the information when |
| 808 | constructing an OAuth2Decorator. |
| 809 | |
| 810 | Example: |
| 811 | |
| 812 | decorator = OAuth2DecoratorFromClientSecrets( |
| 813 | os.path.join(os.path.dirname(__file__), 'client_secrets.json') |
Joe Gregorio | c4fc095 | 2011-11-09 12:21:11 -0500 | [diff] [blame] | 814 | scope='https://www.googleapis.com/auth/plus') |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 815 | |
| 816 | |
| 817 | class MainHandler(webapp.RequestHandler): |
| 818 | |
| 819 | @decorator.oauth_required |
| 820 | def get(self): |
| 821 | http = decorator.http() |
| 822 | # http is authorized with the user's Credentials and can be used |
| 823 | # in API calls |
| 824 | """ |
| 825 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 826 | @util.positional(3) |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 827 | def __init__(self, filename, scope, message=None, cache=None): |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 828 | """Constructor |
| 829 | |
| 830 | Args: |
| 831 | filename: string, File name of client secrets. |
Joe Gregorio | 5cf5d12 | 2012-11-16 16:36:12 -0500 | [diff] [blame] | 832 | scope: string or iterable of strings, scope(s) of the credentials being |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 833 | requested. |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 834 | message: string, A friendly string to display to the user if the |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 835 | clientsecrets file is missing or invalid. The message may contain HTML |
| 836 | and will be presented on the web interface for any method that uses the |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 837 | decorator. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 838 | cache: An optional cache service client that implements get() and set() |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 839 | methods. See clientsecrets.loadfile() for details. |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 840 | """ |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 841 | client_type, client_info = clientsecrets.loadfile(filename, cache=cache) |
| 842 | if client_type not in [ |
| 843 | clientsecrets.TYPE_WEB, clientsecrets.TYPE_INSTALLED]: |
| 844 | raise InvalidClientSecretsError( |
| 845 | 'OAuth2Decorator doesn\'t support this OAuth 2.0 flow.') |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 846 | constructor_kwargs = { |
| 847 | 'auth_uri': client_info['auth_uri'], |
| 848 | 'token_uri': client_info['token_uri'], |
| 849 | 'message': message, |
| 850 | } |
| 851 | revoke_uri = client_info.get('revoke_uri') |
| 852 | if revoke_uri is not None: |
| 853 | constructor_kwargs['revoke_uri'] = revoke_uri |
Joe Gregorio | 6ceea2d | 2012-08-24 11:57:58 -0400 | [diff] [blame] | 854 | super(OAuth2DecoratorFromClientSecrets, self).__init__( |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 855 | client_info['client_id'], client_info['client_secret'], |
| 856 | scope, **constructor_kwargs) |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 857 | if message is not None: |
| 858 | self._message = message |
| 859 | else: |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 860 | self._message = 'Please configure your application for OAuth 2.0.' |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 861 | |
| 862 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 863 | @util.positional(2) |
| 864 | def oauth2decorator_from_clientsecrets(filename, scope, |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 865 | message=None, cache=None): |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 866 | """Creates an OAuth2Decorator populated from a clientsecrets file. |
| 867 | |
| 868 | Args: |
| 869 | filename: string, File name of client secrets. |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 870 | scope: string or list of strings, scope(s) of the credentials being |
| 871 | requested. |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 872 | message: string, A friendly string to display to the user if the |
| 873 | clientsecrets file is missing or invalid. The message may contain HTML and |
| 874 | will be presented on the web interface for any method that uses the |
| 875 | decorator. |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 876 | cache: An optional cache service client that implements get() and set() |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 877 | methods. See clientsecrets.loadfile() for details. |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 878 | |
| 879 | Returns: An OAuth2Decorator |
| 880 | |
| 881 | """ |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 882 | return OAuth2DecoratorFromClientSecrets(filename, scope, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 883 | message=message, cache=cache) |