Skip to content

Commit

Permalink
feat(oauth): add google login
Browse files Browse the repository at this point in the history
  • Loading branch information
MorvanZhou committed May 21, 2024
1 parent cf859da commit 4bbddb9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/retk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Settings(BaseSettings):
OAUTH_CLIENT_SEC_QQ: str = Field(env='OAUTH_CLIENT_SEC_QQ', default="")
OAUTH_CLIENT_ID_FACEBOOK: str = Field(env='OAUTH_CLIENT_ID_QQ', default="")
OAUTH_CLIENT_SEC_FACEBOOK: str = Field(env='OAUTH_CLIENT_SEC_QQ', default="")
OAUTH_CLIENT_ID_GOOGLE: str = Field(env='OAUTH_CLIENT_ID_GOOGLE', default="")
OAUTH_CLIENT_SEC_GOOGLE: str = Field(env='OAUTH_CLIENT_SEC_GOOGLE', default="")
COS_SECRET_ID: str = Field(env='COS_SECRET_ID', default="")
COS_SECRET_KEY: str = Field(env="COS_SECRET_KEY", default="")
COS_REGION: str = Field(env="COS_REGION", default="")
Expand Down
18 changes: 16 additions & 2 deletions src/retk/controllers/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,29 @@ async def __get_then_set_github_user_id(au: AuthedUser, req: schemas.manager.Get
req.uid = u["id"]


async def __get_then_set_google_user_id(req: schemas.manager.GetUserRequest):
# req.google is a email
u, code = await user.get_google_account_by_email(
email=req.google,
disabled=None,
exclude_manager=True,
)
if code == const.CodeEnum.OK:
req.uid = u["id"]


async def __check_user_uid(au: AuthedUser, req: schemas.manager.GetUserRequest) -> bool:
if req.uid is None and req.email is None and req.github is None:
# if all req.dict().values() is None, raise exception
if all(v is None for v in req.dict().values()):
raise json_exception(
request_id=au.request_id,
code=const.CodeEnum.INVALID_PARAMS,
log_msg="uid and email and github can't be all None",
log_msg="uid and email and github and google can't be all None",
)
if req.github is not None:
await __get_then_set_github_user_id(au=au, req=req)
elif req.google is not None:
await __get_then_set_google_user_id(req=req)
return req.uid is not None


Expand Down
9 changes: 9 additions & 0 deletions src/retk/controllers/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from retk.depend.sso.base import SSOLoginError, SSOBase
from retk.depend.sso.facebook import FacebookSSO
from retk.depend.sso.github import GithubSSO
from retk.depend.sso.google import GoogleSSO
# from retk.depend.sso.qq import QQSSO
from .schemas.oauth import OAuthResponse

Expand All @@ -32,10 +33,18 @@ def init_oauth_provider_map():
allow_insecure_http=False,
use_state=False,
),
"google": GoogleSSO(
client_id=config.get_settings().OAUTH_CLIENT_ID_GOOGLE,
client_secret=config.get_settings().OAUTH_CLIENT_SEC_GOOGLE,
redirect_uri=f"{config.get_settings().OAUTH_REDIRECT_URL}/google",
allow_insecure_http=False,
use_state=False,
),
})
user_source_map.update({
"github": const.UserSourceEnum.GITHUB.value,
"facebook": const.UserSourceEnum.FACEBOOK.value,
"google": const.UserSourceEnum.GOOGLE.value,
})


Expand Down
1 change: 1 addition & 0 deletions src/retk/controllers/schemas/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GetUserRequest(BaseModel):
email: Optional[str] = Field(max_length=settings.EMAIL_MAX_LENGTH, default=None)
github: Optional[str] = Field(max_length=50, default=None)
uid: Optional[str] = Field(max_length=settings.UID_MAX_LENGTH, default=None)
google: Optional[str] = Field(max_length=settings.EMAIL_MAX_LENGTH, default=None)


class GetManagerDataResponse(BaseModel):
Expand Down
2 changes: 2 additions & 0 deletions src/retk/core/account/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ async def __delete_post_process(uid: str):
await client.coll.nodes.delete_many({"uid": uid})
await client.coll.user_file.delete_many({"uid": uid})
await client.coll.import_data.delete_many({"uid": uid})
await client.coll.notice_system.delete_many({"recipientId": uid})
await client.coll.user_behavior.delete_many({"uid": uid})
await client.search.force_delete_all(uid=uid)


Expand Down
24 changes: 21 additions & 3 deletions src/retk/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async def patch( # noqa: C901
return await get(uid=au.u.id, disabled=None)


def __get_user_condition(condition: dict, exclude_manager: bool) -> dict:
def __get_condition_filter_manager(condition: dict, exclude_manager: bool) -> dict:
if exclude_manager:
condition["type"] = {"$nin": [
const.user_types.USER_TYPE.ADMIN.id,
Expand Down Expand Up @@ -164,7 +164,25 @@ async def get_account(
c = {"source": source, "account": account}
if disabled is not None:
c["disabled"] = disabled
c = __get_user_condition(condition=c, exclude_manager=False) # exclude_manager)
c = __get_condition_filter_manager(condition=c, exclude_manager=False) # exclude_manager)
u = await client.coll.users.find_one(c)
if u is None:
return None, const.CodeEnum.ACCOUNT_OR_PASSWORD_ERROR
return u, const.CodeEnum.OK


async def get_google_account_by_email(
email: str,
disabled: Optional[bool] = False,
exclude_manager: bool = False,
) -> Tuple[Optional[tps.UserMeta], const.CodeEnum]:
c = {
"source": const.UserSourceEnum.GOOGLE.value,
"email": email,
}
if disabled is not None:
c["disabled"] = disabled
c = __get_condition_filter_manager(condition=c, exclude_manager=exclude_manager)
u = await client.coll.users.find_one(c)
if u is None:
return None, const.CodeEnum.ACCOUNT_OR_PASSWORD_ERROR
Expand All @@ -179,7 +197,7 @@ async def get(
c = {"id": uid}
if disabled is not None:
c["disabled"] = disabled
c = __get_user_condition(condition=c, exclude_manager=exclude_manager)
c = __get_condition_filter_manager(condition=c, exclude_manager=exclude_manager)
u = await client.coll.users.find_one(c)
if u is None:
return None, const.CodeEnum.USER_NOT_EXIST
Expand Down
4 changes: 2 additions & 2 deletions src/retk/routes/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
response_model=co.OAuthResponse,
)
@utils.measure_time_spend
async def login_github(
async def login_provider(
provider: str = Annotated[str, Path(title="The provider name", max_length=40)],
) -> co.OAuthResponse:
return await co.login_provider(provider_name=provider)
Expand All @@ -29,7 +29,7 @@ async def login_github(
status_code=200,
)
@utils.measure_time_spend
async def callback_github(
async def callback_provider(
request: Request,
provider: str = Annotated[str, Path(title="The provider name", max_length=40)],
) -> JSONResponse:
Expand Down

0 comments on commit 4bbddb9

Please sign in to comment.