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 'string': [
39 'client_id',
40 'client_secret'
41 ]
42 },
43 TYPE_INSTALLED: {
44 'required': [
45 'client_id',
46 'client_secret',
47 'redirect_uris',
48 'auth_uri',
49 'token_uri'],
50 'string': [
51 'client_id',
52 'client_secret'
53 ]
54 }
55 }
56
58 """Base error for this module."""
59 pass
60
61
63 """Format of ClientSecrets file is invalid."""
64 pass
65
66
68 if obj is None or len(obj) != 1:
69 raise InvalidClientSecretsError('Invalid file format.')
70 client_type = obj.keys()[0]
71 if client_type not in VALID_CLIENT.keys():
72 raise InvalidClientSecretsError('Unknown client type: %s.' % client_type)
73 client_info = obj[client_type]
74 for prop_name in VALID_CLIENT[client_type]['required']:
75 if prop_name not in client_info:
76 raise InvalidClientSecretsError(
77 'Missing property "%s" in a client type of "%s".' % (prop_name,
78 client_type))
79 for prop_name in VALID_CLIENT[client_type]['string']:
80 if client_info[prop_name].startswith('[['):
81 raise InvalidClientSecretsError(
82 'Property "%s" is not configured.' % prop_name)
83 return client_type, client_info
84
85
89
90
94
95
106