Skip to content

Commit ed6d767

Browse files
committed
Validate token after OAuth on Adapters for better response in oauth_authorized event
1 parent be2d803 commit ed6d767

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

twitchio/authentication/payloads.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,14 @@ class UserTokenPayload(BasePayload):
100100
A ``str`` or ``list[str]`` containing the scopes the user authenticated with.
101101
token_type: str
102102
The type of token provided. Usually ``bearer``.
103+
user_id: str | None
104+
An optional :class:`str` representing the ID of the User who authorized your application. This could be ``None``.
105+
user_login: str | None
106+
An optional :class:`str` representing the user name of the User who authorized your application.
107+
This could be ``None``.
103108
"""
104109

105-
__slots__ = ("access_token", "expires_in", "refresh_token", "scope", "token_type")
110+
__slots__ = ("_user_id", "_user_login", "access_token", "expires_in", "refresh_token", "scope", "token_type")
106111

107112
def __init__(self, raw: UserTokenResponse, /) -> None:
108113
super().__init__(raw)
@@ -112,6 +117,24 @@ def __init__(self, raw: UserTokenResponse, /) -> None:
112117
self.expires_in: int = raw["expires_in"]
113118
self.scope: str | list[str] = raw["scope"]
114119
self.token_type: str = raw["token_type"]
120+
self._user_id: str | None = None
121+
self._user_login: str | None = None
122+
123+
@property
124+
def user_id(self) -> str | None:
125+
return self._user_id
126+
127+
@user_id.setter
128+
def user_id(self, other: str) -> None:
129+
self._user_id = other
130+
131+
@property
132+
def user_login(self) -> str | None:
133+
return self._user_login
134+
135+
@user_login.setter
136+
def user_login(self, other: str) -> None:
137+
self._user_login = other
115138

116139

117140
class ClientCredentialsPayload(BasePayload):

twitchio/web/aio_adapter.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
from aiohttp import web
3535

36+
from twitchio.authentication.payloads import ValidateTokenPayload
37+
3638
from ..authentication import Scopes
3739
from ..eventsub.subscriptions import _SUB_MAPPING
3840
from ..exceptions import HTTPException
@@ -44,7 +46,7 @@
4446
if TYPE_CHECKING:
4547
from ssl import SSLContext
4648

47-
from ..authentication import AuthorizationURLPayload, UserTokenPayload
49+
from ..authentication import AuthorizationURLPayload, UserTokenPayload, ValidateTokenPayload
4850
from ..client import Client
4951
from ..types_.eventsub import EventSubHeaders
5052

@@ -321,7 +323,12 @@ async def fetch_token(self, request: web.Request) -> FetchTokenPayload:
321323
status: int = e.status
322324
return FetchTokenPayload(status=status, response=web.Response(status=status), exception=e)
323325

326+
validated: ValidateTokenPayload = await self.client._http.validate_token(resp.access_token)
327+
resp._user_id = validated.user_id
328+
resp._user_login = validated.login
329+
324330
self.client.dispatch(event="oauth_authorized", payload=resp)
331+
325332
return FetchTokenPayload(
326333
status=20,
327334
response=web.Response(body="Success. You can leave this page.", status=200),

twitchio/web/starlette_adapter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
if TYPE_CHECKING:
4848
from starlette.requests import Request
4949

50-
from ..authentication import AuthorizationURLPayload, UserTokenPayload
50+
from ..authentication import AuthorizationURLPayload, UserTokenPayload, ValidateTokenPayload
5151
from ..client import Client
5252
from ..types_.eventsub import EventSubHeaders
5353

@@ -347,7 +347,12 @@ async def fetch_token(self, request: Request) -> FetchTokenPayload:
347347
status: int = e.status
348348
return FetchTokenPayload(status=status, response=Response(status_code=status), exception=e)
349349

350+
validated: ValidateTokenPayload = await self.client._http.validate_token(resp.access_token)
351+
resp._user_id = validated.user_id
352+
resp._user_login = validated.login
353+
350354
self.client.dispatch(event="oauth_authorized", payload=resp)
355+
351356
return FetchTokenPayload(
352357
status=200,
353358
response=Response(content="Success. You can leave this page.", status_code=200),

0 commit comments

Comments
 (0)