From c2a8c36142be612c0d1278fef3ed4943d4870b28 Mon Sep 17 00:00:00 2001 From: Mawoka Date: Fri, 17 Feb 2023 12:18:28 +0100 Subject: [PATCH] :wip: Nearly finished openid --- classquiz/config.py | 2 +- classquiz/db/models.py | 1 + classquiz/oauth/custom.py | 36 +++++++++++++++++++++++++++++------- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/classquiz/config.py b/classquiz/config.py index 760979a..602c3b2 100644 --- a/classquiz/config.py +++ b/classquiz/config.py @@ -14,7 +14,7 @@ from classquiz.storage import Storage class CustomOpenIDProvider(BaseModel): - scopes: str = "openid email profile" + scopes: str = "openid email" server_metadata_url: str client_id: str client_secret: str diff --git a/classquiz/db/models.py b/classquiz/db/models.py index 83b3e9f..0935360 100644 --- a/classquiz/db/models.py +++ b/classquiz/db/models.py @@ -18,6 +18,7 @@ class UserAuthTypes(Enum): LOCAL = "LOCAL" GOOGLE = "GOOGLE" GITHUB = "GITHUB" + CUSTOM = "CUSTOM" class User(ormar.Model): diff --git a/classquiz/oauth/custom.py b/classquiz/oauth/custom.py index e166cd2..cabd58b 100644 --- a/classquiz/oauth/custom.py +++ b/classquiz/oauth/custom.py @@ -12,15 +12,37 @@ from classquiz.auth import check_token from classquiz.helpers.avatar import gzipped_user_avatar from classquiz.oauth.authenticate_user import log_user_in, rememberme_check from classquiz.oauth.init_oauth import init_oauth +from pydantic import BaseModel, ValidationError settings = settings() router = APIRouter() +class Userinfo(BaseModel): + exp: int + iat: int + iss: str + aud: str + sub: uuid.UUID + nonce: str + email: str + email_verified: bool + + +class OpenIDResponse(BaseModel): + access_token: str + expires_in: int + token_type: str + scope: str + refresh_token: str + id_token: str + expires_at: int + userinfo: Userinfo + + @router.get("/login") async def openid_login(req: Request): - print(settings.custom_openid_provider) if settings.google_client_secret is None or settings.google_client_id is None: raise HTTPException(status_code=501, detail="Custom-OAuth-Login isn't available on this server") oauth = init_oauth() @@ -45,12 +67,12 @@ async def auth(request: Request, response: Response): return await rememberme_check(rememberme_token=rememberme_token, response=response) oauth = init_oauth() - user_data = await oauth.google.authorize_access_token(request) + user_data = await oauth.custom.authorize_access_token(request) + try: + user_data = OpenIDResponse(**user_data).userinfo + except (TypeError, ValidationError): + raise HTTPException(status_code=401, detail="Something went wrong.") print(user_data) - # try: - # user_data = OauthGoogleResponse(**user_data).userinfo - # except (TypeError, ValidationError): - # raise HTTPException(status_code=401, detail="Something went wrong.") user_in_db = await User.objects.get_or_none(email=user_data.email) if user_in_db is None: # REGISTER USER @@ -60,7 +82,7 @@ async def auth(request: Request, response: Response): email=user_data.email, username=user_data.name, verified=user_data.email_verified, - auth_type=UserAuthTypes.GOOGLE, + auth_type=UserAuthTypes.CUSTOM, google_uid=user_data.sub, avatar=gzipped_user_avatar(), )