import logging from flask import render_template, request, redirect, url_for from twitchAPI.twitch import Twitch from twitchAPI.type import TwitchAPIException from twitchAPI.oauth import UserAuthenticator from database import db from database.helpers import ConfigurationHelper from twitchbot import USER_SCOPE from webapp import webapp auth: UserAuthenticator @webapp.route("/configurations/twitch/help") def twitchConfigurationHelp(): return render_template("twitch-aide.html", token_redirect_url = _buildUrl()) @webapp.route("/configurations/twitch/request-token") async def twitchRequestToken(): global auth try: helper = ConfigurationHelper() import asyncio twitch = await asyncio.wait_for( Twitch(helper.getValue('twitch_client_id'), helper.getValue('twitch_client_secret')), timeout=30.0 ) auth = UserAuthenticator(twitch, USER_SCOPE, url=_buildUrl()) return redirect(auth.return_auth_url()) except asyncio.TimeoutError: logging.error('Timeout lors de la connexion à Twitch API pour la demande de token') return redirect(url_for('openConfigurations')) except TwitchAPIException as e: logging.error(f'Erreur API Twitch lors de la demande de token : {e}') return redirect(url_for('openConfigurations')) except Exception as e: logging.error(f'Erreur inattendue lors de la demande de token Twitch : {e}') return redirect(url_for('openConfigurations')) @webapp.route("/configurations/twitch/receive-token") async def twitchReceiveToken(): global auth state = request.args.get('state') code = request.args.get('code') if state != auth.state : logging.error('bad returned state') return redirect(url_for('openConfigurations')) if code == None : logging.error('no returned code') return redirect(url_for('openConfigurations')) try: import asyncio token, refresh = await asyncio.wait_for(auth.authenticate(user_token=code), timeout=30.0) helper = ConfigurationHelper() helper.createOrUpdate('twitch_access_token', token) helper.createOrUpdate('twitch_refresh_token', refresh) db.session.commit() except asyncio.TimeoutError: logging.error('Timeout lors de l\'authentification Twitch') except TwitchAPIException as e: logging.error(f'Erreur API Twitch lors de l\'authentification : {e}') except Exception as e: logging.error(f'Erreur inattendue lors de l\'authentification Twitch : {e}') return redirect(url_for('openConfigurations')) # hack pas fou mais on estime qu'on sera toujours en ssl en connecté def _buildUrl(): url = f'{request.url_root[:-1]}{url_for('twitchReceiveToken')}' if url.find('localhost') != -1 : return url url = url.replace('http://', 'https://') return url