-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathoauth.py
More file actions
50 lines (40 loc) · 1.63 KB
/
oauth.py
File metadata and controls
50 lines (40 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from typing import Dict
import requests
from django.conf import settings
from authlib.integrations.requests_client import OAuth2Session
class OAuthManager(object):
def __enter__(self):
return self
def __init__(self):
self.client_id = settings.OAUTH_CLIENT_ID
self.client_secret = settings.OAUTH_CLIENT_SECRET
self.token_url = settings.OAUTH_ACCESS_TOKEN_URL
self.api_url = settings.OAUTH_ACCESS_API_URL
self.client = OAuth2Session(self.client_id, self.client_secret)
def fetch_token(self, username: str, password: str) -> Dict:
response = self.client.fetch_token(self.token_url,
username=username,
password=password)
return response
def get_user(self) -> Dict:
response = self.client.get(f'{self.api_url}/users')
result = response.json()
return result
def get_email(self) -> str:
response = self.client.get(f'{self.api_url}/users/emails')
result = response.json()
return result['email']
def get_user_by_token(self, token: str) -> Dict:
response = requests.get(f'{self.api_url}/users', headers={
'Authorization': f'Bearer {token}'
})
result = response.json()
return result
def get_email_by_token(self, token: str) -> Dict:
response = requests.get(f'{self.api_url}/users/emails', headers={
'Authorization': f'Bearer {token}'
})
result = response.json()
return result
def __exit__(self, exc_type, exc_val, exc_tb):
self.client.close()