From 16bd8b25f2f9f283e2ed5682950ee98dcc60d319 Mon Sep 17 00:00:00 2001 From: Jakob Scheid Date: Fri, 8 May 2026 16:50:18 +0200 Subject: [PATCH] Add middleware to check whether Gitea is enabled --- .../gitea/middlewares/check_enabled.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/jcloud_deployment_server/integrations/gitea/middlewares/check_enabled.py diff --git a/src/jcloud_deployment_server/integrations/gitea/middlewares/check_enabled.py b/src/jcloud_deployment_server/integrations/gitea/middlewares/check_enabled.py new file mode 100644 index 0000000..08d0c52 --- /dev/null +++ b/src/jcloud_deployment_server/integrations/gitea/middlewares/check_enabled.py @@ -0,0 +1,60 @@ +# Copyright 2026 jCloud + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import jcloud_config_parser +from typing import Optional +from fastapi import FastAPI, Request +from starlette.middleware.base import BaseHTTPMiddleware +from starlette.responses import JSONResponse + +__all__ = [ + 'GiteaCheckEnabledMiddleware' +] + +class GiteaCheckEnabledMiddleware(BaseHTTPMiddleware): + ''' + A middleware to check whether Gitea webhooks are enabled. + + :param app: The (FastAPI) app. + :param configuration: The configuration. + :type configuration: jcloud_config_parser.ini.INIConfiguration + :param logger: The logger. + :type logger: Optional[logging.Logger] + ''' + def __init__(self, app, configuration: jcloud_config_parser.ini.INIConfiguration, logger: Optional[logging.Logger] = None) -> None: + super().__init__(app) + self.fastapi_app = app + self.configuration = configuration + self.logger = logger + + async def dispatch(self, request: Request, call_next): + ''' + Dispatch. + + :param request: The request. + :type request: Request + ''' + + if request.url.path.startswith('/gitea/'): + + if self.configuration.gitea.enabled not in ('true', 'yes', 't', 'y'): + if self.logger is not None: + self.logger.error(f'{request.url.path}: Gitea is disabled') + return JSONResponse( + status_code = 403, + content = {'detail': 'Gitea disabled'} + ) + + return await call_next(request) \ No newline at end of file