OLD | NEW |
1 # Copyright 2014 Google Inc. All rights reserved. | 1 # Copyright (C) 2012 Google Inc. |
2 # | 2 # |
3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
4 # you may not use this file except in compliance with 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 | 5 # You may obtain a copy of the License at |
6 # | 6 # |
7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
8 # | 8 # |
9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 # See the License for the specific language governing permissions and | 12 # See the License for the specific language governing permissions and |
13 # limitations under the License. | 13 # limitations under the License. |
14 | 14 |
15 """Utilities for Google Compute Engine | 15 """Utilities for Google Compute Engine |
16 | 16 |
17 Utilities for making it easier to use OAuth 2.0 on Google Compute Engine. | 17 Utilities for making it easier to use OAuth 2.0 on Google Compute Engine. |
18 """ | 18 """ |
19 | 19 |
20 __author__ = 'jcgregorio@google.com (Joe Gregorio)' | 20 __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
21 | 21 |
22 import json | 22 import httplib2 |
23 import logging | 23 import logging |
24 from third_party.six.moves import urllib | 24 import uritemplate |
25 | 25 |
26 from third_party.oauth2client import util | 26 from oauth2client import util |
27 from third_party.oauth2client.client import AccessTokenRefreshError | 27 from oauth2client.anyjson import simplejson |
28 from third_party.oauth2client.client import AssertionCredentials | 28 from oauth2client.client import AccessTokenRefreshError |
| 29 from oauth2client.client import AssertionCredentials |
29 | 30 |
30 logger = logging.getLogger(__name__) | 31 logger = logging.getLogger(__name__) |
31 | 32 |
32 # URI Template for the endpoint that returns access_tokens. | 33 # URI Template for the endpoint that returns access_tokens. |
33 META = ('http://metadata.google.internal/0.1/meta-data/service-accounts/' | 34 META = ('http://metadata.google.internal/0.1/meta-data/service-accounts/' |
34 'default/acquire{?scope}') | 35 'default/acquire{?scope}') |
35 | 36 |
36 | 37 |
37 class AppAssertionCredentials(AssertionCredentials): | 38 class AppAssertionCredentials(AssertionCredentials): |
38 """Credentials object for Compute Engine Assertion Grants | 39 """Credentials object for Compute Engine Assertion Grants |
(...skipping 10 matching lines...) Expand all Loading... |
49 | 50 |
50 @util.positional(2) | 51 @util.positional(2) |
51 def __init__(self, scope, **kwargs): | 52 def __init__(self, scope, **kwargs): |
52 """Constructor for AppAssertionCredentials | 53 """Constructor for AppAssertionCredentials |
53 | 54 |
54 Args: | 55 Args: |
55 scope: string or iterable of strings, scope(s) of the credentials being | 56 scope: string or iterable of strings, scope(s) of the credentials being |
56 requested. | 57 requested. |
57 """ | 58 """ |
58 self.scope = util.scopes_to_string(scope) | 59 self.scope = util.scopes_to_string(scope) |
59 self.kwargs = kwargs | |
60 | 60 |
61 # Assertion type is no longer used, but still in the parent class signature. | 61 # Assertion type is no longer used, but still in the parent class signature. |
62 super(AppAssertionCredentials, self).__init__(None) | 62 super(AppAssertionCredentials, self).__init__(None) |
63 | 63 |
64 @classmethod | 64 @classmethod |
65 def from_json(cls, json_data): | 65 def from_json(cls, json): |
66 data = json.loads(json_data) | 66 data = simplejson.loads(json) |
67 return AppAssertionCredentials(data['scope']) | 67 return AppAssertionCredentials(data['scope']) |
68 | 68 |
69 def _refresh(self, http_request): | 69 def _refresh(self, http_request): |
70 """Refreshes the access_token. | 70 """Refreshes the access_token. |
71 | 71 |
72 Skip all the storage hoops and just refresh using the API. | 72 Skip all the storage hoops and just refresh using the API. |
73 | 73 |
74 Args: | 74 Args: |
75 http_request: callable, a callable that matches the method signature of | 75 http_request: callable, a callable that matches the method signature of |
76 httplib2.Http.request, used to make the refresh request. | 76 httplib2.Http.request, used to make the refresh request. |
77 | 77 |
78 Raises: | 78 Raises: |
79 AccessTokenRefreshError: When the refresh fails. | 79 AccessTokenRefreshError: When the refresh fails. |
80 """ | 80 """ |
81 query = '?scope=%s' % urllib.parse.quote(self.scope, '') | 81 uri = uritemplate.expand(META, {'scope': self.scope}) |
82 uri = META.replace('{?scope}', query) | |
83 response, content = http_request(uri) | 82 response, content = http_request(uri) |
84 if response.status == 200: | 83 if response.status == 200: |
85 try: | 84 try: |
86 d = json.loads(content) | 85 d = simplejson.loads(content) |
87 except Exception as e: | 86 except StandardError, e: |
88 raise AccessTokenRefreshError(str(e)) | 87 raise AccessTokenRefreshError(str(e)) |
89 self.access_token = d['accessToken'] | 88 self.access_token = d['accessToken'] |
90 else: | 89 else: |
91 if response.status == 404: | |
92 content += (' This can occur if a VM was created' | |
93 ' with no service account or scopes.') | |
94 raise AccessTokenRefreshError(content) | 90 raise AccessTokenRefreshError(content) |
95 | |
96 @property | |
97 def serialization_data(self): | |
98 raise NotImplementedError( | |
99 'Cannot serialize credentials for GCE service accounts.') | |
100 | |
101 def create_scoped_required(self): | |
102 return not self.scope | |
103 | |
104 def create_scoped(self, scopes): | |
105 return AppAssertionCredentials(scopes, **self.kwargs) | |
OLD | NEW |