OLD | NEW |
1 # Copyright (C) 2011 Google Inc. | 1 # Copyright 2014 Google Inc. All rights reserved. |
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 reading OAuth 2.0 client secret files. | 15 """Utilities for reading OAuth 2.0 client secret files. |
16 | 16 |
17 A client_secrets.json file contains all the information needed to interact with | 17 A client_secrets.json file contains all the information needed to interact with |
18 an OAuth 2.0 protected service. | 18 an OAuth 2.0 protected service. |
19 """ | 19 """ |
20 | 20 |
21 __author__ = 'jcgregorio@google.com (Joe Gregorio)' | 21 __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
22 | 22 |
| 23 import json |
23 | 24 |
24 from anyjson import simplejson | 25 from third_party import six |
| 26 |
25 | 27 |
26 # Properties that make a client_secrets.json file valid. | 28 # Properties that make a client_secrets.json file valid. |
27 TYPE_WEB = 'web' | 29 TYPE_WEB = 'web' |
28 TYPE_INSTALLED = 'installed' | 30 TYPE_INSTALLED = 'installed' |
29 | 31 |
30 VALID_CLIENT = { | 32 VALID_CLIENT = { |
31 TYPE_WEB: { | 33 TYPE_WEB: { |
32 'required': [ | 34 'required': [ |
33 'client_id', | 35 'client_id', |
34 'client_secret', | 36 'client_secret', |
(...skipping 26 matching lines...) Expand all Loading... |
61 """Base error for this module.""" | 63 """Base error for this module.""" |
62 pass | 64 pass |
63 | 65 |
64 | 66 |
65 class InvalidClientSecretsError(Error): | 67 class InvalidClientSecretsError(Error): |
66 """Format of ClientSecrets file is invalid.""" | 68 """Format of ClientSecrets file is invalid.""" |
67 pass | 69 pass |
68 | 70 |
69 | 71 |
70 def _validate_clientsecrets(obj): | 72 def _validate_clientsecrets(obj): |
71 if obj is None or len(obj) != 1: | 73 _INVALID_FILE_FORMAT_MSG = ( |
72 raise InvalidClientSecretsError('Invalid file format.') | 74 'Invalid file format. See ' |
73 client_type = obj.keys()[0] | 75 'https://developers.google.com/api-client-library/' |
74 if client_type not in VALID_CLIENT.keys(): | 76 'python/guide/aaa_client_secrets') |
75 raise InvalidClientSecretsError('Unknown client type: %s.' % client_type) | 77 |
| 78 if obj is None: |
| 79 raise InvalidClientSecretsError(_INVALID_FILE_FORMAT_MSG) |
| 80 if len(obj) != 1: |
| 81 raise InvalidClientSecretsError( |
| 82 _INVALID_FILE_FORMAT_MSG + ' ' |
| 83 'Expected a JSON object with a single property for a "web" or ' |
| 84 '"installed" application') |
| 85 client_type = tuple(obj)[0] |
| 86 if client_type not in VALID_CLIENT: |
| 87 raise InvalidClientSecretsError('Unknown client type: %s.' % (client_type,)) |
76 client_info = obj[client_type] | 88 client_info = obj[client_type] |
77 for prop_name in VALID_CLIENT[client_type]['required']: | 89 for prop_name in VALID_CLIENT[client_type]['required']: |
78 if prop_name not in client_info: | 90 if prop_name not in client_info: |
79 raise InvalidClientSecretsError( | 91 raise InvalidClientSecretsError( |
80 'Missing property "%s" in a client type of "%s".' % (prop_name, | 92 'Missing property "%s" in a client type of "%s".' % (prop_name, |
81 client_type)) | 93 client_type)) |
82 for prop_name in VALID_CLIENT[client_type]['string']: | 94 for prop_name in VALID_CLIENT[client_type]['string']: |
83 if client_info[prop_name].startswith('[['): | 95 if client_info[prop_name].startswith('[['): |
84 raise InvalidClientSecretsError( | 96 raise InvalidClientSecretsError( |
85 'Property "%s" is not configured.' % prop_name) | 97 'Property "%s" is not configured.' % prop_name) |
86 return client_type, client_info | 98 return client_type, client_info |
87 | 99 |
88 | 100 |
89 def load(fp): | 101 def load(fp): |
90 obj = simplejson.load(fp) | 102 obj = json.load(fp) |
91 return _validate_clientsecrets(obj) | 103 return _validate_clientsecrets(obj) |
92 | 104 |
93 | 105 |
94 def loads(s): | 106 def loads(s): |
95 obj = simplejson.loads(s) | 107 obj = json.loads(s) |
96 return _validate_clientsecrets(obj) | 108 return _validate_clientsecrets(obj) |
97 | 109 |
98 | 110 |
99 def _loadfile(filename): | 111 def _loadfile(filename): |
100 try: | 112 try: |
101 fp = file(filename, 'r') | 113 with open(filename, 'r') as fp: |
102 try: | 114 obj = json.load(fp) |
103 obj = simplejson.load(fp) | |
104 finally: | |
105 fp.close() | |
106 except IOError: | 115 except IOError: |
107 raise InvalidClientSecretsError('File not found: "%s"' % filename) | 116 raise InvalidClientSecretsError('File not found: "%s"' % filename) |
108 return _validate_clientsecrets(obj) | 117 return _validate_clientsecrets(obj) |
109 | 118 |
110 | 119 |
111 def loadfile(filename, cache=None): | 120 def loadfile(filename, cache=None): |
112 """Loading of client_secrets JSON file, optionally backed by a cache. | 121 """Loading of client_secrets JSON file, optionally backed by a cache. |
113 | 122 |
114 Typical cache storage would be App Engine memcache service, | 123 Typical cache storage would be App Engine memcache service, |
115 but you can pass in any other cache client that implements | 124 but you can pass in any other cache client that implements |
116 these methods: | 125 these methods: |
117 - get(key, namespace=ns) | |
118 - set(key, value, namespace=ns) | |
119 | 126 |
120 Usage: | 127 * ``get(key, namespace=ns)`` |
| 128 * ``set(key, value, namespace=ns)`` |
| 129 |
| 130 Usage:: |
| 131 |
121 # without caching | 132 # without caching |
122 client_type, client_info = loadfile('secrets.json') | 133 client_type, client_info = loadfile('secrets.json') |
123 # using App Engine memcache service | 134 # using App Engine memcache service |
124 from google.appengine.api import memcache | 135 from google.appengine.api import memcache |
125 client_type, client_info = loadfile('secrets.json', cache=memcache) | 136 client_type, client_info = loadfile('secrets.json', cache=memcache) |
126 | 137 |
127 Args: | 138 Args: |
128 filename: string, Path to a client_secrets.json file on a filesystem. | 139 filename: string, Path to a client_secrets.json file on a filesystem. |
129 cache: An optional cache service client that implements get() and set() | 140 cache: An optional cache service client that implements get() and set() |
130 methods. If not specified, the file is always being loaded from | 141 methods. If not specified, the file is always being loaded from |
(...skipping 12 matching lines...) Expand all Loading... |
143 | 154 |
144 if not cache: | 155 if not cache: |
145 return _loadfile(filename) | 156 return _loadfile(filename) |
146 | 157 |
147 obj = cache.get(filename, namespace=_SECRET_NAMESPACE) | 158 obj = cache.get(filename, namespace=_SECRET_NAMESPACE) |
148 if obj is None: | 159 if obj is None: |
149 client_type, client_info = _loadfile(filename) | 160 client_type, client_info = _loadfile(filename) |
150 obj = {client_type: client_info} | 161 obj = {client_type: client_info} |
151 cache.set(filename, obj, namespace=_SECRET_NAMESPACE) | 162 cache.set(filename, obj, namespace=_SECRET_NAMESPACE) |
152 | 163 |
153 return obj.iteritems().next() | 164 return next(six.iteritems(obj)) |
OLD | NEW |