core/auth/oauth.py

92 lines
3.0 KiB
Python
Raw Normal View History

2021-07-08 14:48:35 +00:00
from authlib.integrations.starlette_client import OAuth
2021-11-10 13:46:16 +00:00
from starlette.responses import RedirectResponse
2021-07-09 07:14:16 +00:00
from auth.authorize import Authorize
from auth.identity import Identity
2021-11-10 14:10:59 +00:00
from settings import OAUTH_CLIENTS, BACKEND_URL, OAUTH_CALLBACK_URL
2021-07-13 09:15:15 +00:00
2021-07-08 14:48:35 +00:00
oauth = OAuth()
oauth.register(
2022-09-03 10:50:14 +00:00
name="facebook",
client_id=OAUTH_CLIENTS["FACEBOOK"]["id"],
client_secret=OAUTH_CLIENTS["FACEBOOK"]["key"],
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": "public_profile email"},
2021-07-09 07:14:16 +00:00
)
oauth.register(
2022-09-03 10:50:14 +00:00
name="github",
client_id=OAUTH_CLIENTS["GITHUB"]["id"],
client_secret=OAUTH_CLIENTS["GITHUB"]["key"],
access_token_url="https://github.com/login/oauth/access_token",
access_token_params=None,
authorize_url="https://github.com/login/oauth/authorize",
authorize_params=None,
api_base_url="https://api.github.com/",
client_kwargs={"scope": "user:email"},
2021-07-08 14:48:35 +00:00
)
2021-07-13 09:15:15 +00:00
oauth.register(
2022-09-03 10:50:14 +00:00
name="google",
client_id=OAUTH_CLIENTS["GOOGLE"]["id"],
client_secret=OAUTH_CLIENTS["GOOGLE"]["key"],
server_metadata_url="https://accounts.google.com/.well-known/openid-configuration",
client_kwargs={"scope": "openid email profile"},
2021-07-13 09:15:15 +00:00
)
2022-09-03 10:50:14 +00:00
2021-08-26 15:16:44 +00:00
async def google_profile(client, request, token):
2022-09-03 10:50:14 +00:00
profile = await client.parse_id_token(request, token)
profile["id"] = profile["sub"]
return profile
2021-08-26 15:16:44 +00:00
async def facebook_profile(client, request, token):
2022-09-03 10:50:14 +00:00
profile = await client.get("me?fields=name,id,email", token=token)
return profile.json()
2021-08-26 15:16:44 +00:00
async def github_profile(client, request, token):
2022-09-03 10:50:14 +00:00
profile = await client.get("user", token=token)
return profile.json()
2021-08-26 15:16:44 +00:00
profile_callbacks = {
2022-09-03 10:50:14 +00:00
"google": google_profile,
"facebook": facebook_profile,
"github": github_profile,
2021-08-26 15:16:44 +00:00
}
2021-07-08 14:48:35 +00:00
async def oauth_login(request):
2022-09-03 10:50:14 +00:00
provider = request.path_params["provider"]
request.session["provider"] = provider
client = oauth.create_client(provider)
redirect_uri = "%s/%s" % (BACKEND_URL, "oauth_authorize")
return await client.authorize_redirect(request, redirect_uri)
2021-07-08 14:48:35 +00:00
async def oauth_authorize(request):
2022-09-03 10:50:14 +00:00
provider = request.session["provider"]
client = oauth.create_client(provider)
token = await client.authorize_access_token(request)
get_profile = profile_callbacks[provider]
profile = await get_profile(client, request, token)
user_oauth_info = "%s:%s" % (provider, profile["id"])
user_input = {
"oauth": user_oauth_info,
"email": profile["email"],
"username": profile["name"],
}
user = Identity.identity_oauth(user_input)
token = await Authorize.authorize(user, device="pc")
response = RedirectResponse(url=OAUTH_CALLBACK_URL)
response.set_cookie("token", token)
return response