| OLD | NEW |
| (Empty) |
| 1 # Copyright 2011 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 # This code implements an OAUTH2 plugin that reads the access token from | |
| 16 # App Engine StorageByKeyName. | |
| 17 | |
| 18 from oauth2client.appengine import CredentialsProperty | |
| 19 from oauth2client.appengine import StorageByKeyName | |
| 20 from google.appengine.api import users | |
| 21 from google.appengine.ext import db | |
| 22 from boto.auth_handler import AuthHandler | |
| 23 from boto.auth_handler import NotReadyToAuthenticate | |
| 24 | |
| 25 | |
| 26 class Credentials(db.Model): | |
| 27 credentials = CredentialsProperty() | |
| 28 | |
| 29 | |
| 30 class OAuth2Auth(AuthHandler): | |
| 31 | |
| 32 capability = ['google-oauth2', 's3'] | |
| 33 | |
| 34 def __init__(self, path, config, provider): | |
| 35 if provider.name != 'google': | |
| 36 raise NotReadyToAuthenticate() | |
| 37 | |
| 38 def add_auth(self, http_request): | |
| 39 user = users.get_current_user() | |
| 40 credentials = StorageByKeyName( | |
| 41 Credentials, user.user_id(), 'credentials').get() | |
| 42 if not credentials or credentials.invalid: | |
| 43 raise NotReadyToAuthenticate() | |
| 44 http_request.headers['Authorization'] = ( | |
| 45 'OAuth %s' % str(credentials.access_token)) | |
| OLD | NEW |