diff --git a/backend/core/conf.py b/backend/core/conf.py index 0ca91655..29194fa8 100644 --- a/backend/core/conf.py +++ b/backend/core/conf.py @@ -231,7 +231,9 @@ class Settings(BaseSettings): OAUTH2_LINUX_DO_CLIENT_SECRET: str # 基础配置 - OAUTH2_BACKEND_BASE_URL: str = 'http://127.0.0.1:8000' + OAUTH2_GITHUB_REDIRECT_URI: str = 'http://127.0.0.1:8000/api/v1/oauth2/github/callback' + OAUTH2_GOOGLE_REDIRECT_URI: str = 'http://127.0.0.1:8000/api/v1/oauth2/google/callback' + OAUTH2_LINUX_DO_REDIRECT_URI: str = 'http://127.0.0.1:8000/api/v1/oauth2/linux-do/callback' OAUTH2_FRONTEND_REDIRECT_URI: str = 'http://localhost:5173/oauth2/callback' ################################################## diff --git a/backend/plugin/oauth2/api/v1/github.py b/backend/plugin/oauth2/api/v1/github.py index 0456931e..70226eb5 100644 --- a/backend/plugin/oauth2/api/v1/github.py +++ b/backend/plugin/oauth2/api/v1/github.py @@ -1,6 +1,6 @@ from typing import Annotated -from fastapi import APIRouter, BackgroundTasks, Depends, Request, Response +from fastapi import APIRouter, BackgroundTasks, Depends, Response from fastapi_limiter.depends import RateLimiter from fastapi_oauth20 import FastAPIOAuth20, GitHubOAuth20 from starlette.responses import RedirectResponse @@ -17,10 +17,8 @@ @router.get('', summary='获取 Github 授权链接') -async def get_github_oauth2_url(request: Request) -> ResponseSchemaModel[str]: - auth_url = await github_client.get_authorization_url( - redirect_uri=f'{settings.OAUTH2_BACKEND_BASE_URL}{request.url.path}/callback' - ) +async def get_github_oauth2_url() -> ResponseSchemaModel[str]: + auth_url = await github_client.get_authorization_url(redirect_uri=settings.OAUTH2_GITHUB_REDIRECT_URI) return response_base.success(data=auth_url) @@ -36,11 +34,11 @@ async def github_oauth2_callback( # noqa: ANN201 background_tasks: BackgroundTasks, oauth2: Annotated[ FastAPIOAuth20, - Depends(FastAPIOAuth20(github_client, redirect_route_name='github_oauth2_callback')), + Depends(FastAPIOAuth20(github_client, redirect_uri=settings.OAUTH2_GITHUB_REDIRECT_URI)), ], ): - token, _state = oauth2 - access_token = token['access_token'] + token_data, _state = oauth2 + access_token = token_data['access_token'] user = await github_client.get_userinfo(access_token) data = await oauth2_service.create_with_login( db=db, diff --git a/backend/plugin/oauth2/api/v1/google.py b/backend/plugin/oauth2/api/v1/google.py index 5d302f2a..4a649f9e 100644 --- a/backend/plugin/oauth2/api/v1/google.py +++ b/backend/plugin/oauth2/api/v1/google.py @@ -1,6 +1,6 @@ from typing import Annotated -from fastapi import APIRouter, BackgroundTasks, Depends, Request, Response +from fastapi import APIRouter, BackgroundTasks, Depends, Response from fastapi_limiter.depends import RateLimiter from fastapi_oauth20 import FastAPIOAuth20, GoogleOAuth20 from starlette.responses import RedirectResponse @@ -17,10 +17,8 @@ @router.get('', summary='获取 google 授权链接') -async def get_google_oauth2_url(request: Request) -> ResponseSchemaModel[str]: - auth_url = await google_client.get_authorization_url( - redirect_uri=f'{settings.OAUTH2_BACKEND_BASE_URL}{request.url.path}/callback' - ) +async def get_google_oauth2_url() -> ResponseSchemaModel[str]: + auth_url = await google_client.get_authorization_url(redirect_uri=settings.OAUTH2_GOOGLE_REDIRECT_URI) return response_base.success(data=auth_url) @@ -36,11 +34,11 @@ async def google_oauth2_callback( # noqa: ANN201 background_tasks: BackgroundTasks, oauth2: Annotated[ FastAPIOAuth20, - Depends(FastAPIOAuth20(google_client, redirect_route_name='google_oauth2_callback')), + Depends(FastAPIOAuth20(google_client, redirect_uri=settings.OAUTH2_GOOGLE_REDIRECT_URI)), ], ): - token, _state = oauth2 - access_token = token['access_token'] + token_data, _state = oauth2 + access_token = token_data['access_token'] user = await google_client.get_userinfo(access_token) data = await oauth2_service.create_with_login( db=db, diff --git a/backend/plugin/oauth2/api/v1/linux_do.py b/backend/plugin/oauth2/api/v1/linux_do.py index 0c3e9a8b..3152584a 100644 --- a/backend/plugin/oauth2/api/v1/linux_do.py +++ b/backend/plugin/oauth2/api/v1/linux_do.py @@ -1,6 +1,6 @@ from typing import Annotated -from fastapi import APIRouter, BackgroundTasks, Depends, Request, Response +from fastapi import APIRouter, BackgroundTasks, Depends, Response from fastapi_limiter.depends import RateLimiter from fastapi_oauth20 import FastAPIOAuth20, LinuxDoOAuth20 from starlette.responses import RedirectResponse @@ -17,10 +17,8 @@ @router.get('', summary='获取 LinuxDo 授权链接') -async def get_linux_do_oauth2_url(request: Request) -> ResponseSchemaModel[str]: - auth_url = await linux_do_client.get_authorization_url( - redirect_uri=f'{settings.OAUTH2_BACKEND_BASE_URL}{request.url.path}/callback' - ) +async def get_linux_do_oauth2_url() -> ResponseSchemaModel[str]: + auth_url = await linux_do_client.get_authorization_url(redirect_uri=settings.OAUTH2_LINUX_DO_REDIRECT_URI) return response_base.success(data=auth_url) @@ -36,11 +34,11 @@ async def linux_do_oauth2_callback( # noqa: ANN201 background_tasks: BackgroundTasks, oauth2: Annotated[ FastAPIOAuth20, - Depends(FastAPIOAuth20(linux_do_client, redirect_route_name='linux_do_oauth2_callback')), + Depends(FastAPIOAuth20(linux_do_client, redirect_uri=settings.OAUTH2_LINUX_DO_REDIRECT_URI)), ], ): - token, _state = oauth2 - access_token = token['access_token'] + token_data, _state = oauth2 + access_token = token_data['access_token'] user = await linux_do_client.get_userinfo(access_token) data = await oauth2_service.create_with_login( db=db, diff --git a/backend/plugin/oauth2/plugin.toml b/backend/plugin/oauth2/plugin.toml index 64c7126f..8b6e3f24 100644 --- a/backend/plugin/oauth2/plugin.toml +++ b/backend/plugin/oauth2/plugin.toml @@ -1,6 +1,6 @@ [plugin] summary = 'OAuth 2.0' -version = '0.0.6' +version = '0.0.7' description = '通过 OAuth 2.0 的方式登录系统' author = 'wu-clan' diff --git a/backend/plugin/oauth2/requirements.txt b/backend/plugin/oauth2/requirements.txt index 128d4062..58055ee0 100644 --- a/backend/plugin/oauth2/requirements.txt +++ b/backend/plugin/oauth2/requirements.txt @@ -1 +1 @@ -fastapi-oauth20>=0.0.1 +fastapi-oauth20>=0.0.2