Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import logging |
| 11 | |
| 12 | import settings |
| 13 | from api import monorail_servicer |
| 14 | from api.api_proto import sitewide_pb2 |
| 15 | from api.api_proto import sitewide_prpc_pb2 |
| 16 | from framework import servlet_helpers |
| 17 | from framework import xsrf |
| 18 | |
| 19 | |
| 20 | class SitewideServicer(monorail_servicer.MonorailServicer): |
| 21 | """Handle API requests related to sitewide operations. |
| 22 | |
| 23 | Each API request is implemented with a method as defined in the .proto |
| 24 | file that does any request-specific validation, uses work_env to |
| 25 | safely operate on business objects, and returns a response proto. |
| 26 | """ |
| 27 | |
| 28 | DESCRIPTION = sitewide_prpc_pb2.SitewideServiceDescription |
| 29 | |
| 30 | def __init__(self, services, make_rate_limiter=True): |
| 31 | # It might be that the token we're asked to refresh is the same one we are |
| 32 | # using to authenticate. So we should use a longer timeout |
| 33 | # (xsrf.REFRESH_TOKEN_TIMEOUT_SEC) when checking the XSRF |
| 34 | super(SitewideServicer, self).__init__( |
| 35 | services, make_rate_limiter, xsrf.REFRESH_TOKEN_TIMEOUT_SEC) |
| 36 | |
| 37 | @monorail_servicer.PRPCMethod |
| 38 | def RefreshToken(self, mc, request): |
| 39 | """Return a new token.""" |
| 40 | # Validate that the token we're asked to refresh would still be valid with a |
| 41 | # longer timeout. |
| 42 | xsrf.ValidateToken( |
| 43 | request.token, mc.auth.user_id, request.token_path, |
| 44 | timeout=xsrf.REFRESH_TOKEN_TIMEOUT_SEC) |
| 45 | |
| 46 | result = sitewide_pb2.RefreshTokenResponse( |
| 47 | token=xsrf.GenerateToken(mc.auth.user_id, request.token_path), |
| 48 | token_expires_sec=xsrf.TokenExpiresSec()) |
| 49 | return result |
| 50 | |
| 51 | @monorail_servicer.PRPCMethod |
| 52 | def GetServerStatus(self, _mc, _request): |
| 53 | result = sitewide_pb2.GetServerStatusResponse( |
| 54 | banner_message=settings.banner_message, |
| 55 | banner_time=servlet_helpers.GetBannerTime(settings.banner_time), |
| 56 | read_only=settings.read_only) |
| 57 | return result |