2022-01-18 10:48:18 +01:00
|
|
|
from flask import request, session
|
|
|
|
from requests_oauthlib import OAuth2Session
|
|
|
|
|
|
|
|
from config import *
|
|
|
|
from helpers import HydraError
|
|
|
|
|
|
|
|
|
|
|
|
class HydraOauth:
|
|
|
|
@staticmethod
|
|
|
|
def authorize():
|
|
|
|
try:
|
2022-11-02 10:25:43 +01:00
|
|
|
hydra = OAuth2Session(HYDRA_CLIENT_ID, redirect_uri=REDIRECT_URL)
|
2022-01-18 10:48:18 +01:00
|
|
|
authorization_url, state = hydra.authorization_url(
|
|
|
|
HYDRA_AUTHORIZATION_BASE_URL
|
|
|
|
)
|
|
|
|
|
|
|
|
return authorization_url
|
|
|
|
except Exception as err:
|
|
|
|
raise HydraError(str(err), 500)
|
|
|
|
|
|
|
|
@staticmethod
|
2022-01-20 07:40:11 +01:00
|
|
|
def get_token(state, code):
|
2022-01-18 10:48:18 +01:00
|
|
|
try:
|
2022-01-20 09:11:40 +01:00
|
|
|
hydra = OAuth2Session(
|
|
|
|
client_id=HYDRA_CLIENT_ID,
|
|
|
|
state=state,
|
|
|
|
)
|
2022-01-20 07:49:11 +01:00
|
|
|
token = hydra.fetch_token(
|
2022-01-20 08:59:00 +01:00
|
|
|
token_url=TOKEN_URL,
|
|
|
|
code=code,
|
|
|
|
client_secret=HYDRA_CLIENT_SECRET,
|
2022-01-20 09:11:40 +01:00
|
|
|
include_client_id=True,
|
2022-01-18 10:48:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
session["hydra_token"] = token
|
|
|
|
return token
|
|
|
|
except Exception as err:
|
|
|
|
raise HydraError(str(err), 500)
|
2022-02-10 09:43:15 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_user_info():
|
|
|
|
try:
|
|
|
|
hydra = OAuth2Session(
|
|
|
|
client_id=HYDRA_CLIENT_ID, token=session["hydra_token"]
|
|
|
|
)
|
2022-04-04 14:24:30 +02:00
|
|
|
user_info = hydra.get("{}/userinfo".format(HYDRA_PUBLIC_URL))
|
2022-02-10 09:43:15 +01:00
|
|
|
|
|
|
|
return user_info.json()
|
|
|
|
except Exception as err:
|
|
|
|
raise HydraError(str(err), 500)
|