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