From 5106392df839f1d12bd08fd64f7bb97a16f10b54 Mon Sep 17 00:00:00 2001 From: Mawoka Date: Fri, 5 May 2023 21:19:08 +0200 Subject: [PATCH] :ambulance: Fixed OAuth bug --- classquiz/oauth/init_oauth.py | 57 +++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/classquiz/oauth/init_oauth.py b/classquiz/oauth/init_oauth.py index 1f39e8d..bd6eee1 100644 --- a/classquiz/oauth/init_oauth.py +++ b/classquiz/oauth/init_oauth.py @@ -12,30 +12,35 @@ settings = settings() @lru_cache() def init_oauth() -> OAuth: oauth = OAuth() - oauth.register( - name="google", - server_metadata_url="https://accounts.google.com/.well-known/openid-configuration", - client_kwargs={"scope": "openid email profile"}, - client_id=settings.google_client_id, - client_secret=settings.google_client_secret, - ) - - oauth.register( - name="github", - client_kwargs={"scope": "read:user user:email"}, - 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_id=settings.github_client_id, - client_secret=settings.github_client_secret, - ) - oauth.register( - name="custom", - client_kwargs={"scope": settings.custom_openid_provider.scopes}, - server_metadata_url=settings.custom_openid_provider.server_metadata_url, - client_id=settings.custom_openid_provider.client_id, - client_secret=settings.custom_openid_provider.client_secret, - ) + if settings.google_client_secret is not None and settings.google_client_id is not None: + oauth.register( + name="google", + server_metadata_url="https://accounts.google.com/.well-known/openid-configuration", + client_kwargs={"scope": "openid email profile"}, + client_id=settings.google_client_id, + client_secret=settings.google_client_secret, + ) + if settings.github_client_id is not None and settings.github_client_secret is not None: + oauth.register( + name="github", + client_kwargs={"scope": "read:user user:email"}, + 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_id=settings.github_client_id, + client_secret=settings.github_client_secret, + ) + if ( + settings.custom_openid_provider.client_id is not None + and settings.custom_openid_provider.client_secret is not None + ): + oauth.register( + name="custom", + client_kwargs={"scope": settings.custom_openid_provider.scopes}, + server_metadata_url=settings.custom_openid_provider.server_metadata_url, + client_id=settings.custom_openid_provider.client_id, + client_secret=settings.custom_openid_provider.client_secret, + ) return oauth