| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2014 Google Inc. All Rights Reserved. |
| 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 """Boto auth plugin for OAuth2.0 for Google Cloud Storage.""" |
| 16 |
| 17 from __future__ import absolute_import |
| 18 |
| 19 from boto.auth_handler import AuthHandler |
| 20 from boto.auth_handler import NotReadyToAuthenticate |
| 21 |
| 22 from gcs_oauth2_boto_plugin import oauth2_client |
| 23 from gcs_oauth2_boto_plugin import oauth2_helper |
| 24 |
| 25 IS_SERVICE_ACCOUNT = False |
| 26 |
| 27 |
| 28 class OAuth2Auth(AuthHandler): |
| 29 |
| 30 capability = ['google-oauth2', 's3'] |
| 31 |
| 32 def __init__(self, path, config, provider): |
| 33 self.oauth2_client = None |
| 34 if (provider.name == 'google'): |
| 35 if config.has_option('Credentials', 'gs_oauth2_refresh_token'): |
| 36 self.oauth2_client = oauth2_helper.OAuth2ClientFromBotoConfig(config) |
| 37 elif config.has_option('GoogleCompute', 'service_account'): |
| 38 self.oauth2_client = oauth2_client.CreateOAuth2GCEClient() |
| 39 if not self.oauth2_client: |
| 40 raise NotReadyToAuthenticate() |
| 41 |
| 42 def add_auth(self, http_request): |
| 43 http_request.headers['Authorization'] = \ |
| 44 self.oauth2_client.GetAuthorizationHeader() |
| 45 |
| 46 |
| 47 class OAuth2ServiceAccountAuth(AuthHandler): |
| 48 |
| 49 capability = ['google-oauth2', 's3'] |
| 50 |
| 51 def __init__(self, path, config, provider): |
| 52 if (provider.name == 'google' |
| 53 and config.has_option('Credentials', 'gs_service_client_id') |
| 54 and config.has_option('Credentials', 'gs_service_key_file')): |
| 55 self.oauth2_client = oauth2_helper.OAuth2ClientFromBotoConfig(config, |
| 56 cred_type=oauth2_client.CredTypes.OAUTH2_SERVICE_ACCOUNT) |
| 57 |
| 58 # If we make it to this point, then we will later attempt to authenticate |
| 59 # as a service account based on how the boto auth plugins work. This is |
| 60 # global so that command.py can access this value once it's set. |
| 61 # TODO: replace this approach with a way to get the current plugin |
| 62 # from boto so that we don't have to have global variables. |
| 63 global IS_SERVICE_ACCOUNT |
| 64 IS_SERVICE_ACCOUNT = True |
| 65 else: |
| 66 raise NotReadyToAuthenticate() |
| 67 |
| 68 def add_auth(self, http_request): |
| 69 http_request.headers['Authorization'] = \ |
| 70 self.oauth2_client.GetAuthorizationHeader() |
| 71 |
| OLD | NEW |