Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion backend/core/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

##################################################
Expand Down
14 changes: 6 additions & 8 deletions backend/plugin/oauth2/api/v1/github.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)


Expand All @@ -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,
Expand Down
14 changes: 6 additions & 8 deletions backend/plugin/oauth2/api/v1/google.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)


Expand All @@ -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,
Expand Down
14 changes: 6 additions & 8 deletions backend/plugin/oauth2/api/v1/linux_do.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)


Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion backend/plugin/oauth2/plugin.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[plugin]
summary = 'OAuth 2.0'
version = '0.0.6'
version = '0.0.7'
description = '通过 OAuth 2.0 的方式登录系统'
author = 'wu-clan'

Expand Down
2 changes: 1 addition & 1 deletion backend/plugin/oauth2/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fastapi-oauth20>=0.0.1
fastapi-oauth20>=0.0.2