完成部分Github登录回调
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi.responses import PlainTextResponse
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi.responses import PlainTextResponse, RedirectResponse
|
||||
from middleware.auth import SignRequired
|
||||
from models.response import ResponseModel
|
||||
import service.oauth
|
||||
|
||||
callback_router = APIRouter(
|
||||
prefix='/callback',
|
||||
@@ -41,19 +42,79 @@ def router_callback_qq() -> ResponseModel:
|
||||
"""
|
||||
pass
|
||||
|
||||
@oauth_router.post(
|
||||
@oauth_router.get(
|
||||
path='/github',
|
||||
summary='GitHub OAuth 回调',
|
||||
description='Handle GitHub OAuth callback and return user information.',
|
||||
)
|
||||
def router_callback_github() -> ResponseModel:
|
||||
async def router_callback_github(
|
||||
code: str = Query(description="The token received from GitHub for authentication.")) -> PlainTextResponse:
|
||||
"""
|
||||
Handle GitHub OAuth callback and return user information.
|
||||
GitHub OAuth 回调处理
|
||||
|
||||
- Github 成功响应:
|
||||
- JWT: {"access_token": "gho_xxxxxxxx", "token_type": "bearer", "scope": ""}
|
||||
- User Info:{
|
||||
"code": "grfessg1312432313421fdgs",
|
||||
"user_data": {
|
||||
"login": "Yuerchu",
|
||||
"id": 114514,
|
||||
"node_id": "xxxxx",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/114514?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/Yuerchu",
|
||||
"html_url": "https://github.com/Yuerchu",
|
||||
"followers_url": "https://api.github.com/users/Yuerchu/followers",
|
||||
"following_url": "https://api.github.com/users/Yuerchu/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/Yuerchu/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/Yuerchu/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/Yuerchu/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/Yuerchu/orgs",
|
||||
"repos_url": "https://api.github.com/users/Yuerchu/repos",
|
||||
"events_url": "https://api.github.com/users/Yuerchu/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/Yuerchu/received_events",
|
||||
"type": "User",
|
||||
"user_view_type": "public",
|
||||
"site_admin": false,
|
||||
"name": "于小丘",
|
||||
"company": null,
|
||||
"blog": "https://www.yxqi.cn",
|
||||
"location": "ChangSha, HuNan, China",
|
||||
"email": "admin@yuxiaoqiu.cn",
|
||||
"hireable": null,
|
||||
"bio": null,
|
||||
"twitter_username": null,
|
||||
"notification_email": "admin@yuxiaoqiu.cn",
|
||||
"public_repos": 17,
|
||||
"public_gists": 0,
|
||||
"followers": 8,
|
||||
"following": 8,
|
||||
"created_at": "2019-04-13T11:17:33Z",
|
||||
"updated_at": "2025-08-20T03:03:16Z"
|
||||
}
|
||||
}
|
||||
- 错误响应示例:
|
||||
- {
|
||||
'error': 'bad_verification_code',
|
||||
'error_description': 'The code passed is incorrect or expired.',
|
||||
'error_uri': 'https://docs.github.com/apps/managing-oauth-apps/troubleshooting-oauth-app-access-token-request-errors/#bad-verification-code'
|
||||
}
|
||||
|
||||
Returns:
|
||||
ResponseModel: A model containing the response data for the GitHub OAuth callback.
|
||||
PlainTextResponse: A response containing the user information from GitHub.
|
||||
"""
|
||||
pass
|
||||
try:
|
||||
access_token = await service.oauth.github.get_access_token(code)
|
||||
# [TODO] 把access_token写数据库里
|
||||
if not access_token:
|
||||
return PlainTextResponse("Failed to retrieve access token from GitHub.", status_code=400)
|
||||
|
||||
user_data = await service.oauth.github.get_user_info(access_token.access_token)
|
||||
# [TODO] 把user_data写数据库里
|
||||
|
||||
return PlainTextResponse(f"User information processed successfully, code: {code}, user_data: {user_data.json_dump()}", status_code=200)
|
||||
except Exception as e:
|
||||
return PlainTextResponse(f"An error occurred: {str(e)}", status_code=500)
|
||||
|
||||
@pay_router.post(
|
||||
path='/alipay',
|
||||
|
||||
@@ -3,10 +3,10 @@ from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from middleware.auth import AuthRequired, SignRequired
|
||||
import models
|
||||
from models.response import ResponseModel, TokenModel, userModel, groupModel
|
||||
from models.response import ResponseModel, TokenModel, userModel, groupModel, UserSettingModel
|
||||
from deprecated import deprecated
|
||||
from pkg.log import log
|
||||
import service.user.login
|
||||
import service
|
||||
|
||||
user_router = APIRouter(
|
||||
prefix="/user",
|
||||
@@ -30,7 +30,7 @@ async def router_user_session(
|
||||
username = form_data.username
|
||||
password = form_data.password
|
||||
|
||||
user = await service.user.login.login(username=username, password=password)
|
||||
user = await service.user.Login(username=username, password=password)
|
||||
|
||||
if user is None:
|
||||
raise HTTPException(status_code=400, detail="Invalid username or password")
|
||||
@@ -38,8 +38,11 @@ async def router_user_session(
|
||||
raise HTTPException(status_code=400, detail="User account is not fully registered")
|
||||
elif user == 2:
|
||||
raise HTTPException(status_code=403, detail="User account is banned")
|
||||
|
||||
return user
|
||||
elif isinstance(user, TokenModel):
|
||||
return user
|
||||
else:
|
||||
log.error(f"Unexpected return type from login service: {type(user)}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error during login")
|
||||
|
||||
@user_router.post(
|
||||
path='/',
|
||||
@@ -83,6 +86,10 @@ def router_user_reset() -> ResponseModel:
|
||||
"""
|
||||
pass
|
||||
|
||||
@deprecated(
|
||||
version="0.0.1",
|
||||
reason="邮件中带链接的激活易使得被收件服务器误判为垃圾邮件,新版更换为验证码方式"
|
||||
)
|
||||
@user_router.patch(
|
||||
path='/reset',
|
||||
summary='通过邮件里的链接重设密码',
|
||||
@@ -216,10 +223,10 @@ async def router_user_me(
|
||||
user: Annotated[models.user.User, Depends(AuthRequired)],
|
||||
) -> ResponseModel:
|
||||
"""
|
||||
Get user information.
|
||||
获取用户信息.
|
||||
|
||||
Returns:
|
||||
dict: A dictionary containing user information.
|
||||
:return: ResponseModel containing user information.
|
||||
:rtype: ResponseModel
|
||||
"""
|
||||
|
||||
group = await models.Group.get(id=user.group_id)
|
||||
@@ -252,14 +259,22 @@ async def router_user_me(
|
||||
description='Get user storage information.',
|
||||
dependencies=[Depends(SignRequired)],
|
||||
)
|
||||
def router_user_storage() -> ResponseModel:
|
||||
def router_user_storage(
|
||||
user: Annotated[models.user.User, Depends(AuthRequired)],
|
||||
) -> ResponseModel:
|
||||
"""
|
||||
Get user storage information.
|
||||
|
||||
Returns:
|
||||
dict: A dictionary containing user storage information.
|
||||
"""
|
||||
pass
|
||||
return ResponseModel(
|
||||
data={
|
||||
"used": 0,
|
||||
"free": 0,
|
||||
"total": 0,
|
||||
}
|
||||
)
|
||||
|
||||
@user_router.put(
|
||||
path='/authn/start',
|
||||
@@ -346,9 +361,9 @@ def router_user_settings() -> ResponseModel:
|
||||
Get current user settings.
|
||||
|
||||
Returns:
|
||||
dict: A dictionary containing the user's current settings.
|
||||
dict: A dictionary containing the current user settings.
|
||||
"""
|
||||
pass
|
||||
return ResponseModel(data=UserSettingModel().model_dump())
|
||||
|
||||
@user_settings_router.post(
|
||||
path='/avatar',
|
||||
|
||||
Reference in New Issue
Block a user