Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / chromevox / tools / webstore_extension_util.py
blobe0f8490f963a0bb6a386e32680251a17e9889e72
1 #!/usr/bin/env python
3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 '''A set of utilities to interface with the Chrome Webstore API.'''
9 import SimpleHTTPServer
10 import SocketServer
11 import httplib
12 import json
13 import os
14 import re
15 import sys
16 import thread
17 import urllib
18 import webbrowser
20 PROJECT_ARGS = {
21 'client_id': ('937534751394-gbj5334v9144c57qjqghl7d283plj5r4'
22 '.apps.googleusercontent.com'),
23 'grant_type': 'authorization_code',
24 'redirect_uri': 'http://localhost:8000'
27 # Persists across all utility methods for authentication.
28 g_auth_code = None
29 g_oauth_token = None
31 # The app id to use for all utility methods.
32 g_app_id = ''
34 # Constants.
35 PORT = 8000
36 OAUTH_DOMAIN = 'accounts.google.com'
37 OAUTH_AUTH_COMMAND = '/o/oauth2/auth'
38 OAUTH_TOKEN_COMMAND = '/o/oauth2/token'
39 WEBSTORE_API_SCOPE = 'https://www.googleapis.com/auth/chromewebstore'
40 API_ENDPOINT_DOMAIN = 'www.googleapis.com'
42 def GetUploadStatusCommand():
43 global g_app_id
44 return '/chromewebstore/v1.1/items/%s?projection=draft' % g_app_id
46 def GetPublishCommand():
47 global g_app_id
48 return '/chromewebstore/v1.1/items/%s/publish' % g_app_id
50 def GetUploadCommand():
51 global g_app_id
52 return '/upload/chromewebstore/v1.1/items/%s' % g_app_id
54 class CodeRequestHandler(SocketServer.StreamRequestHandler):
55 def handle(self):
56 content = self.rfile.readline()
57 self.server.code = re.search('code=(.*) ', content).groups()[0]
58 self.rfile.close()
60 def GetAuthCode():
61 global g_auth_code
62 if g_auth_code:
63 return g_auth_code
65 Handler = CodeRequestHandler
66 httpd = SocketServer.TCPServer(("", PORT), Handler)
67 query = '&'.join(['response_type=code',
68 'scope=%s' % WEBSTORE_API_SCOPE,
69 'client_id=%(client_id)s' % PROJECT_ARGS,
70 'redirect_uri=%(redirect_uri)s' % PROJECT_ARGS])
71 auth_url = 'https://%s%s?%s' % (OAUTH_DOMAIN, OAUTH_AUTH_COMMAND, query)
72 print 'Navigating to %s' % auth_url
73 webbrowser.open(auth_url)
74 httpd.handle_request()
75 httpd.server_close()
76 g_auth_code = httpd.code
77 return g_auth_code
79 def GetOauthToken(code, client_secret):
80 global g_oauth_token
81 if g_oauth_token:
82 return g_oauth_token
84 PROJECT_ARGS['code'] = code
85 PROJECT_ARGS['client_secret'] = client_secret
86 body = urllib.urlencode(PROJECT_ARGS)
87 conn = httplib.HTTPSConnection(OAUTH_DOMAIN)
88 conn.putrequest('POST', OAUTH_TOKEN_COMMAND)
89 conn.putheader('content-type', 'application/x-www-form-urlencoded')
90 conn.putheader('content-length', len(body))
91 conn.endheaders()
92 conn.send(body)
93 content = conn.getresponse().read()
94 conn.close()
95 g_oauth_token = json.loads(content)
96 return g_oauth_token
98 def GetPopulatedHeader(client_secret):
99 code = GetAuthCode()
100 access_token = GetOauthToken(code, client_secret)
102 url = 'www.googleapis.com'
104 return {'Authorization': 'Bearer %(access_token)s' % access_token,
105 'x-goog-api-version': 2,
106 'Content-Length': 0
109 def SendGetCommand(command, client_secret):
110 headers = GetPopulatedHeader(client_secret)
111 conn = httplib.HTTPSConnection(API_ENDPOINT_DOMAIN)
112 conn.request('GET', command, '', headers)
113 r = conn.getresponse()
114 conn.close()
115 return r
117 def SendPostCommand(command, client_secret, header_additions = {}, body=None):
118 headers = GetPopulatedHeader(client_secret)
119 headers = dict(headers.items() + header_additions.items())
120 conn = httplib.HTTPSConnection(API_ENDPOINT_DOMAIN)
121 conn.request('POST', command, body, headers)
122 r = conn.getresponse()
123 conn.close()
124 return r
126 def GetUploadStatus(client_secret):
127 '''Gets the status of a previous upload.
128 Args:
129 client_secret ChromeVox's client secret creds.
131 return SendGetCommand(GetUploadStatusCommand(), client_secret)
133 # httplib fails to persist the connection during upload; use curl instead.
134 def PostUpload(file, client_secret):
135 '''Posts an uploaded version of ChromeVox.
136 Args:
137 file A string path to the ChromeVox extension zip.
138 client_secret ChromeVox's client secret creds.
140 header = GetPopulatedHeader(client_secret)
141 curl_command = ' '.join(['curl',
142 '-H "Authorization: %(Authorization)s"' % header,
143 '-H "x-goog-api-version: 2"',
144 '-X PUT',
145 '-T %s' % file,
146 '-v',
147 'https://%s%s' % (API_ENDPOINT_DOMAIN,
148 GetUploadCommand())])
150 print 'Running %s' % curl_command
151 if os.system(curl_command) != 0:
152 sys.exit(-1)
154 def PostPublishTrustedTesters(client_secret):
155 '''Publishes a previously uploaded ChromeVox extension to trusted testers.
156 Args:
157 client_secret ChromeVox's client secret creds.
159 return SendPostCommand(GetPublishCommand(),
160 client_secret,
161 { 'publishTarget': 'trustedTesters'})
163 def PostPublish(client_secret):
164 '''Publishes a previously uploaded ChromeVox extension publically.
165 Args:
166 client_secret ChromeVox's client secret creds.
168 return SendPostCommand(GetPublishCommand(), client_secret)