blob: 6d9e22c4a0890470e1b9658c9af3e945c6e9ee83 [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
16 dashboard administrator leaves the project.
17
18 This script should be run on the build bot which runs the track_coverage.py
19 script. This script will present a link during its execution, which the new
20 administrator should follow and then click approve on the web page that
21 appears. The new administrator should have admin rights on the coverage
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000022 dashboard, otherwise track_coverage.py will not work.
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000023
24 If successful, this script will write the access token to a file access.token
25 in the current directory, which later can be read by track_coverage.py.
26 The token is stored in string form (as reported by the web server) using the
27 shelve module.
28"""
29
30__author__ = 'phoglund@webrtc.org (Patrik Höglund)'
31
32import shelve
33import sys
34import urlparse
35import oauth2 as oauth
36
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000037import constants
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000038
phoglund@webrtc.org86ce46d2012-02-06 10:55:12 +000039
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000040class FailedToRequestPermissionException(Exception):
41 pass
42
43
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000044def _ensure_token_response_is_200(response, queried_url, token_type):
45 if response.status != 200:
46 raise FailedToRequestPermissionException('Failed to request %s from %s: '
47 'received status %d, reason %s.' %
48 (token_type,
49 queried_url,
50 response.status,
51 response.reason))
52
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000053
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000054def _request_unauthorized_token(consumer, request_token_url):
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000055 """Requests the initial token from the dashboard service.
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000056
57 Given that the response from the server is correct, we will return a
58 dictionary containing oauth_token and oauth_token_secret mapped to the
59 token and secret value, respectively.
60 """
61 client = oauth.Client(consumer)
62
63 try:
64 response, content = client.request(request_token_url, 'POST')
65 except AttributeError as error:
66 # This catch handler is here since we'll get very confusing messages
67 # if the target server is down for some reason.
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000068 raise FailedToRequestPermissionException('Failed to request token: '
69 'the dashboard is likely down.',
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000070 error)
71
72 _ensure_token_response_is_200(response, request_token_url,
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000073 'unauthorized token')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000074
75 return dict(urlparse.parse_qsl(content))
76
77
78def _ask_user_to_authorize_us(unauthorized_token):
79 """This function will block until the user enters y + newline."""
80 print 'Go to the following link in your browser:'
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000081 print '%s?oauth_token=%s' % (constants.AUTHORIZE_TOKEN_URL,
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000082 unauthorized_token['oauth_token'])
83
84 accepted = 'n'
85 while accepted.lower() != 'y':
86 accepted = raw_input('Have you authorized me yet? (y/n) ')
87
88
89def _request_access_token(consumer, unauthorized_token):
90 token = oauth.Token(unauthorized_token['oauth_token'],
91 unauthorized_token['oauth_token_secret'])
92 client = oauth.Client(consumer, token)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000093 response, content = client.request(constants.ACCESS_TOKEN_URL, 'POST')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000094
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000095 _ensure_token_response_is_200(response, constants.ACCESS_TOKEN_URL,
96 'access token')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000097
98 return content
99
100
101def _write_access_token_to_file(access_token, filename):
102 output = shelve.open(filename)
103 output['access_token'] = access_token
104 output.close()
105
106 print 'Wrote the access token to the file %s.' % filename
107
108
109def _main():
110 if len(sys.argv) != 2:
111 print ('Usage: %s <consumer secret>.\n\nThe consumer secret is an OAuth '
112 'concept and is obtained from the appengine running the dashboard.' %
113 sys.argv[0])
114 return
115
116 consumer_secret = sys.argv[1]
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000117 consumer = oauth.Consumer(constants.CONSUMER_KEY, consumer_secret)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000118
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000119 unauthorized_token = _request_unauthorized_token(consumer,
120 constants.REQUEST_TOKEN_URL)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000121
122 _ask_user_to_authorize_us(unauthorized_token)
123
124 access_token_string = _request_access_token(consumer, unauthorized_token)
125
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000126 _write_access_token_to_file(access_token_string, constants.ACCESS_TOKEN_FILE)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000127
128if __name__ == '__main__':
129 _main()