oauth initial support

This commit is contained in:
knst-kotov 2021-07-08 17:48:35 +03:00
parent cd8af4d761
commit e6b0866f6e
2 changed files with 36 additions and 1 deletions

28
auth/oauth.py Normal file
View File

@ -0,0 +1,28 @@
from authlib.integrations.starlette_client import OAuth
from starlette.responses import PlainTextResponse
oauth = OAuth()
oauth.register(
name='facebook',
client_id='222122999761250',
client_secret='',
access_token_url='https://graph.facebook.com/v11.0/oauth/access_token',
access_token_params=None,
authorize_url='https://www.facebook.com/v11.0/dialog/oauth',
authorize_params=None,
api_base_url='https://graph.facebook.com/',
client_kwargs={'scope': 'user:email'},
)
async def oauth_login(request):
facebook = oauth.create_client('facebook')
redirect_uri = request.url_for('oauth_authorize')
return await facebook.authorize_redirect(request, redirect_uri)
async def oauth_authorize(request):
facebook = oauth.create_client('facebook')
token = await facebook.authorize_access_token(request)
email = await facebook.parse_id_token(request, token)
print(email)
return PlainTextResponse("%s auth" % email)

View File

@ -5,8 +5,10 @@ from ariadne.asgi import GraphQL
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.routing import Route
from auth.authenticate import JWTAuthenticate
from auth.oauth import oauth_login, oauth_authorize
from redis import redis
from resolvers.base import resolvers
@ -23,6 +25,11 @@ async def start_up():
async def shutdown():
await redis.disconnect()
routes = [
Route("/oauth", endpoint=oauth_login),
Route("/authorize", endpoint=oauth_authorize)
]
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown], middleware=middleware)
app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown], middleware=middleware, routes=routes)
app.mount("/", GraphQL(schema, debug=True))