Skip to content

Commit 716f797

Browse files
committed
creating users with no password and with other characters on their email fix
1 parent 00f1a37 commit 716f797

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

src/app/api/v1/oauth.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from abc import ABC
33
from typing import Any
44

5-
from fastapi import APIRouter, Depends, RedirectResponse, Request, Response
5+
from fastapi import APIRouter, Depends, Request, Response
6+
from fastapi.responses import RedirectResponse
67
from fastapi_sso.sso.base import OpenID, SSOBase
78
from fastapi_sso.sso.github import GithubSSO
89
from fastapi_sso.sso.google import GoogleSSO
@@ -100,6 +101,7 @@ async def _get_user_details(self, oauth_user: OpenID) -> UserCreateInternal:
100101
if not oauth_user.email:
101102
raise UnauthorizedException(f"Invalid response from {self.provider_name.title()} OAuth.")
102103
username = oauth_user.email.split("@")[0]
104+
username = "".join(c for c in username.lower() if c.isalnum())
103105
name = oauth_user.display_name or username
104106

105107
return UserCreateInternal(

src/app/api/v1/users.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ async def write_user_internal(user: UserCreate | UserCreateInternal, db: AsyncSe
4242
user_internal_dict["hashed_password"] = get_password_hash(password=user_internal_dict["password"])
4343
del user_internal_dict["password"]
4444
user = UserCreateInternal(**user_internal_dict)
45+
elif isinstance(user, UserCreateInternal) and user.hashed_password is None:
46+
# NULL passwords are only allowed for OAuth users (UserCreateInternal from OAuth flow)
47+
# This is validated here to prevent any other code path from creating passwordless users
48+
pass
4549

4650
created_user = await crud_users.create(db=db, object=user, schema_to_select=UserRead)
4751
if created_user is None:

0 commit comments

Comments
 (0)