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 |
| 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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 22 | dashboard, otherwise track_coverage.py will not work. |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 23 | |
| 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 | |
| 32 | import shelve |
| 33 | import sys |
| 34 | import urlparse |
| 35 | import oauth2 as oauth |
| 36 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 37 | import constants |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 38 | |
phoglund@webrtc.org | 86ce46d | 2012-02-06 10:55:12 +0000 | [diff] [blame^] | 39 | |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 40 | class FailedToRequestPermissionException(Exception): |
| 41 | pass |
| 42 | |
| 43 | |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 44 | def _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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 53 | |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 54 | def _request_unauthorized_token(consumer, request_token_url): |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 55 | """Requests the initial token from the dashboard service. |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 56 | |
| 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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 68 | raise FailedToRequestPermissionException('Failed to request token: ' |
| 69 | 'the dashboard is likely down.', |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 70 | error) |
| 71 | |
| 72 | _ensure_token_response_is_200(response, request_token_url, |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 73 | 'unauthorized token') |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 74 | |
| 75 | return dict(urlparse.parse_qsl(content)) |
| 76 | |
| 77 | |
| 78 | def _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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 81 | print '%s?oauth_token=%s' % (constants.AUTHORIZE_TOKEN_URL, |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 82 | 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 | |
| 89 | def _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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 93 | response, content = client.request(constants.ACCESS_TOKEN_URL, 'POST') |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 94 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 95 | _ensure_token_response_is_200(response, constants.ACCESS_TOKEN_URL, |
| 96 | 'access token') |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 97 | |
| 98 | return content |
| 99 | |
| 100 | |
| 101 | def _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 | |
| 109 | def _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.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 117 | consumer = oauth.Consumer(constants.CONSUMER_KEY, consumer_secret) |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 118 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 119 | unauthorized_token = _request_unauthorized_token(consumer, |
| 120 | constants.REQUEST_TOKEN_URL) |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 121 | |
| 122 | _ask_user_to_authorize_us(unauthorized_token) |
| 123 | |
| 124 | access_token_string = _request_access_token(consumer, unauthorized_token) |
| 125 | |
phoglund@webrtc.org | d4f0a0e | 2012-02-01 10:59:23 +0000 | [diff] [blame] | 126 | _write_access_token_to_file(access_token_string, constants.ACCESS_TOKEN_FILE) |
phoglund@webrtc.org | 96c39d1 | 2012-01-24 14:44:51 +0000 | [diff] [blame] | 127 | |
| 128 | if __name__ == '__main__': |
| 129 | _main() |