blob: b008e5def3b796d6034e122c556282fe12d88473 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import logging
11
12import settings
13from api import monorail_servicer
14from api.api_proto import sitewide_pb2
15from api.api_proto import sitewide_prpc_pb2
16from framework import servlet_helpers
17from framework import xsrf
18
19
20class 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