[Streams] Invalidate old bearer token when api key is updated. (#3990)

This commit is contained in:
Jamie
2020-06-21 18:44:38 +01:00
committed by GitHub
parent d2de3c109a
commit 84d0282815

View File

@@ -30,7 +30,7 @@ import aiohttp
import contextlib import contextlib
from datetime import datetime from datetime import datetime
from collections import defaultdict from collections import defaultdict
from typing import Optional, List, Tuple, Union from typing import Optional, List, Tuple, Union, Dict
_ = Translator("Streams", __file__) _ = Translator("Streams", __file__)
log = logging.getLogger("red.core.cogs.Streams") log = logging.getLogger("red.core.cogs.Streams")
@@ -100,6 +100,11 @@ class Streams(commands.Cog):
self._ready_event.set() self._ready_event.set()
@commands.Cog.listener()
async def on_red_api_tokens_update(self, service_name, api_tokens):
if service_name == "twitch":
await self.get_twitch_bearer_token(api_tokens)
async def cog_before_invoke(self, ctx: commands.Context): async def cog_before_invoke(self, ctx: commands.Context):
await self._ready_event.wait() await self._ready_event.wait()
@@ -116,8 +121,10 @@ class Streams(commands.Cog):
await self.bot.set_shared_api_tokens("twitch", client_id=token) await self.bot.set_shared_api_tokens("twitch", client_id=token)
await self.config.tokens.clear() await self.config.tokens.clear()
async def get_twitch_bearer_token(self) -> None: async def get_twitch_bearer_token(self, api_tokens: Optional[Dict] = None) -> None:
tokens = await self.bot.get_shared_api_tokens("twitch") tokens = (
await self.bot.get_shared_api_tokens("twitch") if api_tokens is None else api_tokens
)
if tokens.get("client_id"): if tokens.get("client_id"):
notified_owner_missing_twitch_secret = ( notified_owner_missing_twitch_secret = (
await self.config.notified_owner_missing_twitch_secret() await self.config.notified_owner_missing_twitch_secret()