blob: fb97738f6ed7fa1e0b1535ac90ccb41a9e1b3180 [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
33__author__ = 'phoglund@webrtc.org (Patrik Höglund)'
34
35import shelve
36import sys
37import urlparse
38import oauth2 as oauth
39
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000040import constants
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000041
phoglund@webrtc.org86ce46d2012-02-06 10:55:12 +000042
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000043class FailedToRequestPermissionException(Exception):
44 pass
45
46
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000047def _ensure_token_response_is_200(response, queried_url, token_type):
48 if response.status != 200:
49 raise FailedToRequestPermissionException('Failed to request %s from %s: '
50 'received status %d, reason %s.' %
51 (token_type,
52 queried_url,
53 response.status,
54 response.reason))
55
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000056
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000057def _request_unauthorized_token(consumer, request_token_url):
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000058 """Requests the initial token from the dashboard service.
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000059
60 Given that the response from the server is correct, we will return a
61 dictionary containing oauth_token and oauth_token_secret mapped to the
62 token and secret value, respectively.
63 """
64 client = oauth.Client(consumer)
65
66 try:
67 response, content = client.request(request_token_url, 'POST')
68 except AttributeError as error:
69 # This catch handler is here since we'll get very confusing messages
70 # if the target server is down for some reason.
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000071 raise FailedToRequestPermissionException('Failed to request token: '
72 'the dashboard is likely down.',
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000073 error)
74
75 _ensure_token_response_is_200(response, request_token_url,
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000076 'unauthorized token')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000077
78 return dict(urlparse.parse_qsl(content))
79
80
81def _ask_user_to_authorize_us(unauthorized_token):
82 """This function will block until the user enters y + newline."""
83 print 'Go to the following link in your browser:'
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000084 print '%s?oauth_token=%s' % (constants.AUTHORIZE_TOKEN_URL,
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000085 unauthorized_token['oauth_token'])
86
87 accepted = 'n'
88 while accepted.lower() != 'y':
89 accepted = raw_input('Have you authorized me yet? (y/n) ')
90
91
92def _request_access_token(consumer, unauthorized_token):
93 token = oauth.Token(unauthorized_token['oauth_token'],
94 unauthorized_token['oauth_token_secret'])
95 client = oauth.Client(consumer, token)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000096 response, content = client.request(constants.ACCESS_TOKEN_URL, 'POST')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000097
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000098 _ensure_token_response_is_200(response, constants.ACCESS_TOKEN_URL,
99 'access token')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000100
101 return content
102
103
104def _write_access_token_to_file(access_token, filename):
105 output = shelve.open(filename)
106 output['access_token'] = access_token
107 output.close()
108
109 print 'Wrote the access token to the file %s.' % filename
110
111
phoglund@webrtc.org914ef272012-02-27 15:42:25 +0000112def _write_consumer_secret_to_file(consumer_secret, filename):
113 output = shelve.open(filename)
114 output['consumer_secret'] = consumer_secret
115 output.close()
116
117 print 'Wrote the consumer secret to the file %s.' % filename
118
119
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000120def _main():
121 if len(sys.argv) != 2:
122 print ('Usage: %s <consumer secret>.\n\nThe consumer secret is an OAuth '
phoglund@webrtc.org914ef272012-02-27 15:42:25 +0000123 'concept and is obtained from the Google Accounts domain dashboard.'
124 % sys.argv[0])
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000125 return
126
127 consumer_secret = sys.argv[1]
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000128 consumer = oauth.Consumer(constants.CONSUMER_KEY, consumer_secret)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000129
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000130 unauthorized_token = _request_unauthorized_token(consumer,
131 constants.REQUEST_TOKEN_URL)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000132
133 _ask_user_to_authorize_us(unauthorized_token)
134
135 access_token_string = _request_access_token(consumer, unauthorized_token)
136
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000137 _write_access_token_to_file(access_token_string, constants.ACCESS_TOKEN_FILE)
phoglund@webrtc.org914ef272012-02-27 15:42:25 +0000138 _write_consumer_secret_to_file(consumer_secret,
139 constants.CONSUMER_SECRET_FILE)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000140
141if __name__ == '__main__':
142 _main()