Skip to content

Commit 2267da8

Browse files
committed
Add optional redirect_path parameter to Adapters
1 parent fea3801 commit 2267da8

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

twitchio/web/aio_adapter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ class AiohttpAdapter(BaseAdapter, web.Application):
105105
An optional :class:`str` passed to use as the EventSub secret. It is recommended you pass this parameter when using
106106
an adapter for EventSub, as it will reset upon restarting otherwise. You can generate token safe secrets with the
107107
:mod:`secrets` module.
108+
redirect_path: str | None
109+
An optional :class:`str` passed to use as the path for the Oauth Redirect callback. Defaults to ``/oauth/callback``.
110+
E.g. ``http://localhost:4343/oauth/callback`` or ``https://mydomain.org/oauth/callback``.
108111
ssl_context: SSLContext | None
109112
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.
110113
@@ -145,6 +148,7 @@ def __init__(
145148
domain: str | None = None,
146149
eventsub_path: str | None = None,
147150
eventsub_secret: str | None = None,
151+
redirect_path: str | None = None,
148152
ssl_context: SSLContext | None = None,
149153
) -> None:
150154
super().__init__()
@@ -169,11 +173,14 @@ def __init__(
169173
path: str = eventsub_path.removeprefix("/").removesuffix("/") if eventsub_path else "callback"
170174
self._eventsub_path: str = f"/{path}"
171175

176+
opath: str = redirect_path.removeprefix("/").removesuffix("/") if redirect_path else "oauth/callback"
177+
self._redirect_path: str = f"/{opath}"
178+
172179
self._runner_task: asyncio.Task[None] | None = None
173180
self.startup = self.event_startup
174181
self.shutdown = self.event_shutdown
175182

176-
self.router.add_route("GET", "/oauth/callback", self.oauth_callback)
183+
self.router.add_route("GET", self._redirect_path, self.oauth_callback)
177184
self.router.add_route("GET", "/oauth", self.oauth_redirect)
178185
self.router.add_route("POST", self._eventsub_path, self.eventsub_callback)
179186

@@ -194,7 +201,7 @@ def eventsub_url(self) -> str:
194201
@property
195202
def redirect_url(self) -> str:
196203
"""Property returning the fully qualified URL to the OAuth callback."""
197-
return f"{self._domain}/oauth/callback"
204+
return f"{self._domain}{self._redirect_path}"
198205

199206
async def event_startup(self) -> None:
200207
logger.info("Starting %r on %s://%s:%s.", self, self._proto, self._host, self._port)
@@ -387,9 +394,9 @@ def _find_redirect(self, request: web.Request) -> str:
387394
if request.host.startswith((self._domain, stripped)):
388395
redirect = self.redirect_url
389396
elif request.host.startswith((self._host, local)):
390-
redirect = f"{local}/oauth/callback"
397+
redirect = f"{local}:{self._port}{self._redirect_path}"
391398
else:
392-
redirect = f"{request.scheme}://{request.host}/oauth/callback"
399+
redirect = f"{request.scheme}://{request.host}{self._redirect_path}"
393400

394401
return redirect
395402

twitchio/web/starlette_adapter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class StarletteAdapter(BaseAdapter, Starlette):
107107
An optional :class:`str` passed to use as the EventSub secret. It is recommended you pass this parameter when using
108108
an adapter for EventSub, as it will reset upon restarting otherwise. You can generate token safe secrets with the
109109
:mod:`secrets` module.
110+
redirect_path: str | None
111+
An optional :class:`str` passed to use as the path for the Oauth Redirect callback. Defaults to ``/oauth/callback``.
112+
E.g. ``http://localhost:4343/oauth/callback`` or ``https://mydomain.org/oauth/callback``.
110113
ssl_keyfile: str | PathLike[str] | None
111114
An optional SSL key file passed to Uvicorn.
112115
ssl_keyfile_password: str | None
@@ -157,6 +160,7 @@ def __init__(
157160
domain: str | None = None,
158161
eventsub_path: str | None = None,
159162
eventsub_secret: str | None = None,
163+
redirect_path: str | None = None,
160164
ssl_keyfile: str | PathLike[str] | None = None,
161165
ssl_keyfile_password: str | None = None,
162166
ssl_certfile: str | PathLike[str] | None = None,
@@ -184,12 +188,15 @@ def __init__(
184188
path: str = eventsub_path.removeprefix("/").removesuffix("/") if eventsub_path else "callback"
185189
self._eventsub_path: str = f"/{path}"
186190

191+
opath: str = redirect_path.removeprefix("/").removesuffix("/") if redirect_path else "oauth/callback"
192+
self._redirect_path: str = f"/{opath}"
193+
187194
self._runner_task: asyncio.Task[None] | None = None
188195
self._responded: deque[str] = deque(maxlen=5000)
189196

190197
super().__init__(
191198
routes=[
192-
Route("/oauth/callback", self.oauth_callback, methods=["GET"]),
199+
Route(self._redirect_path, self.oauth_callback, methods=["GET"]),
193200
Route("/oauth", self.oauth_redirect, methods=["GET"]),
194201
Route(self._eventsub_path, self.eventsub_callback, methods=["POST"]),
195202
],
@@ -215,7 +222,7 @@ def eventsub_url(self) -> str:
215222
@property
216223
def redirect_url(self) -> str:
217224
"""Property returning the fully qualified URL to the OAuth callback."""
218-
return f"{self._domain}/oauth/callback"
225+
return f"{self._domain}{self._redirect_path}"
219226

220227
async def event_startup(self) -> None:
221228
logger.info("Starting %r on %s://%s:%s.", self, self._proto, self._host, self._port)
@@ -405,9 +412,9 @@ def _find_redirect(self, request: Request) -> str:
405412
if host.startswith((self._domain, stripped)):
406413
redirect = self.redirect_url
407414
elif host.startswith((self._host, local)):
408-
redirect = f"{local}:{self._port}/oauth/callback"
415+
redirect = f"{local}:{self._port}{self._redirect_path}"
409416
else:
410-
redirect = f"{scheme}://{host}/oauth/callback"
417+
redirect = f"{scheme}://{host}{self._redirect_path}"
411418

412419
return redirect
413420

0 commit comments

Comments
 (0)