1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """Utilities for reading OAuth 2.0 client secret files.
16
17 A client_secrets.json file contains all the information needed to interact with
18 an OAuth 2.0 protected service.
19 """
20
21 __author__ = 'jcgregorio@google.com (Joe Gregorio)'
22
23
24 from anyjson import simplejson
25
26
27 TYPE_WEB = 'web'
28 TYPE_INSTALLED = 'installed'
29
30 VALID_CLIENT = {
31 TYPE_WEB: {
32 'required': [
33 'client_id',
34 'client_secret',
35 'redirect_uris',
36 'auth_uri',
37 'token_uri',
38 ],
39 'string': [
40 'client_id',
41 'client_secret',
42 ],
43 },
44 TYPE_INSTALLED: {
45 'required': [
46 'client_id',
47 'client_secret',
48 'redirect_uris',
49 'auth_uri',
50 'token_uri',
51 ],
52 'string': [
53 'client_id',
54 'client_secret',
55 ],
56 },
57 }
58
59
61 """Base error for this module."""
62 pass
63
64
66 """Format of ClientSecrets file is invalid."""
67 pass
68
69
71 if obj is None or len(obj) != 1:
72 raise InvalidClientSecretsError('Invalid file format.')
73 client_type = obj.keys()[0]
74 if client_type not in VALID_CLIENT.keys():
75 raise InvalidClientSecretsError('Unknown client type: %s.' % client_type)
76 client_info = obj[client_type]
77 for prop_name in VALID_CLIENT[client_type]['required']:
78 if prop_name not in client_info:
79 raise InvalidClientSecretsError(
80 'Missing property "%s" in a client type of "%s".' % (prop_name,
81 client_type))
82 for prop_name in VALID_CLIENT[client_type]['string']:
83 if client_info[prop_name].startswith('[['):
84 raise InvalidClientSecretsError(
85 'Property "%s" is not configured.' % prop_name)
86 return client_type, client_info
87
88
92
93
97
98
109
110
112 """Loading of client_secrets JSON file, optionally backed by a cache.
113
114 Typical cache storage would be App Engine memcache service,
115 but you can pass in any other cache client that implements
116 these methods:
117 - get(key, namespace=ns)
118 - set(key, value, namespace=ns)
119
120 Usage:
121 # without caching
122 client_type, client_info = loadfile('secrets.json')
123 # using App Engine memcache service
124 from google.appengine.api import memcache
125 client_type, client_info = loadfile('secrets.json', cache=memcache)
126
127 Args:
128 filename: string, Path to a client_secrets.json file on a filesystem.
129 cache: An optional cache service client that implements get() and set()
130 methods. If not specified, the file is always being loaded from
131 a filesystem.
132
133 Raises:
134 InvalidClientSecretsError: In case of a validation error or some
135 I/O failure. Can happen only on cache miss.
136
137 Returns:
138 (client_type, client_info) tuple, as _loadfile() normally would.
139 JSON contents is validated only during first load. Cache hits are not
140 validated.
141 """
142 _SECRET_NAMESPACE = 'oauth2client:secrets#ns'
143
144 if not cache:
145 return _loadfile(filename)
146
147 obj = cache.get(filename, namespace=_SECRET_NAMESPACE)
148 if obj is None:
149 client_type, client_info = _loadfile(filename)
150 obj = {client_type: client_info}
151 cache.set(filename, obj, namespace=_SECRET_NAMESPACE)
152
153 return obj.iteritems().next()
154