mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 13:12:24 +00:00
Remove Linux Do OAuth2 login (#994)
This commit is contained in:
@@ -26,8 +26,6 @@ OAUTH2_GITHUB_CLIENT_ID='test'
|
|||||||
OAUTH2_GITHUB_CLIENT_SECRET='test'
|
OAUTH2_GITHUB_CLIENT_SECRET='test'
|
||||||
OAUTH2_GOOGLE_CLIENT_ID='test'
|
OAUTH2_GOOGLE_CLIENT_ID='test'
|
||||||
OAUTH2_GOOGLE_CLIENT_SECRET='test'
|
OAUTH2_GOOGLE_CLIENT_SECRET='test'
|
||||||
OAUTH2_LINUX_DO_CLIENT_ID='test'
|
|
||||||
OAUTH2_LINUX_DO_CLIENT_SECRET='test'
|
|
||||||
# [ Plugin ] email
|
# [ Plugin ] email
|
||||||
EMAIL_USERNAME=''
|
EMAIL_USERNAME=''
|
||||||
EMAIL_PASSWORD=''
|
EMAIL_PASSWORD=''
|
||||||
|
|||||||
@@ -193,7 +193,6 @@ class Settings(BaseSettings):
|
|||||||
f'{FASTAPI_API_V1_PATH}/auth/login/swagger',
|
f'{FASTAPI_API_V1_PATH}/auth/login/swagger',
|
||||||
f'{FASTAPI_API_V1_PATH}/oauth2/github/callback',
|
f'{FASTAPI_API_V1_PATH}/oauth2/github/callback',
|
||||||
f'{FASTAPI_API_V1_PATH}/oauth2/google/callback',
|
f'{FASTAPI_API_V1_PATH}/oauth2/google/callback',
|
||||||
f'{FASTAPI_API_V1_PATH}/oauth2/linux-do/callback',
|
|
||||||
]
|
]
|
||||||
OPERA_LOG_REDACT_KEYS: list[str] = [
|
OPERA_LOG_REDACT_KEYS: list[str] = [
|
||||||
'password',
|
'password',
|
||||||
@@ -250,15 +249,12 @@ class Settings(BaseSettings):
|
|||||||
OAUTH2_GITHUB_CLIENT_SECRET: str
|
OAUTH2_GITHUB_CLIENT_SECRET: str
|
||||||
OAUTH2_GOOGLE_CLIENT_ID: str
|
OAUTH2_GOOGLE_CLIENT_ID: str
|
||||||
OAUTH2_GOOGLE_CLIENT_SECRET: str
|
OAUTH2_GOOGLE_CLIENT_SECRET: str
|
||||||
OAUTH2_LINUX_DO_CLIENT_ID: str
|
|
||||||
OAUTH2_LINUX_DO_CLIENT_SECRET: str
|
|
||||||
|
|
||||||
# 基础配置
|
# 基础配置
|
||||||
OAUTH2_STATE_REDIS_PREFIX: str = 'fba:oauth2:state'
|
OAUTH2_STATE_REDIS_PREFIX: str = 'fba:oauth2:state'
|
||||||
OAUTH2_STATE_EXPIRE_SECONDS: int = 60 * 3 # 3 分钟
|
OAUTH2_STATE_EXPIRE_SECONDS: int = 60 * 3 # 3 分钟
|
||||||
OAUTH2_GITHUB_REDIRECT_URI: str = 'http://127.0.0.1:8000/api/v1/oauth2/github/callback'
|
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_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_LOGIN_REDIRECT_URI: str = 'http://localhost:5173/oauth2/callback'
|
OAUTH2_FRONTEND_LOGIN_REDIRECT_URI: str = 'http://localhost:5173/oauth2/callback'
|
||||||
OAUTH2_FRONTEND_BINDING_REDIRECT_URI: str = 'http://localhost:5173/profile'
|
OAUTH2_FRONTEND_BINDING_REDIRECT_URI: str = 'http://localhost:5173/profile'
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ from fastapi import APIRouter
|
|||||||
from backend.core.conf import settings
|
from backend.core.conf import settings
|
||||||
from backend.plugin.oauth2.api.v1.github import router as github_router
|
from backend.plugin.oauth2.api.v1.github import router as github_router
|
||||||
from backend.plugin.oauth2.api.v1.google import router as google_router
|
from backend.plugin.oauth2.api.v1.google import router as google_router
|
||||||
from backend.plugin.oauth2.api.v1.linux_do import router as linux_do_router
|
|
||||||
from backend.plugin.oauth2.api.v1.user_social import router as user_social_router
|
from backend.plugin.oauth2.api.v1.user_social import router as user_social_router
|
||||||
|
|
||||||
v1 = APIRouter(prefix=f'{settings.FASTAPI_API_V1_PATH}/oauth2')
|
v1 = APIRouter(prefix=f'{settings.FASTAPI_API_V1_PATH}/oauth2')
|
||||||
@@ -11,4 +10,3 @@ v1 = APIRouter(prefix=f'{settings.FASTAPI_API_V1_PATH}/oauth2')
|
|||||||
v1.include_router(user_social_router, tags=['OAuth2'])
|
v1.include_router(user_social_router, tags=['OAuth2'])
|
||||||
v1.include_router(github_router, prefix='/github', tags=['Github OAuth2'])
|
v1.include_router(github_router, prefix='/github', tags=['Github OAuth2'])
|
||||||
v1.include_router(google_router, prefix='/google', tags=['Google OAuth2'])
|
v1.include_router(google_router, prefix='/google', tags=['Google OAuth2'])
|
||||||
v1.include_router(linux_do_router, prefix='/linux-do', tags=['LinuxDo OAuth2'])
|
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
import json
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
from typing import Annotated
|
|
||||||
|
|
||||||
from fastapi import APIRouter, BackgroundTasks, Depends, Response
|
|
||||||
from fastapi_limiter.depends import RateLimiter
|
|
||||||
from fastapi_oauth20 import FastAPIOAuth20, LinuxDoOAuth20
|
|
||||||
from starlette.responses import RedirectResponse
|
|
||||||
|
|
||||||
from backend.common.response.response_schema import ResponseSchemaModel, response_base
|
|
||||||
from backend.core.conf import settings
|
|
||||||
from backend.database.db import CurrentSessionTransaction
|
|
||||||
from backend.database.redis import redis_client
|
|
||||||
from backend.plugin.oauth2.enums import UserSocialAuthType, UserSocialType
|
|
||||||
from backend.plugin.oauth2.service.oauth2_service import oauth2_service
|
|
||||||
|
|
||||||
router = APIRouter()
|
|
||||||
|
|
||||||
linux_do_client = LinuxDoOAuth20(settings.OAUTH2_LINUX_DO_CLIENT_ID, settings.OAUTH2_LINUX_DO_CLIENT_SECRET)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get('', summary='获取 LinuxDo 授权链接')
|
|
||||||
async def get_linux_do_oauth2_url() -> ResponseSchemaModel[str]:
|
|
||||||
state = str(uuid.uuid4())
|
|
||||||
|
|
||||||
await redis_client.setex(
|
|
||||||
f'{settings.OAUTH2_STATE_REDIS_PREFIX}:{state}',
|
|
||||||
settings.OAUTH2_STATE_EXPIRE_SECONDS,
|
|
||||||
json.dumps({'type': UserSocialAuthType.login.value}),
|
|
||||||
)
|
|
||||||
|
|
||||||
auth_url = await linux_do_client.get_authorization_url(
|
|
||||||
redirect_uri=settings.OAUTH2_LINUX_DO_REDIRECT_URI, state=state
|
|
||||||
)
|
|
||||||
return response_base.success(data=auth_url)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
|
||||||
'/callback',
|
|
||||||
summary='LinuxDo 授权自动重定向',
|
|
||||||
description='LinuxDo 授权后,自动重定向到当前地址并获取用户信息,通过用户信息自动创建系统用户',
|
|
||||||
dependencies=[Depends(RateLimiter(times=5, minutes=1))],
|
|
||||||
)
|
|
||||||
async def linux_do_oauth2_callback( # noqa: ANN201
|
|
||||||
db: CurrentSessionTransaction,
|
|
||||||
response: Response,
|
|
||||||
background_tasks: BackgroundTasks,
|
|
||||||
oauth2: Annotated[
|
|
||||||
FastAPIOAuth20,
|
|
||||||
Depends(FastAPIOAuth20(linux_do_client, redirect_uri=settings.OAUTH2_LINUX_DO_REDIRECT_URI)),
|
|
||||||
],
|
|
||||||
):
|
|
||||||
token_data, state = oauth2
|
|
||||||
access_token = token_data['access_token']
|
|
||||||
user = await linux_do_client.get_userinfo(access_token)
|
|
||||||
data = await oauth2_service.login_or_binding(
|
|
||||||
db=db,
|
|
||||||
response=response,
|
|
||||||
background_tasks=background_tasks,
|
|
||||||
user=user,
|
|
||||||
social=UserSocialType.linux_do,
|
|
||||||
state=state,
|
|
||||||
)
|
|
||||||
|
|
||||||
# 绑定流程
|
|
||||||
if data is None:
|
|
||||||
return RedirectResponse(url=settings.OAUTH2_FRONTEND_BINDING_REDIRECT_URI)
|
|
||||||
|
|
||||||
# 登录流程
|
|
||||||
return RedirectResponse(
|
|
||||||
url=f'{settings.OAUTH2_FRONTEND_LOGIN_REDIRECT_URI}?access_token={data.access_token}&session_uuid={data.session_uuid}',
|
|
||||||
)
|
|
||||||
@@ -6,7 +6,6 @@ class UserSocialType(StrEnum):
|
|||||||
|
|
||||||
github = 'Github'
|
github = 'Github'
|
||||||
google = 'Google'
|
google = 'Google'
|
||||||
linux_do = 'LinuxDo'
|
|
||||||
|
|
||||||
|
|
||||||
class UserSocialAuthType(StrEnum):
|
class UserSocialAuthType(StrEnum):
|
||||||
|
|||||||
@@ -167,9 +167,6 @@ class OAuth2Service:
|
|||||||
username = user.get('name')
|
username = user.get('name')
|
||||||
nickname = user.get('given_name')
|
nickname = user.get('given_name')
|
||||||
avatar = user.get('picture')
|
avatar = user.get('picture')
|
||||||
case UserSocialType.linux_do:
|
|
||||||
sid = user.get('id')
|
|
||||||
nickname = user.get('name')
|
|
||||||
case _:
|
case _:
|
||||||
raise errors.ForbiddenError(msg=f'暂不支持 {social} OAuth2 登录')
|
raise errors.ForbiddenError(msg=f'暂不支持 {social} OAuth2 登录')
|
||||||
|
|
||||||
|
|||||||
@@ -90,13 +90,6 @@ class UserSocialService:
|
|||||||
redirect_uri=settings.OAUTH2_GOOGLE_REDIRECT_URI,
|
redirect_uri=settings.OAUTH2_GOOGLE_REDIRECT_URI,
|
||||||
state=state,
|
state=state,
|
||||||
)
|
)
|
||||||
case UserSocialType.linux_do:
|
|
||||||
from backend.plugin.oauth2.api.v1.linux_do import linux_do_client
|
|
||||||
|
|
||||||
auth_url = await linux_do_client.get_authorization_url(
|
|
||||||
redirect_uri=settings.OAUTH2_LINUX_DO_REDIRECT_URI,
|
|
||||||
state=state,
|
|
||||||
)
|
|
||||||
case _:
|
case _:
|
||||||
raise errors.ForbiddenError(msg=f'暂不支持 {source} 绑定')
|
raise errors.ForbiddenError(msg=f'暂不支持 {source} 绑定')
|
||||||
|
|
||||||
|
|||||||
@@ -26,8 +26,6 @@ OAUTH2_GITHUB_CLIENT_ID='test'
|
|||||||
OAUTH2_GITHUB_CLIENT_SECRET='test'
|
OAUTH2_GITHUB_CLIENT_SECRET='test'
|
||||||
OAUTH2_GOOGLE_CLIENT_ID='test'
|
OAUTH2_GOOGLE_CLIENT_ID='test'
|
||||||
OAUTH2_GOOGLE_CLIENT_SECRET='test'
|
OAUTH2_GOOGLE_CLIENT_SECRET='test'
|
||||||
OAUTH2_LINUX_DO_CLIENT_ID='test'
|
|
||||||
OAUTH2_LINUX_DO_CLIENT_SECRET='test'
|
|
||||||
# [ Plugin ] email
|
# [ Plugin ] email
|
||||||
EMAIL_USERNAME=''
|
EMAIL_USERNAME=''
|
||||||
EMAIL_PASSWORD=''
|
EMAIL_PASSWORD=''
|
||||||
|
|||||||
Reference in New Issue
Block a user