36 lines
924 B
Python
36 lines
924 B
Python
|
|
"""
|
||
|
|
Module specific business logic for user module
|
||
|
|
|
||
|
|
Functions:
|
||
|
|
- add_user_to_db
|
||
|
|
|
||
|
|
Exports:
|
||
|
|
- add_user_to_db
|
||
|
|
"""
|
||
|
|
from authlib.jose import JWTClaims
|
||
|
|
from fastapi import HTTPException
|
||
|
|
|
||
|
|
from src.user.schemas import OIDCUser
|
||
|
|
from src.user.models import User
|
||
|
|
from src.database import get_db
|
||
|
|
|
||
|
|
|
||
|
|
async def add_user_to_db(user_claims: JWTClaims) -> int:
|
||
|
|
try:
|
||
|
|
valid_user = OIDCUser(first_name=user_claims["given_name"], last_name=user_claims["family_name"], email=user_claims["email"], oidc_id=user_claims["sub"])
|
||
|
|
except Exception as e:
|
||
|
|
print(e)
|
||
|
|
raise HTTPException(status_code=422, detail="Invalid or missing OIDC data")
|
||
|
|
|
||
|
|
db = next(get_db())
|
||
|
|
db_user = db.query(User).filter(User.oidc_id == valid_user.oidc_id).first()
|
||
|
|
|
||
|
|
if not db_user:
|
||
|
|
user_model = User(**valid_user.model_dump())
|
||
|
|
db.add(user_model)
|
||
|
|
db.commit()
|
||
|
|
return user_model.id
|
||
|
|
else:
|
||
|
|
# Verify details still match and update accordingly.
|
||
|
|
return db_user.id
|