2024-07-11 10:35:50 -07:00
|
|
|
import os
|
2024-07-11 10:40:19 -07:00
|
|
|
|
2024-07-17 12:59:13 -07:00
|
|
|
from flask import abort, current_app, has_request_context, request
|
2016-10-13 17:05:37 +01:00
|
|
|
from flask_login import current_user
|
2020-06-22 09:39:32 +01:00
|
|
|
|
|
|
|
|
from app.extensions import redis_client
|
2025-06-10 11:40:14 -07:00
|
|
|
from notifications_python_client import __version__
|
|
|
|
|
from notifications_python_client.base import BaseAPIClient
|
2024-05-16 10:37:37 -04:00
|
|
|
from notifications_utils.clients.redis import RequestCache
|
2020-06-22 09:39:32 +01:00
|
|
|
|
|
|
|
|
cache = RequestCache(redis_client)
|
2016-04-15 11:08:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _attach_current_user(data):
|
2023-08-25 09:12:23 -07:00
|
|
|
return dict(created_by=current_user.id, **data)
|
2016-11-30 17:00:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class NotifyAdminAPIClient(BaseAPIClient):
|
2019-01-29 11:12:33 +00:00
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__("a" * 73, "b")
|
|
|
|
|
|
2018-02-09 15:03:32 +00:00
|
|
|
def init_app(self, app):
|
2023-08-25 09:12:23 -07:00
|
|
|
self.base_url = app.config["API_HOST_NAME"]
|
|
|
|
|
self.service_id = app.config["ADMIN_CLIENT_USER_NAME"]
|
|
|
|
|
self.api_key = app.config["ADMIN_CLIENT_SECRET"]
|
|
|
|
|
self.route_secret = app.config["ROUTE_SECRET_KEY_1"]
|
2018-02-09 15:03:32 +00:00
|
|
|
|
2016-11-30 17:00:42 +00:00
|
|
|
def generate_headers(self, api_token):
|
|
|
|
|
headers = {
|
|
|
|
|
"Content-type": "application/json",
|
|
|
|
|
"Authorization": "Bearer {}".format(api_token),
|
2018-02-09 15:03:32 +00:00
|
|
|
"X-Custom-Forwarder": self.route_secret,
|
2023-08-25 09:12:23 -07:00
|
|
|
"User-agent": "NOTIFY-API-PYTHON-CLIENT/{}".format(__version__),
|
2016-11-30 17:00:42 +00:00
|
|
|
}
|
|
|
|
|
return self._add_request_id_header(headers)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _add_request_id_header(headers):
|
|
|
|
|
if not has_request_context():
|
|
|
|
|
return headers
|
2023-08-25 09:12:23 -07:00
|
|
|
headers["X-B3-TraceId"] = request.request_id
|
|
|
|
|
headers["X-B3-SpanId"] = request.span_id
|
2016-11-30 17:00:42 +00:00
|
|
|
return headers
|
2016-12-09 15:44:58 +00:00
|
|
|
|
|
|
|
|
def check_inactive_service(self):
|
|
|
|
|
# this file is imported in app/__init__.py before current_service is initialised, so need to import later
|
|
|
|
|
# to prevent cyclical imports
|
|
|
|
|
from app import current_service
|
|
|
|
|
|
|
|
|
|
# if the current service is inactive and the user isn't a platform admin, we should block them from making any
|
|
|
|
|
# stateful modifications to that service
|
2023-08-25 09:12:23 -07:00
|
|
|
if (
|
|
|
|
|
current_service
|
|
|
|
|
and not current_service.active
|
|
|
|
|
and not current_user.platform_admin
|
|
|
|
|
):
|
2016-12-09 15:44:58 +00:00
|
|
|
abort(403)
|
|
|
|
|
|
2024-07-26 09:15:09 -07:00
|
|
|
def is_calling_signin_url(self, arg):
|
2024-07-26 09:49:12 -07:00
|
|
|
return arg.startswith("('/user")
|
2024-07-26 09:15:09 -07:00
|
|
|
|
2024-07-12 10:09:48 -07:00
|
|
|
def check_inactive_user(self, *args):
|
|
|
|
|
still_signing_in = False
|
2024-07-24 07:59:05 -07:00
|
|
|
|
|
|
|
|
# TODO clean up and add testing etc.
|
|
|
|
|
# We really should be checking for exact matches
|
|
|
|
|
# and we only want to check the first arg
|
2024-07-12 10:09:48 -07:00
|
|
|
for arg in args:
|
|
|
|
|
arg = str(arg)
|
2024-07-26 09:15:09 -07:00
|
|
|
if self.is_calling_signin_url(arg):
|
2024-07-12 10:09:48 -07:00
|
|
|
still_signing_in = True
|
2024-07-16 10:10:23 -07:00
|
|
|
|
|
|
|
|
# This seems to be a weird edge case that happens intermittently with invites
|
|
|
|
|
if str(arg) == "()":
|
|
|
|
|
still_signing_in = True
|
2024-07-12 13:19:14 -07:00
|
|
|
# TODO: Update this once E2E tests are managed by a feature flag or some other main config option.
|
2024-07-11 10:27:04 -07:00
|
|
|
if os.getenv("NOTIFY_E2E_TEST_EMAIL"):
|
|
|
|
|
# allow end-to-end tests to skip check
|
|
|
|
|
pass
|
2024-07-12 10:09:48 -07:00
|
|
|
elif still_signing_in is True:
|
|
|
|
|
# we are not full signed in yet
|
|
|
|
|
pass
|
2024-07-11 10:27:04 -07:00
|
|
|
elif not current_user or not current_user.is_active:
|
2024-07-17 12:59:13 -07:00
|
|
|
current_app.logger.error(f"Unauthorized URL #notify-compliance-46 {args}")
|
2024-07-11 09:38:32 -07:00
|
|
|
abort(403)
|
|
|
|
|
|
2016-12-09 15:44:58 +00:00
|
|
|
def post(self, *args, **kwargs):
|
|
|
|
|
self.check_inactive_service()
|
2024-07-12 10:09:48 -07:00
|
|
|
self.check_inactive_user(args)
|
2016-12-09 15:44:58 +00:00
|
|
|
return super().post(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def put(self, *args, **kwargs):
|
|
|
|
|
self.check_inactive_service()
|
2024-07-11 09:38:32 -07:00
|
|
|
self.check_inactive_user()
|
2016-12-09 15:44:58 +00:00
|
|
|
return super().put(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
|
self.check_inactive_service()
|
2024-07-11 09:38:32 -07:00
|
|
|
self.check_inactive_user()
|
2016-12-09 15:44:58 +00:00
|
|
|
return super().delete(*args, **kwargs)
|
2019-05-23 15:27:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class InviteTokenError(Exception):
|
|
|
|
|
pass
|