Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: third_party/oauth2client/clientsecrets.py

Issue 1094533003: Revert of Upgrade 3rd packages (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/oauth2client/client.py ('k') | third_party/oauth2client/crypt.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 Google Inc. All rights reserved. 1 # Copyright (C) 2011 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 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
24 23
25 from third_party import six 24 from anyjson import simplejson
26
27 25
28 # Properties that make a client_secrets.json file valid. 26 # Properties that make a client_secrets.json file valid.
29 TYPE_WEB = 'web' 27 TYPE_WEB = 'web'
30 TYPE_INSTALLED = 'installed' 28 TYPE_INSTALLED = 'installed'
31 29
32 VALID_CLIENT = { 30 VALID_CLIENT = {
33 TYPE_WEB: { 31 TYPE_WEB: {
34 'required': [ 32 'required': [
35 'client_id', 33 'client_id',
36 'client_secret', 34 'client_secret',
(...skipping 26 matching lines...) Expand all
63 """Base error for this module.""" 61 """Base error for this module."""
64 pass 62 pass
65 63
66 64
67 class InvalidClientSecretsError(Error): 65 class InvalidClientSecretsError(Error):
68 """Format of ClientSecrets file is invalid.""" 66 """Format of ClientSecrets file is invalid."""
69 pass 67 pass
70 68
71 69
72 def _validate_clientsecrets(obj): 70 def _validate_clientsecrets(obj):
73 _INVALID_FILE_FORMAT_MSG = ( 71 if obj is None or len(obj) != 1:
74 'Invalid file format. See ' 72 raise InvalidClientSecretsError('Invalid file format.')
75 'https://developers.google.com/api-client-library/' 73 client_type = obj.keys()[0]
76 'python/guide/aaa_client_secrets') 74 if client_type not in VALID_CLIENT.keys():
77 75 raise InvalidClientSecretsError('Unknown client type: %s.' % client_type)
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,))
88 client_info = obj[client_type] 76 client_info = obj[client_type]
89 for prop_name in VALID_CLIENT[client_type]['required']: 77 for prop_name in VALID_CLIENT[client_type]['required']:
90 if prop_name not in client_info: 78 if prop_name not in client_info:
91 raise InvalidClientSecretsError( 79 raise InvalidClientSecretsError(
92 'Missing property "%s" in a client type of "%s".' % (prop_name, 80 'Missing property "%s" in a client type of "%s".' % (prop_name,
93 client_type)) 81 client_type))
94 for prop_name in VALID_CLIENT[client_type]['string']: 82 for prop_name in VALID_CLIENT[client_type]['string']:
95 if client_info[prop_name].startswith('[['): 83 if client_info[prop_name].startswith('[['):
96 raise InvalidClientSecretsError( 84 raise InvalidClientSecretsError(
97 'Property "%s" is not configured.' % prop_name) 85 'Property "%s" is not configured.' % prop_name)
98 return client_type, client_info 86 return client_type, client_info
99 87
100 88
101 def load(fp): 89 def load(fp):
102 obj = json.load(fp) 90 obj = simplejson.load(fp)
103 return _validate_clientsecrets(obj) 91 return _validate_clientsecrets(obj)
104 92
105 93
106 def loads(s): 94 def loads(s):
107 obj = json.loads(s) 95 obj = simplejson.loads(s)
108 return _validate_clientsecrets(obj) 96 return _validate_clientsecrets(obj)
109 97
110 98
111 def _loadfile(filename): 99 def _loadfile(filename):
112 try: 100 try:
113 with open(filename, 'r') as fp: 101 fp = file(filename, 'r')
114 obj = json.load(fp) 102 try:
103 obj = simplejson.load(fp)
104 finally:
105 fp.close()
115 except IOError: 106 except IOError:
116 raise InvalidClientSecretsError('File not found: "%s"' % filename) 107 raise InvalidClientSecretsError('File not found: "%s"' % filename)
117 return _validate_clientsecrets(obj) 108 return _validate_clientsecrets(obj)
118 109
119 110
120 def loadfile(filename, cache=None): 111 def loadfile(filename, cache=None):
121 """Loading of client_secrets JSON file, optionally backed by a cache. 112 """Loading of client_secrets JSON file, optionally backed by a cache.
122 113
123 Typical cache storage would be App Engine memcache service, 114 Typical cache storage would be App Engine memcache service,
124 but you can pass in any other cache client that implements 115 but you can pass in any other cache client that implements
125 these methods: 116 these methods:
117 - get(key, namespace=ns)
118 - set(key, value, namespace=ns)
126 119
127 * ``get(key, namespace=ns)`` 120 Usage:
128 * ``set(key, value, namespace=ns)``
129
130 Usage::
131
132 # without caching 121 # without caching
133 client_type, client_info = loadfile('secrets.json') 122 client_type, client_info = loadfile('secrets.json')
134 # using App Engine memcache service 123 # using App Engine memcache service
135 from google.appengine.api import memcache 124 from google.appengine.api import memcache
136 client_type, client_info = loadfile('secrets.json', cache=memcache) 125 client_type, client_info = loadfile('secrets.json', cache=memcache)
137 126
138 Args: 127 Args:
139 filename: string, Path to a client_secrets.json file on a filesystem. 128 filename: string, Path to a client_secrets.json file on a filesystem.
140 cache: An optional cache service client that implements get() and set() 129 cache: An optional cache service client that implements get() and set()
141 methods. If not specified, the file is always being loaded from 130 methods. If not specified, the file is always being loaded from
(...skipping 12 matching lines...) Expand all
154 143
155 if not cache: 144 if not cache:
156 return _loadfile(filename) 145 return _loadfile(filename)
157 146
158 obj = cache.get(filename, namespace=_SECRET_NAMESPACE) 147 obj = cache.get(filename, namespace=_SECRET_NAMESPACE)
159 if obj is None: 148 if obj is None:
160 client_type, client_info = _loadfile(filename) 149 client_type, client_info = _loadfile(filename)
161 obj = {client_type: client_info} 150 obj = {client_type: client_info}
162 cache.set(filename, obj, namespace=_SECRET_NAMESPACE) 151 cache.set(filename, obj, namespace=_SECRET_NAMESPACE)
163 152
164 return next(six.iteritems(obj)) 153 return obj.iteritems().next()
OLDNEW
« no previous file with comments | « third_party/oauth2client/client.py ('k') | third_party/oauth2client/crypt.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698