oauth via google,facebook,github

This commit is contained in:
knst-kotov 2021-08-26 18:16:44 +03:00
parent 730129718b
commit 512e73b3b6
2 changed files with 27 additions and 11 deletions

View File

@ -13,7 +13,7 @@ passlib = "*"
PyJWT = "*" PyJWT = "*"
SQLAlchemy = "*" SQLAlchemy = "*"
itsdangerous = "*" itsdangerous = "*"
httpx = "*" httpx = "<0.18.2"
psycopg2-binary = "*" psycopg2-binary = "*"
Authlib = "*" Authlib = "*"
bson = "*" bson = "*"

View File

@ -17,7 +17,7 @@ oauth.register(
authorize_url='https://www.facebook.com/v11.0/dialog/oauth', authorize_url='https://www.facebook.com/v11.0/dialog/oauth',
authorize_params=None, authorize_params=None,
api_base_url='https://graph.facebook.com/', api_base_url='https://graph.facebook.com/',
client_kwargs={'scope': 'user:email'}, client_kwargs={'scope': 'public_profile email'},
) )
oauth.register( oauth.register(
@ -36,14 +36,30 @@ oauth.register(
name='google', name='google',
client_id=OAUTH_CLIENTS["GOOGLE"]["id"], client_id=OAUTH_CLIENTS["GOOGLE"]["id"],
client_secret=OAUTH_CLIENTS["GOOGLE"]["key"], client_secret=OAUTH_CLIENTS["GOOGLE"]["key"],
access_token_url='https://oauth2.googleapis.com/token', server_metadata_url="https://accounts.google.com/.well-known/openid-configuration",
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'} client_kwargs={'scope': 'openid email profile'}
) )
async def google_profile(client, request, token):
profile = await client.parse_id_token(request, token)
profile["id"] = profile["sub"]
return profile
async def facebook_profile(client, request, token):
profile = await client.get('me?fields=name,id,email', token=token)
return profile.json()
async def github_profile(client, request, token):
profile = await client.get('user', token=token)
return profile.json()
profile_callbacks = {
"google" : google_profile,
"facebook" : facebook_profile,
"github" : github_profile
}
async def oauth_login(request): async def oauth_login(request):
provider = request.path_params['provider'] provider = request.path_params['provider']
request.session['provider'] = provider request.session['provider'] = provider
@ -55,11 +71,11 @@ async def oauth_authorize(request):
provider = request.session['provider'] provider = request.session['provider']
client = oauth.create_client(provider) client = oauth.create_client(provider)
token = await client.authorize_access_token(request) token = await client.authorize_access_token(request)
resp = await client.get('user', token=token) get_profile = profile_callbacks[provider]
profile = resp.json() profile = await get_profile(client, request, token)
oauth = profile["id"] user_oauth_info = "%s:%s" % (provider, profile["id"])
user_input = { user_input = {
"oauth" : oauth, "oauth" : user_oauth_info,
"email" : profile["email"], "email" : profile["email"],
"username" : profile["name"] "username" : profile["name"]
} }