oauth code minor changes

This commit is contained in:
knst-kotov 2021-07-13 09:15:15 +00:00
parent 571dad6f60
commit ead404fc72
3 changed files with 42 additions and 25 deletions

View File

@ -4,12 +4,14 @@ from starlette.responses import PlainTextResponse
from auth.authorize import Authorize from auth.authorize import Authorize
from auth.identity import Identity from auth.identity import Identity
from sensitive_settings import CLIENT_ID, CLIENT_SECRET
oauth = OAuth() oauth = OAuth()
oauth.register( oauth.register(
name='facebook', name='facebook',
client_id='222122999761250', client_id=CLIENT_ID["FACEBOOK"],
client_secret='', client_secret=CLIENT_SECRET["FACEBOOK"],
access_token_url='https://graph.facebook.com/v11.0/oauth/access_token', access_token_url='https://graph.facebook.com/v11.0/oauth/access_token',
access_token_params=None, access_token_params=None,
authorize_url='https://www.facebook.com/v11.0/dialog/oauth', authorize_url='https://www.facebook.com/v11.0/dialog/oauth',
@ -20,8 +22,8 @@ oauth.register(
oauth.register( oauth.register(
name='github', name='github',
client_id='58877ba7ad9baef280b4', client_id=CLIENT_ID["GITHUB"],
client_secret='', client_secret=CLIENT_SECRET["GITHUB"],
access_token_url='https://github.com/login/oauth/access_token', access_token_url='https://github.com/login/oauth/access_token',
access_token_params=None, access_token_params=None,
authorize_url='https://github.com/login/oauth/authorize', authorize_url='https://github.com/login/oauth/authorize',
@ -30,15 +32,30 @@ oauth.register(
client_kwargs={'scope': 'user:email'}, client_kwargs={'scope': 'user:email'},
) )
oauth.register(
name='google',
client_id=CLIENT_ID["GOOGLE"],
client_secret=CLIENT_SECRET["GOOGLE"],
access_token_url='https://oauth2.googleapis.com/token',
access_token_params=None,
authorize_url='https://accounts.google.com/o/oauth2/v2/auth',
authorize_params=None,
api_base_url='https://oauth2.googleapis.com/',
client_kwargs={'scope': 'openid email profile'}
)
async def oauth_login(request): async def oauth_login(request):
github = oauth.create_client('github') provider = request.path_params['provider']
request.session['provider'] = provider
client = oauth.create_client(provider)
redirect_uri = request.url_for('oauth_authorize') redirect_uri = request.url_for('oauth_authorize')
return await github.authorize_redirect(request, redirect_uri) return await client.authorize_redirect(request, redirect_uri)
async def oauth_authorize(request): async def oauth_authorize(request):
github = oauth.create_client('github') provider = request.session['provider']
token = await github.authorize_access_token(request) client = oauth.create_client(provider)
resp = await github.get('user', token=token) token = await client.authorize_access_token(request)
resp = await client.get('user', token=token)
profile = resp.json() profile = resp.json()
oauth_id = profile["id"] oauth_id = profile["id"]
user_input = { user_input = {

View File

@ -29,7 +29,7 @@ async def shutdown():
await redis.disconnect() await redis.disconnect()
routes = [ routes = [
Route("/oauth", endpoint=oauth_login), Route("/oauth/{provider}", endpoint=oauth_login),
Route("/authorize", endpoint=oauth_authorize) Route("/authorize", endpoint=oauth_authorize)
] ]