mirror of
https://github.com/skylanix/MamieHenriette.git
synced 2026-02-14 12:10:38 +01:00
Gestion de l'authentification pour le access token et refresh_token
This commit is contained in:
45
webapp/twitch_auth.py
Normal file
45
webapp/twitch_auth.py
Normal file
@@ -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'))
|
||||
Reference in New Issue
Block a user