32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
import requests
|
|
|
|
|
|
class Oauth:
|
|
def __init__(self, config: dict):
|
|
self.token_url = config['token_url']
|
|
self.introspection_url = config['introspection_url']
|
|
self.client_id = config['client_id']
|
|
self.client_secret = config['client_secret']
|
|
|
|
auth_url = config['authorization_url']
|
|
auth_args=f'response_type=code&client_id={self.client_id}'
|
|
self.auth_url = f'{auth_url}?{auth_args}'
|
|
|
|
self.session = requests.Session()
|
|
self.session.auth = (self.client_id, self.client_secret)
|
|
|
|
def request_access_token(self, one_time_code: str) -> dict:
|
|
body = {'grant_type': 'authorization_code',
|
|
'code': one_time_code}
|
|
response = self.session.post(self.token_url, data=body)
|
|
return response.json()['access_token']
|
|
|
|
def authorize(self, token: str) -> dict:
|
|
response = self.session.post(self.introspection_url,
|
|
data=token)
|
|
try:
|
|
response.raise_for_status()
|
|
except requests.HTTPError:
|
|
return None
|
|
return response.json()
|