diff --git a/requirements.txt b/requirements.txt index 3271e08..0fe17e7 100755 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,7 @@ twitchAPI>=4.5.0 # Nécessaire pour l'hébergement du site web flask>=2.3.2 flask-sqlalchemy>=3.0.3 +flask[async] waitress>=3.0.2 # Nécessaire pour l'appel à l'API Humble Bundle diff --git a/webapp/__init__.py b/webapp/__init__.py index 8620413..c6f55d1 100644 --- a/webapp/__init__.py +++ b/webapp/__init__.py @@ -2,4 +2,4 @@ from flask import Flask webapp = Flask(__name__) -from webapp import commandes, configurations, index, humeurs, messages, moderation, protondb +from webapp import commandes, configurations, index, humeurs, messages, moderation, protondb, twitch_auth diff --git a/webapp/configurations.py b/webapp/configurations.py index 691629a..1fed45e 100644 --- a/webapp/configurations.py +++ b/webapp/configurations.py @@ -27,4 +27,4 @@ def updateConfiguration(): @webapp.route("/configurations/twitch/help") def twitchConfigurationHelp(): - return render_template("twitch-aide.html") + return render_template("twitch-aide.html", token_redirect_url = f'{request.url_root[:-1]}{url_for('twitchReceiveToken')}') diff --git a/webapp/static/img/twitch-api-01.jpg b/webapp/static/img/twitch-api-01.jpg index 08d3aa8..e66ad9f 100644 Binary files a/webapp/static/img/twitch-api-01.jpg and b/webapp/static/img/twitch-api-01.jpg differ diff --git a/webapp/static/img/twitch-api-02.jpg b/webapp/static/img/twitch-api-02.jpg index a878973..9c20cba 100644 Binary files a/webapp/static/img/twitch-api-02.jpg and b/webapp/static/img/twitch-api-02.jpg differ diff --git a/webapp/static/img/twitch-api-03.jpg b/webapp/static/img/twitch-api-03.jpg deleted file mode 100644 index 1bd71cf..0000000 Binary files a/webapp/static/img/twitch-api-03.jpg and /dev/null differ diff --git a/webapp/static/img/twitch-api-04.jpg b/webapp/static/img/twitch-api-04.jpg deleted file mode 100644 index 55ec373..0000000 Binary files a/webapp/static/img/twitch-api-04.jpg and /dev/null differ diff --git a/webapp/static/img/twitch-api-05.jpg b/webapp/static/img/twitch-api-05.jpg deleted file mode 100644 index c5bea6a..0000000 Binary files a/webapp/static/img/twitch-api-05.jpg and /dev/null differ diff --git a/webapp/templates/configurations.html b/webapp/templates/configurations.html index 05ceb02..5a7fbd8 100644 --- a/webapp/templates/configurations.html +++ b/webapp/templates/configurations.html @@ -18,10 +18,6 @@ - - - - @@ -29,6 +25,17 @@

Aide

+ {% if configuration.getValue('twitch_client_secret') and configuration.getValue('twitch_client_id') %} +

+ Obtenir token et refresh token +

+ + + + + {% endif %}

Nécessite un redémarrage

diff --git a/webapp/templates/twitch-aide.html b/webapp/templates/twitch-aide.html index 11c1fab..487ded0 100644 --- a/webapp/templates/twitch-aide.html +++ b/webapp/templates/twitch-aide.html @@ -3,14 +3,15 @@ {% block content %}

Procédure de configuration de Twitch

- Avant toute chose, activez l'authentification à deux facteurs (2FA) : - Guide officiel Twitch pour la 2FA + Avant toute chose, activez l'authentification à deux facteurs (2FA) : + Guide officiel + Twitch pour la 2FA

Rendez-vous sur la console d'applications Twitch et ajoutez une application. Renseignez :

@@ -25,25 +26,10 @@

- Ensuite, rendez-vous sur twitchtokengenerator.com et - sélectionnez Custom Scope Target. Dans la section Use My Client Secret and Client - ID, renseignez les deux champs. + Ensuite, retourner sur la page de Configuration, apres avoir + enregistret le Client ID et le Client Secret, cliquer sur le lien Obtenir + token et refresh token. Si tout se passe bien les champs Access Token et + Refresh Token sont rempli.

- - -

- Ensuite, dans la section Available Token Scopes, cochez chat:read et chat:edit. - Puis, dans la même section, cliquez sur Generate token. -

- - - -

- Suivez la procédure et vous avez votre Access Token et Refresh Token -

- - - - {% endblock %} \ No newline at end of file diff --git a/webapp/twitch_auth.py b/webapp/twitch_auth.py new file mode 100644 index 0000000..fdfdde2 --- /dev/null +++ b/webapp/twitch_auth.py @@ -0,0 +1,45 @@ +import logging + +from flask import 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/request-token") +async def twitchRequestToken(): + global auth + url = f'{request.url_root[:-1]}{url_for('twitchReceiveToken')}' + helper = ConfigurationHelper() + twitch = await Twitch(helper.getValue('twitch_client_id'), helper.getValue('twitch_client_secret')) + auth = UserAuthenticator(twitch, USER_SCOPE, url=url) + return redirect(auth.return_auth_url()) + +@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('bad returned state') + return redirect(url_for('openConfigurations')) + if code == None : + logging('no returned state') + return redirect(url_for('openConfigurations')) + + try: + token, refresh = await auth.authenticate(user_token=code) + helper = ConfigurationHelper() + helper.createOrUpdate('twitch_access_token', token) + helper.createOrUpdate('twitch_refresh_token', refresh) + db.session.commit() + except TwitchAPIException as e: + logging(e) + return redirect(url_for('openConfigurations'))