feat(auth): add CustomOpenID class to force get_user_info implementation

This commit is contained in:
2025-10-11 21:37:49 +02:00
parent 7a67b12533
commit 0cf06b7bd9
4 changed files with 16 additions and 6 deletions

View File

@@ -1,11 +1,12 @@
import secrets import secrets
from typing import Optional, Literal from typing import Optional, Literal
from httpx_oauth.clients.openid import OpenID
from httpx_oauth.oauth2 import T from httpx_oauth.oauth2 import T
from app.oauth.custom_openid import CustomOpenID
class BankID(OpenID):
class BankID(CustomOpenID):
def __init__(self, client_id: str, client_secret: str): def __init__(self, client_id: str, client_secret: str):
super().__init__( super().__init__(
client_id, client_id,

View File

@@ -0,0 +1,6 @@
from httpx_oauth.clients.openid import OpenID
class CustomOpenID(OpenID):
async def get_user_info(self, token: str) -> dict:
raise NotImplementedError()

View File

@@ -1,11 +1,12 @@
import json import json
from typing import Optional, Literal, Any from typing import Optional, Literal, Any
from httpx_oauth.clients.openid import OpenID
from httpx_oauth.oauth2 import T from httpx_oauth.oauth2 import T
from app.oauth.custom_openid import CustomOpenID
class MojeIDOAuth(OpenID):
class MojeIDOAuth(CustomOpenID):
def __init__(self, client_id: str, client_secret: str): def __init__(self, client_id: str, client_secret: str):
super().__init__( super().__init__(
client_id, client_id,

View File

@@ -10,9 +10,11 @@ from fastapi_users.authentication import (
) )
from fastapi_users.authentication.strategy.jwt import JWTStrategy from fastapi_users.authentication.strategy.jwt import JWTStrategy
from fastapi_users.db import SQLAlchemyUserDatabase from fastapi_users.db import SQLAlchemyUserDatabase
from httpx_oauth.oauth2 import BaseOAuth2
from app.models.user import User from app.models.user import User
from app.oauth.bank_id import BankID from app.oauth.bank_id import BankID
from app.oauth.custom_openid import CustomOpenID
from app.oauth.moje_id import MojeIDOAuth from app.oauth.moje_id import MojeIDOAuth
from app.services.db import get_user_db from app.services.db import get_user_db
from app.core.queue import enqueue_email from app.core.queue import enqueue_email
@@ -34,7 +36,7 @@ providers = {
} }
def get_oauth_provider(name: str) -> Optional[MojeIDOAuth]: def get_oauth_provider(name: str) -> Optional[BaseOAuth2]:
if name not in providers: if name not in providers:
return None return None
return providers[name] return providers[name]
@@ -55,7 +57,7 @@ class UserManager(UUIDIDMixin, BaseUserManager[User, uuid.UUID]):
# set additional user info from the OAuth provider # set additional user info from the OAuth provider
provider = get_oauth_provider(oauth_name) provider = get_oauth_provider(oauth_name)
if provider is not None and hasattr(provider, "get_user_info"): if provider is not None and isinstance(provider, CustomOpenID):
update_dict = await provider.get_user_info(access_token) update_dict = await provider.get_user_info(access_token)
await self.user_db.update(user, update_dict) await self.user_db.update(user, update_dict)