diff --git a/auth/oauth.py b/auth/oauth.py new file mode 100644 index 00000000..a52afd07 --- /dev/null +++ b/auth/oauth.py @@ -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) diff --git a/main.py b/main.py index 2b94f25e..9e934d6b 100644 --- a/main.py +++ b/main.py @@ -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 @@ -22,7 +24,12 @@ 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))