Skip to content

Commit

Permalink
Add SSLContext arg to aiohttp adapter (#483)
Browse files Browse the repository at this point in the history
* Add SSLContext arg to aiohttp adapter

Pass an SSLContext to the aiohttp adapter to create a valid SSL endpoint for OAuth callback.

* Update docs for aiohttp adapter

* Added ssl import to pass linter checks

* Moved import to TYPE_CHECKING

Moved the ssl import to TYPE_CHECKING section.

* Removed trailing whitespace

* Fix import format issue from Ruff
  • Loading branch information
cwtravis authored Feb 7, 2025
1 parent 4a59b7c commit 69e95e9
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion twitchio/web/aio_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@


if TYPE_CHECKING:
from ssl import SSLContext

from ..authentication import AuthorizationURLPayload, UserTokenPayload
from ..client import Client

Expand Down Expand Up @@ -101,6 +103,8 @@ class AiohttpAdapter(BaseAdapter, web.Application):
An optional :class:`str` passed to use as the EventSub secret. It is recommended you pass this parameter when using
an adapter for EventSub, as it will reset upon restarting otherwise. You can generate token safe secrets with the
:mod:`secrets` module.
ssl_context: SSLContext | None
An optional :class:`SSLContext` passed to the adapter. If SSL is setup via a front-facing web server such as NGINX, you should leave this as None.
Examples
--------
Expand Down Expand Up @@ -139,6 +143,7 @@ def __init__(
domain: str | None = None,
eventsub_path: str | None = None,
eventsub_secret: str | None = None,
ssl_context: SSLContext | None = None,
) -> None:
super().__init__()
self._runner: web.AppRunner | None = None
Expand All @@ -160,6 +165,8 @@ def __init__(
path: str = eventsub_path.removeprefix("/").removesuffix("/") if eventsub_path else "callback"
self._eventsub_path: str = f"/{path}"

self._ssl_context: SSLContext | None = ssl_context

self._runner_task: asyncio.Task[None] | None = None
self.startup = self.event_startup
self.shutdown = self.event_shutdown
Expand Down Expand Up @@ -210,7 +217,7 @@ async def run(self, host: str | None = None, port: int | None = None) -> None:
self._runner = web.AppRunner(self, access_log=None, handle_signals=True)
await self._runner.setup()

site: web.TCPSite = web.TCPSite(self._runner, host or self._host, port or self._port)
site: web.TCPSite = web.TCPSite(self._runner, host or self._host, port or self._port, ssl_context=self._ssl_context)
self._runner_task = asyncio.create_task(site.start(), name=f"twitchio-web-adapter:{self.__class__.__qualname__}")

async def eventsub_callback(self, request: web.Request) -> web.Response:
Expand Down

0 comments on commit 69e95e9

Please sign in to comment.