forked from LouisYZK/Frodo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext.py
More file actions
72 lines (59 loc) · 2.51 KB
/
ext.py
File metadata and controls
72 lines (59 loc) · 2.51 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import traceback
from fastapi.security import OAuth2PasswordBearer
from passlib.context import CryptContext
from functools import wraps
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from fastapi_mako import FastAPIMako
import databases
import aiohttp
from config import DB_URL, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI
oauth2_scheme = OAuth2PasswordBearer(tokenUrl='/auth')
pwd_context = CryptContext(schemes=['bcrypt'], deprecated='auto')
mako = FastAPIMako()
db_engine = create_engine(
DB_URL
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=db_engine)
Base = declarative_base()
# AioDataBase = databases.Database(DB_URL.replace('+pymysql', ''))
class AioDataBase():
async def __aenter__(self):
db = databases.Database(DB_URL.replace('+pymysql', ''))
await db.connect()
self.db = db
return db
async def __aexit__(self, exc_type, exc, tb):
if exc:
traceback.print_exc()
await self.db.disconnect()
GITHUB_OAUTH_URL = 'https://github.com/login/oauth/authorize'
GITHUB_ACCESS_URL = 'https://github.com/login/oauth/access_token'
GITHUB_API = 'https://api.github.com/user'
class GithubClient:
def __init__(self,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI):
self.client_id = client_id
self.client_secret = client_secret
self.auth_url = \
f'{GITHUB_OAUTH_URL}?client_id={self.client_id}&redirect_uri={REDIRECT_URI}'
async def get_access_token(self, code):
async with aiohttp.ClientSession() as session:
async with session.post(GITHUB_ACCESS_URL,
headers={'accept': 'application/json'},
params={'client_id': self.client_id,
'client_secret': self.client_secret,
'code': code}) as resp:
res = await resp.json()
return res
async def user_info(self, token):
async with aiohttp.ClientSession() as session:
async with session.get(GITHUB_API,
headers={
'accept': 'application/json',
'Authorization': f'token {token}'
}) as resp:
return await resp.json()