blob: 4e2fb37e7fad8f858ee04bddbd49f35edc37a1d4 [file] [log] [blame]
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +00001#!/usr/bin/env python
2#-*- coding: utf-8 -*-
3# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS. All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
10
11"""This script request an access token from the appengine running the dashboard.
12
13 The script is intended to be run manually whenever we wish to change which
14 dashboard administrator we act on behalf of when running the
15 track_coverage.py script. For example, this will be useful if the current
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000016 dashboard administrator leaves the project. This script can also be used to
17 launch a new dashboard if that is desired.
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000018
19 This script should be run on the build bot which runs the track_coverage.py
20 script. This script will present a link during its execution, which the new
21 administrator should follow and then click approve on the web page that
22 appears. The new administrator should have admin rights on the coverage
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000023 dashboard, otherwise the track_* scripts will not work.
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000024
25 If successful, this script will write the access token to a file access.token
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000026 in the current directory, which later can be read by the track_* scripts.
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000027 The token is stored in string form (as reported by the web server) using the
phoglund@webrtc.org914ef272012-02-27 15:42:25 +000028 shelve module. The consumer secret passed in as an argument to this script
29 will also similarly be stored in a file consumer.secret. The shelve keys
30 will be 'access_token' and 'consumer_secret', respectively.
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000031"""
32
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000033import shelve
34import sys
35import urlparse
36import oauth2 as oauth
37
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000038import constants
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000039
phoglund@webrtc.org86ce46d2012-02-06 10:55:12 +000040
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000041class FailedToRequestPermissionException(Exception):
42 pass
43
44
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000045def _ensure_token_response_is_200(response, queried_url, token_type):
46 if response.status != 200:
47 raise FailedToRequestPermissionException('Failed to request %s from %s: '
48 'received status %d, reason %s.' %
49 (token_type,
50 queried_url,
51 response.status,
52 response.reason))
53
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000054
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000055def _request_unauthorized_token(consumer, request_token_url):
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000056 """Requests the initial token from the dashboard service.
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000057
58 Given that the response from the server is correct, we will return a
59 dictionary containing oauth_token and oauth_token_secret mapped to the
60 token and secret value, respectively.
61 """
62 client = oauth.Client(consumer)
63
64 try:
65 response, content = client.request(request_token_url, 'POST')
66 except AttributeError as error:
67 # This catch handler is here since we'll get very confusing messages
68 # if the target server is down for some reason.
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000069 raise FailedToRequestPermissionException('Failed to request token: '
70 'the dashboard is likely down.',
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000071 error)
72
73 _ensure_token_response_is_200(response, request_token_url,
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000074 'unauthorized token')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000075
76 return dict(urlparse.parse_qsl(content))
77
78
79def _ask_user_to_authorize_us(unauthorized_token):
80 """This function will block until the user enters y + newline."""
81 print 'Go to the following link in your browser:'
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000082 print '%s?oauth_token=%s' % (constants.AUTHORIZE_TOKEN_URL,
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000083 unauthorized_token['oauth_token'])
84
85 accepted = 'n'
86 while accepted.lower() != 'y':
87 accepted = raw_input('Have you authorized me yet? (y/n) ')
88
89
90def _request_access_token(consumer, unauthorized_token):
91 token = oauth.Token(unauthorized_token['oauth_token'],
92 unauthorized_token['oauth_token_secret'])
93 client = oauth.Client(consumer, token)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000094 response, content = client.request(constants.ACCESS_TOKEN_URL, 'POST')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000095
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000096 _ensure_token_response_is_200(response, constants.ACCESS_TOKEN_URL,
97 'access token')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000098
99 return content
100
101
102def _write_access_token_to_file(access_token, filename):
103 output = shelve.open(filename)
104 output['access_token'] = access_token
105 output.close()
106
107 print 'Wrote the access token to the file %s.' % filename
108
109
phoglund@webrtc.org914ef272012-02-27 15:42:25 +0000110def _write_consumer_secret_to_file(consumer_secret, filename):
111 output = shelve.open(filename)
112 output['consumer_secret'] = consumer_secret
113 output.close()
114
115 print 'Wrote the consumer secret to the file %s.' % filename
116
117
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000118def _main():
119 if len(sys.argv) != 2:
120 print ('Usage: %s <consumer secret>.\n\nThe consumer secret is an OAuth '
phoglund@webrtc.org914ef272012-02-27 15:42:25 +0000121 'concept and is obtained from the Google Accounts domain dashboard.'
122 % sys.argv[0])
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000123 return
124
125 consumer_secret = sys.argv[1]
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000126 consumer = oauth.Consumer(constants.CONSUMER_KEY, consumer_secret)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000127
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000128 unauthorized_token = _request_unauthorized_token(consumer,
129 constants.REQUEST_TOKEN_URL)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000130
131 _ask_user_to_authorize_us(unauthorized_token)
132
133 access_token_string = _request_access_token(consumer, unauthorized_token)
134
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000135 _write_access_token_to_file(access_token_string, constants.ACCESS_TOKEN_FILE)
phoglund@webrtc.org914ef272012-02-27 15:42:25 +0000136 _write_consumer_secret_to_file(consumer_secret,
137 constants.CONSUMER_SECRET_FILE)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000138
139if __name__ == '__main__':
140 _main()