blob: 8e528f2ade47fd1b1a1290d167e044e3d3755008 [file] [log] [blame]
[email protected]13a533682012-09-17 23:48:431#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Python API for retrieving API keys.
7
[email protected]6a7caec2013-07-08 19:57:198Note that this does not have the exact same semantics as the C++ API
9in google_api_keys.h, since it does not have access to gyp variables
10or preprocessor defines.
[email protected]13a533682012-09-17 23:48:4311"""
12
13import os
14import re
15import sys
16
17
18# The token returned when an API key is unset.
19DUMMY_TOKEN = 'dummytoken'
20
21
22def _GetTokenFromOfficialFile(token_name):
23 """Parses the token from the official file if it exists, else returns None."""
[email protected]a769b6cc2012-09-18 19:36:3324 official_path = os.path.join(os.path.dirname(__file__),
[email protected]13a533682012-09-17 23:48:4325 'internal/google_chrome_api_keys.h')
26 if not os.path.isfile(official_path):
27 return None
28
29 line_regexp = '^#define\s*%s\s*"([^"]+)"' % token_name
30 line_pattern = re.compile(line_regexp)
31 def ParseLine(current_line):
32 result = line_pattern.match(current_line)
33 if result:
34 return result.group(1)
35 else:
36 return None
37
38 with open(official_path) as f:
39 current_line = ''
40 for line in f:
41 if line.endswith('\\\n'):
42 current_line += line[:-2]
43 else:
44 # Last line in multi-line #define, or a line that is not a
45 # continuation line.
46 current_line += line
47 token = ParseLine(current_line)
48 if token:
49 if current_line.count('"') != 2:
50 raise Exception(
51 'Embedded quotes and multi-line strings are not supported.')
52 return token
53 current_line = ''
54 return None
55
56
57def _GetToken(token_name):
58 """Returns the API token with the given name, or DUMMY_TOKEN by default."""
59 if token_name in os.environ:
60 return os.environ[token_name]
61 token = _GetTokenFromOfficialFile(token_name)
62 if token:
63 return token
64 else:
65 return DUMMY_TOKEN
66
67
68def GetAPIKey():
69 """Returns the simple API key."""
70 return _GetToken('GOOGLE_API_KEY')
71
72
dvh00cf1f62015-11-06 22:44:2573def GetAPIKeyPhysicalWebTest():
74 """Returns the API key to test Physical Web service."""
75 return _GetToken('GOOGLE_API_KEY_PHYSICAL_WEB_TEST')
76
77
[email protected]13a533682012-09-17 23:48:4378def GetClientID(client_name):
79 """Returns the OAuth 2.0 client ID for the client of the given name."""
80 return _GetToken('GOOGLE_CLIENT_ID_%s' % client_name)
81
82
83def GetClientSecret(client_name):
84 """Returns the OAuth 2.0 client secret for the client of the given name."""
85 return _GetToken('GOOGLE_CLIENT_SECRET_%s' % client_name)
86
87
88if __name__ == "__main__":
89 print 'GOOGLE_API_KEY=%s' % GetAPIKey()
90 print 'GOOGLE_CLIENT_ID_MAIN=%s' % GetClientID('MAIN')
91 print 'GOOGLE_CLIENT_SECRET_MAIN=%s' % GetClientSecret('MAIN')
92 print 'GOOGLE_CLIENT_ID_CLOUD_PRINT=%s' % GetClientID('CLOUD_PRINT')
93 print 'GOOGLE_CLIENT_SECRET_CLOUD_PRINT=%s' % GetClientSecret('CLOUD_PRINT')
94 print 'GOOGLE_CLIENT_ID_REMOTING=%s' % GetClientID('REMOTING')
95 print 'GOOGLE_CLIENT_SECRET_REMOTING=%s' % GetClientSecret('REMOTING')
[email protected]d66d81742013-08-16 05:18:3896 print 'GOOGLE_CLIENT_ID_REMOTING_HOST=%s' % GetClientID('REMOTING_HOST')
97 print 'GOOGLE_CLIENT_SECRET_REMOTING_HOST=%s' % GetClientSecret(
98 'REMOTING_HOST')
[email protected]6e026b62013-08-17 00:47:0499 print 'GOOGLE_CLIENT_ID_REMOTING_IDENTITY_API=%s' %GetClientID(
100 'REMOTING_IDENTITY_API')