phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 1 | #!/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.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame^] | 16 | dashboard administrator leaves the project. This script can also be used to |
| 17 | launch a new dashboard if that is desired. |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 18 | |
| 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.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame^] | 23 | dashboard, otherwise the track_* scripts will not work. |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 24 | |
| 25 | If successful, this script will write the access token to a file access.token |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame^] | 26 | in the current directory, which later can be read by the track_* scripts. |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 27 | The token is stored in string form (as reported by the web server) using the |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame^] | 28 | 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.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 31 | """ |
| 32 | |
| 33 | __author__ = 'phoglund@webrtc.org (Patrik Höglund)' |
| 34 | |
| 35 | import shelve |
| 36 | import sys |
| 37 | import urlparse |
| 38 | import oauth2 as oauth |
| 39 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 40 | import constants |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 41 | |
phoglund@webrtc.org | 86ce46d | 2012-02-06 10:55:12 +0000 | [diff] [blame] | 42 | |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 43 | class FailedToRequestPermissionException(Exception): |
| 44 | pass |
| 45 | |
| 46 | |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 47 | def _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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 56 | |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 57 | def _request_unauthorized_token(consumer, request_token_url): |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 58 | """Requests the initial token from the dashboard service. |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 59 | |
| 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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 71 | raise FailedToRequestPermissionException('Failed to request token: ' |
| 72 | 'the dashboard is likely down.', |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 73 | error) |
| 74 | |
| 75 | _ensure_token_response_is_200(response, request_token_url, |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 76 | 'unauthorized token') |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 77 | |
| 78 | return dict(urlparse.parse_qsl(content)) |
| 79 | |
| 80 | |
| 81 | def _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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 84 | print '%s?oauth_token=%s' % (constants.AUTHORIZE_TOKEN_URL, |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 85 | 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 | |
| 92 | def _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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 96 | response, content = client.request(constants.ACCESS_TOKEN_URL, 'POST') |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 97 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 98 | _ensure_token_response_is_200(response, constants.ACCESS_TOKEN_URL, |
| 99 | 'access token') |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 100 | |
| 101 | return content |
| 102 | |
| 103 | |
| 104 | def _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.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame^] | 112 | def _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.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 120 | def _main(): |
| 121 | if len(sys.argv) != 2: |
| 122 | print ('Usage: %s <consumer secret>.\n\nThe consumer secret is an OAuth ' |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame^] | 123 | 'concept and is obtained from the Google Accounts domain dashboard.' |
| 124 | % sys.argv[0]) |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 125 | return |
| 126 | |
| 127 | consumer_secret = sys.argv[1] |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 128 | consumer = oauth.Consumer(constants.CONSUMER_KEY, consumer_secret) |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 129 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 130 | unauthorized_token = _request_unauthorized_token(consumer, |
| 131 | constants.REQUEST_TOKEN_URL) |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 132 | |
| 133 | _ask_user_to_authorize_us(unauthorized_token) |
| 134 | |
| 135 | access_token_string = _request_access_token(consumer, unauthorized_token) |
| 136 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 137 | _write_access_token_to_file(access_token_string, constants.ACCESS_TOKEN_FILE) |
phoglund@webrtc.org | 914ef27 | 2012-02-27 15:42:25 +0000 | [diff] [blame^] | 138 | _write_consumer_secret_to_file(consumer_secret, |
| 139 | constants.CONSUMER_SECRET_FILE) |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 140 | |
| 141 | if __name__ == '__main__': |
| 142 | _main() |