OAuth fixed

This commit is contained in:
Mawoka
2025-10-25 21:46:29 +02:00
parent efc2d55d9e
commit 932f8dfbcf
3 changed files with 32 additions and 6 deletions
+7 -3
View File
@@ -51,7 +51,7 @@ class GitHubOauthResponse(BaseModel):
events_url: Optional[str] = None
received_events_url: Optional[str] = None
type: Optional[str] = None
site_admin: Optional[str] = None
site_admin: Optional[bool] = None
name: Optional[str] = None
company: Optional[str] = None
blog: Optional[str] = None
@@ -103,7 +103,8 @@ async def auth(request: Request, response: Response):
except authlib.integrations.base_client.OAuthError:
return RedirectResponse("/account/oauth-error")
resp = await oauth.github.get("user", token=token)
user_data = GitHubOauthResponse(**resp.json())
data = resp.json()
user_data = GitHubOauthResponse(**data)
if user_data.email is None:
return RedirectResponse("/account/oauth-error?error=email")
user_in_db = await User.objects.get_or_none(email=user_data.email)
@@ -140,7 +141,10 @@ async def auth(request: Request, response: Response):
else:
raise HTTPException(status_code=500, detail=str(e))
user = await User.objects.get_or_none(
email=user_data.email, username=user_data.login, auth_type=UserAuthTypes.GITHUB, verified=True
email=user_data.email,
username=user_data.login,
auth_type=UserAuthTypes.GITHUB,
verified=True,
)
await log_user_in(user=user, request=request, response=response)
+24 -2
View File
@@ -4,15 +4,37 @@
from authlib.integrations.starlette_client import OAuth
from classquiz.config import settings
from classquiz.config import settings, redis
from functools import lru_cache
import json
settings = settings()
class RedisCache:
def __init__(self) -> None:
pass
async def get(self, key: str) -> str | None:
value = await redis.get(f"authlib:{key}")
if value is None:
return None
try:
return json.loads(value)
except json.JSONDecodeError:
return value
async def set(self, key: str, value: str, expires: int | None = None):
data = json.dumps(value)
await redis.set(f"authlib:{key}", data, ex=expires)
async def delete(self, key: str) -> None:
await redis.delete(key)
@lru_cache()
def init_oauth() -> OAuth:
oauth = OAuth()
oauth = OAuth(cache=RedisCache())
if settings.google_client_secret is not None and settings.google_client_id is not None:
oauth.register(
name="google",
@@ -88,7 +88,7 @@ SPDX-License-Identifier: MPL-2.0
<svelte:head>
<title>ClassQuiz - Login</title>
</svelte:head>
<div class="flex items-center justify-center h-screen
<div class="flex items-center justify-center h-screen">
{#if verified}
<VerifiedBadge />
{/if}