added parameter secret_token

This commit is contained in:
sheldy 2023-05-01 00:23:19 +03:00
parent cf269e15f4
commit a94817c6dd

View file

@ -90,6 +90,7 @@ class BaseRequestHandler(ABC):
self, self,
dispatcher: Dispatcher, dispatcher: Dispatcher,
handle_in_background: bool = False, handle_in_background: bool = False,
secret_token: Optional[str] = None,
**data: Any, **data: Any,
) -> None: ) -> None:
""" """
@ -99,6 +100,7 @@ class BaseRequestHandler(ABC):
""" """
self.dispatcher = dispatcher self.dispatcher = dispatcher
self.handle_in_background = handle_in_background self.handle_in_background = handle_in_background
self.secret_token = secret_token
self.data = data self.data = data
def register(self, app: Application, /, path: str, **kwargs: Any) -> None: def register(self, app: Application, /, path: str, **kwargs: Any) -> None:
@ -184,6 +186,10 @@ class BaseRequestHandler(ABC):
return web.Response(body=self._build_response_writer(bot=bot, result=result)) return web.Response(body=self._build_response_writer(bot=bot, result=result))
async def handle(self, request: web.Request) -> web.Response: async def handle(self, request: web.Request) -> web.Response:
if self.secret_token:
t_token = request.headers.get("X-Telegram-Bot-Api-Secret-Token")
if t_token != self.secret_token:
return web.Response(body="Unauthorized", status=401)
bot = await self.resolve_bot(request) bot = await self.resolve_bot(request)
if self.handle_in_background: if self.handle_in_background:
return await self._handle_request_background(bot=bot, request=request) return await self._handle_request_background(bot=bot, request=request)
@ -198,7 +204,11 @@ class SimpleRequestHandler(BaseRequestHandler):
""" """
def __init__( def __init__(
self, dispatcher: Dispatcher, bot: Bot, handle_in_background: bool = True, **data: Any self, dispatcher: Dispatcher,
bot: Bot,
handle_in_background: bool = True,
secret_token: Optional[str] = None,
**data: Any
) -> None: ) -> None:
""" """
:param dispatcher: instance of :class:`aiogram.dispatcher.dispatcher.Dispatcher` :param dispatcher: instance of :class:`aiogram.dispatcher.dispatcher.Dispatcher`
@ -206,7 +216,11 @@ class SimpleRequestHandler(BaseRequestHandler):
waiting end of handler process waiting end of handler process
:param bot: instance of :class:`aiogram.client.bot.Bot` :param bot: instance of :class:`aiogram.client.bot.Bot`
""" """
super().__init__(dispatcher=dispatcher, handle_in_background=handle_in_background, **data) super().__init__(
dispatcher=dispatcher,
handle_in_background=handle_in_background,
secret_token=secret_token,
**data)
self.bot = bot self.bot = bot
async def close(self) -> None: async def close(self) -> None: