From 1452f2076b9e2b63fd8d6f10310d4c1acef2dbeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?c=C4=83t=C4=83lin?= Date: Mon, 24 Feb 2025 11:29:12 +0100 Subject: [PATCH] fix: retrieve user profile after refreshing twitch creds --- charts/huesoporro/Chart.yaml | 4 ++-- charts/huesoporro/values.yaml | 2 +- devenv.nix | 1 + pyproject.toml | 2 +- src/huesoporro/actions/refresh.py | 20 ++++++++++---------- src/huesoporro/infra/authenticator.py | 3 ++- uv.lock | 2 +- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/charts/huesoporro/Chart.yaml b/charts/huesoporro/Chart.yaml index eb7dbf9..77ad439 100644 --- a/charts/huesoporro/Chart.yaml +++ b/charts/huesoporro/Chart.yaml @@ -15,10 +15,10 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.2.7 +version: 0.2.8 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. # It is recommended to use it with quotes. -appVersion: "0.2.7" +appVersion: "0.2.8" diff --git a/charts/huesoporro/values.yaml b/charts/huesoporro/values.yaml index e154a85..b0bcc31 100644 --- a/charts/huesoporro/values.yaml +++ b/charts/huesoporro/values.yaml @@ -11,7 +11,7 @@ image: # This sets the pull policy for images. pullPolicy: Always # Overrides the image tag whose default is the chart appVersion. - tag: "0.2.7" + tag: "0.2.8" # This is for the secretes for pulling an image from a private repository more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/ imagePullSecrets: [] diff --git a/devenv.nix b/devenv.nix index 06edec4..2342729 100644 --- a/devenv.nix +++ b/devenv.nix @@ -10,6 +10,7 @@ languages.python.version = "3.12.8"; enterShell = '' + uv sync ''; dotenv.enable = true; diff --git a/pyproject.toml b/pyproject.toml index 8b774ef..1b7363a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "huesoporro" -version = "0.2.5" +version = "0.2.8" description = "Misc Twitch bots" readme = "README.md" authors = [ diff --git a/src/huesoporro/actions/refresh.py b/src/huesoporro/actions/refresh.py index 4ba8543..f28cc7c 100644 --- a/src/huesoporro/actions/refresh.py +++ b/src/huesoporro/actions/refresh.py @@ -11,17 +11,17 @@ class RefreshAction(BaseModel): authenticator: TwitchAuthenticator s: Settings - async def run(self, user: User): + async def run(self, user: User) -> str | None: is_valid = await self.authenticator.token_is_valid( user.external_auth["twitch"]["access_token"] ) + if is_valid: + return None - if not is_valid: - new_tokens = await self.authenticator.refresh_token( - user.external_auth["twitch"]["refresh_token"] - ) - user.external_auth["twitch"]["access_token"] = new_tokens["access_token"] # type: ignore[index] - user.external_auth["twitch"]["refresh_token"] = new_tokens["refresh_token"] # type: ignore[index] - await self.user_repo.update(user) - return user.encode() - return None + twitch_auth = await self.authenticator.refresh_token( + user.external_auth["twitch"]["refresh_token"] + ) + user.external_auth["twitch"]["access_token"] = twitch_auth.access_token + user.external_auth["twitch"]["refresh_token"] = twitch_auth.refresh_token + await self.user_repo.update(user) + return user.encode() diff --git a/src/huesoporro/infra/authenticator.py b/src/huesoporro/infra/authenticator.py index 9c372d8..0405c8a 100644 --- a/src/huesoporro/infra/authenticator.py +++ b/src/huesoporro/infra/authenticator.py @@ -52,7 +52,8 @@ class TwitchAuthenticator(BaseModel): headers={"Accept": "application/json"}, ) response.raise_for_status() - return TwitchAuth(**response.json()) + profile = await self.get_userinfo(response.json()["access_token"]) + return TwitchAuth(**response.json(), userinfo=profile) async def validate_token(self, access_token: str) -> str: response = await self.client.get( diff --git a/uv.lock b/uv.lock index 3da5f24..1f980b7 100644 --- a/uv.lock +++ b/uv.lock @@ -472,7 +472,7 @@ wheels = [ [[package]] name = "huesoporro" -version = "0.2.5" +version = "0.2.8" source = { virtual = "." } dependencies = [ { name = "aiosqlite" },