blob: ad85f4be5e148f48de52f26ba74fe8395a9e0a36 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5from __future__ import print_function
6from __future__ import division
7from __future__ import absolute_import
8
9import logging
10
11import settings
12from api import monorail_servicer
13from api.api_proto import sitewide_pb2
14from api.api_proto import sitewide_prpc_pb2
15from framework import servlet_helpers
16from framework import xsrf
17
18
19class 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