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:
|
|
|
|
SESSION_KEY = "oauth_state"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def authorize():
|
|
|
|
try:
|
|
|
|
hydra = OAuth2Session(HYDRA_CLIENT_ID)
|
|
|
|
authorization_url, state = hydra.authorization_url(
|
|
|
|
HYDRA_AUTHORIZATION_BASE_URL
|
|
|
|
)
|
|
|
|
|
|
|
|
# State is used to prevent CSRF, keep this for later.
|
|
|
|
session[HydraOauth.SESSION_KEY] = state
|
|
|
|
|
|
|
|
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 07:49:11 +01:00
|
|
|
hydra = OAuth2Session(
|
2022-01-20 08:00:54 +01:00
|
|
|
client_id=HYDRA_CLIENT_ID,
|
2022-01-20 07:40:11 +01:00
|
|
|
state=state,
|
2022-01-20 07:49:11 +01:00
|
|
|
)
|
|
|
|
token = hydra.fetch_token(
|
|
|
|
TOKEN_URL, code=code, state=state, client_secret=HYDRA_CLIENT_SECRET
|
2022-01-18 10:48:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
session["hydra_token"] = token
|
|
|
|
return token
|
|
|
|
except Exception as err:
|
|
|
|
raise HydraError(str(err), 500)
|