blob: 3bc20f6d2750d72868a41e704ae0eb784824cc51 [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
39class FailedToRequestPermissionException(Exception):
40 pass
41
42
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000043def _ensure_token_response_is_200(response, queried_url, token_type):
44 if response.status != 200:
45 raise FailedToRequestPermissionException('Failed to request %s from %s: '
46 'received status %d, reason %s.' %
47 (token_type,
48 queried_url,
49 response.status,
50 response.reason))
51
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000052
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000053def _request_unauthorized_token(consumer, request_token_url):
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000054 """Requests the initial token from the dashboard service.
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000055
56 Given that the response from the server is correct, we will return a
57 dictionary containing oauth_token and oauth_token_secret mapped to the
58 token and secret value, respectively.
59 """
60 client = oauth.Client(consumer)
61
62 try:
63 response, content = client.request(request_token_url, 'POST')
64 except AttributeError as error:
65 # This catch handler is here since we'll get very confusing messages
66 # if the target server is down for some reason.
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000067 raise FailedToRequestPermissionException('Failed to request token: '
68 'the dashboard is likely down.',
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000069 error)
70
71 _ensure_token_response_is_200(response, request_token_url,
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000072 'unauthorized token')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000073
74 return dict(urlparse.parse_qsl(content))
75
76
77def _ask_user_to_authorize_us(unauthorized_token):
78 """This function will block until the user enters y + newline."""
79 print 'Go to the following link in your browser:'
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000080 print '%s?oauth_token=%s' % (constants.AUTHORIZE_TOKEN_URL,
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000081 unauthorized_token['oauth_token'])
82
83 accepted = 'n'
84 while accepted.lower() != 'y':
85 accepted = raw_input('Have you authorized me yet? (y/n) ')
86
87
88def _request_access_token(consumer, unauthorized_token):
89 token = oauth.Token(unauthorized_token['oauth_token'],
90 unauthorized_token['oauth_token_secret'])
91 client = oauth.Client(consumer, token)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000092 response, content = client.request(constants.ACCESS_TOKEN_URL, 'POST')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000093
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000094 _ensure_token_response_is_200(response, constants.ACCESS_TOKEN_URL,
95 'access token')
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +000096
97 return content
98
99
100def _write_access_token_to_file(access_token, filename):
101 output = shelve.open(filename)
102 output['access_token'] = access_token
103 output.close()
104
105 print 'Wrote the access token to the file %s.' % filename
106
107
108def _main():
109 if len(sys.argv) != 2:
110 print ('Usage: %s <consumer secret>.\n\nThe consumer secret is an OAuth '
111 'concept and is obtained from the appengine running the dashboard.' %
112 sys.argv[0])
113 return
114
115 consumer_secret = sys.argv[1]
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000116 consumer = oauth.Consumer(constants.CONSUMER_KEY, consumer_secret)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000117
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000118 unauthorized_token = _request_unauthorized_token(consumer,
119 constants.REQUEST_TOKEN_URL)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000120
121 _ask_user_to_authorize_us(unauthorized_token)
122
123 access_token_string = _request_access_token(consumer, unauthorized_token)
124
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +0000125 _write_access_token_to_file(access_token_string, constants.ACCESS_TOKEN_FILE)
phoglund@webrtc.org96c39d12012-01-24 14:44:51 +0000126
127if __name__ == '__main__':
128 _main()