File: webstore_extension_util.py

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (184 lines) | stat: -rwxr-xr-x 5,029 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python

# Copyright 2014 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
'''A set of utilities to interface with the Chrome Webstore API.'''

import SimpleHTTPServer
import SocketServer
import httplib
import json
import os
import re
import sys
import thread
import urllib
import webbrowser

PROJECT_ARGS = {
    'client_id': ('937534751394-gbj5334v9144c57qjqghl7d283plj5r4'
                  '.apps.googleusercontent.com'),
    'grant_type':
        'authorization_code',
    'redirect_uri':
        'http://localhost:8000'
}

# Persists across all utility methods for authentication.
g_auth_code = None
g_oauth_token = None

# The app id to use for all utility methods.
g_app_id = ''

# Constants.
PORT = 8000
OAUTH_DOMAIN = 'accounts.google.com'
OAUTH_AUTH_COMMAND = '/o/oauth2/auth'
OAUTH_TOKEN_COMMAND = '/o/oauth2/token'
WEBSTORE_API_SCOPE = 'https://www.googleapis.com/auth/chromewebstore'
API_ENDPOINT_DOMAIN = 'www.googleapis.com'


def GetUploadStatusCommand():
  global g_app_id
  return '/chromewebstore/v1.1/items/%s?projection=draft' % g_app_id


def GetPublishCommand():
  global g_app_id
  return '/chromewebstore/v1.1/items/%s/publish' % g_app_id


def GetUploadCommand():
  global g_app_id
  return '/upload/chromewebstore/v1.1/items/%s' % g_app_id


class CodeRequestHandler(SocketServer.StreamRequestHandler):

  def handle(self):
    content = self.rfile.readline()
    self.server.code = re.search('code=(.*) ', content).groups()[0]
    self.rfile.close()


def GetAuthCode():
  global g_auth_code
  if g_auth_code:
    return g_auth_code

  Handler = CodeRequestHandler
  httpd = SocketServer.TCPServer(("", PORT), Handler)
  query = '&'.join([
      'response_type=code',
      'scope=%s' % WEBSTORE_API_SCOPE,
      'client_id=%(client_id)s' % PROJECT_ARGS,
      'redirect_uri=%(redirect_uri)s' % PROJECT_ARGS
  ])
  auth_url = 'https://%s%s?%s' % (OAUTH_DOMAIN, OAUTH_AUTH_COMMAND, query)
  print 'Navigating to %s' % auth_url
  webbrowser.open(auth_url)
  httpd.handle_request()
  httpd.server_close()
  g_auth_code = httpd.code
  return g_auth_code


def GetOauthToken(code, client_secret):
  global g_oauth_token
  if g_oauth_token:
    return g_oauth_token

  PROJECT_ARGS['code'] = code
  PROJECT_ARGS['client_secret'] = client_secret
  body = urllib.urlencode(PROJECT_ARGS)
  conn = httplib.HTTPSConnection(OAUTH_DOMAIN)
  conn.putrequest('POST', OAUTH_TOKEN_COMMAND)
  conn.putheader('content-type', 'application/x-www-form-urlencoded')
  conn.putheader('content-length', len(body))
  conn.endheaders()
  conn.send(body)
  content = conn.getresponse().read()
  conn.close()
  g_oauth_token = json.loads(content)
  return g_oauth_token


def GetPopulatedHeader(client_secret):
  code = GetAuthCode()
  access_token = GetOauthToken(code, client_secret)

  url = 'www.googleapis.com'

  return {
      'Authorization': 'Bearer %(access_token)s' % access_token,
      'x-goog-api-version': 2,
      'Content-Length': 0
  }


def SendGetCommand(command, client_secret):
  headers = GetPopulatedHeader(client_secret)
  conn = httplib.HTTPSConnection(API_ENDPOINT_DOMAIN)
  conn.request('GET', command, '', headers)
  r = conn.getresponse()
  conn.close()
  return r


def SendPostCommand(command, client_secret, header_additions={}, body=None):
  headers = GetPopulatedHeader(client_secret)
  headers = dict(headers.items() + header_additions.items())
  conn = httplib.HTTPSConnection(API_ENDPOINT_DOMAIN)
  conn.request('POST', command, body, headers)
  r = conn.getresponse()
  conn.close()
  return r


def GetUploadStatus(client_secret):
  '''Gets the status of a previous upload.
  Args:
    client_secret ChromeVox's client secret creds.
  '''
  return SendGetCommand(GetUploadStatusCommand(), client_secret)


# httplib fails to persist the connection during upload; use curl instead.
def PostUpload(file, client_secret):
  '''Posts an uploaded version of ChromeVox.
  Args:
    file A string path to the ChromeVox extension zip.
    client_secret ChromeVox's client secret creds.
  '''
  header = GetPopulatedHeader(client_secret)
  curl_command = ' '.join([
      'curl',
      '-H "Authorization: %(Authorization)s"' % header,
      '-H "x-goog-api-version: 2"', '-X PUT',
      '-T %s' % file, '-v',
      'https://%s%s' % (API_ENDPOINT_DOMAIN, GetUploadCommand())
  ])

  print 'Running %s' % curl_command
  if os.system(curl_command) != 0:
    sys.exit(-1)


def PostPublishTrustedTesters(client_secret):
  '''Publishes a previously uploaded ChromeVox extension to trusted testers.
  Args:
    client_secret ChromeVox's client secret creds.
  '''
  return SendPostCommand(GetPublishCommand(), client_secret,
                         {'publishTarget': 'trustedTesters'})


def PostPublish(client_secret):
  '''Publishes a previously uploaded ChromeVox extension publically.
  Args:
    client_secret ChromeVox's client secret creds.
  '''
  return SendPostCommand(GetPublishCommand(), client_secret)