webrtc/tools/quality_tracking/dashboard/oauth_post_request_handler.py
phoglund@webrtc.org d4f0a0e2bc Refactored the dashboard in order to add new functionality and added some new functionality.
Note that all files were moved to a new directory. The diffs won't be 100% friendly because of this.

Extracted common handling for OAuth on both sides of the connection in order to add a new build status
data handler. This data handler will be used to report build status data. Don't look too closely at the
details of what data is transferred as this will change in the next patch. We will also extract data from
a different page in a slightly different way, but there won't be huge differences.

In particular, we won't look at the /one_box_per_builder page on the master but rather at the transposed
grid (/tgrid) on the build master since we also need the revision number. The regular expressions to
extract the data will be slightly more complex.

BUG=
TEST=

Review URL: https://webrtc-codereview.appspot.com/367023

git-svn-id: http://webrtc.googlecode.com/svn/trunk@1586 4adac7df-926f-26a2-2b94-8c16560cd09d
2012-02-01 10:59:23 +00:00

69 lines
2.6 KiB
Python

#!/usr/bin/env python
#-*- coding: utf-8 -*-
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS. All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.
"""Provides a OAuth request handler base class."""
__author__ = 'phoglund@webrtc.org (Patrik Höglund)'
from google.appengine.api import oauth
import webapp2
class UserNotAuthenticatedException(Exception):
"""Gets thrown if a user is not permitted to store data."""
pass
class OAuthPostRequestHandler(webapp2.RequestHandler):
"""Works like a normal request handler but adds OAuth authentication.
This handler will expect a proper OAuth request over POST. This abstract
class deals with the authentication but leaves user-defined data handling
to its subclasses. Subclasses should not implement the post() method but
the _parse_and_store_data() method. Otherwise they may act like regular
request handlers. Subclasses should NOT override the get() method.
The handler will accept an OAuth request if it is correctly formed and
the consumer is acting on behalf of an administrator for the dashboard.
"""
def post(self):
try:
self._authenticate_user()
except UserNotAuthenticatedException as exception:
self._show_error_page('Failed to authenticate user: %s' % exception)
return
# Do the actual work.
self._parse_and_store_data()
def _parse_and_store_data(self):
"""Reads data from POST request and responds accordingly."""
raise NotImplementedError('You must override this method!')
def _authenticate_user(self):
try:
if oauth.is_current_user_admin():
# The user on whose behalf we are acting is indeed an administrator
# of this application, so we're good to go.
return
else:
raise UserNotAuthenticatedException('We are acting on behalf of '
'user %s, but that user is not '
'an administrator.' %
oauth.get_current_user())
except oauth.OAuthRequestError as exception:
raise UserNotAuthenticatedException('Invalid OAuth request: %s' %
exception)
def _show_error_page(self, error_message):
self.response.write('<html><body>%s</body></html>' % error_message)