-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
centonhuang
committed
Jun 14, 2024
1 parent
91feaae
commit b097ceb
Showing
10 changed files
with
153 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from fastapi import APIRouter | ||
|
||
from .github import github_router | ||
|
||
oauth2_router = APIRouter(prefix="/oauth2", tags=["oauth2"]) | ||
|
||
oauth2_router.include_router(github_router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from datetime import datetime | ||
from typing import Literal | ||
|
||
from src.middleware.jwt import encode_token | ||
from src.middleware.mysql import session | ||
from src.middleware.mysql.models import UserSchema | ||
|
||
|
||
def login(name: str, unique_id: str, avatar: str, platform: Literal["github"]) -> str: | ||
with session() as conn: | ||
if not conn.is_active: | ||
conn.rollback() | ||
conn.close() | ||
else: | ||
conn.commit() | ||
|
||
query = conn.query(UserSchema.uid, UserSchema.is_admin).filter(UserSchema.unique_id == unique_id).filter(UserSchema.platform == platform) | ||
result = query.first() | ||
|
||
if result: | ||
(uid, is_admin) = result | ||
else: | ||
uid, is_admin = register(name, unique_id, avatar, platform), 0 | ||
|
||
with session() as conn: | ||
if not conn.is_active: | ||
conn.rollback() | ||
conn.close() | ||
else: | ||
conn.commit() | ||
|
||
conn.query(UserSchema).filter(UserSchema.uid == uid).update({UserSchema.last_login: datetime.now()}) | ||
conn.flush() | ||
conn.commit() | ||
|
||
return encode_token(uid=uid, level=is_admin) | ||
|
||
|
||
def register(name: str, unique_id: str, avatar: str, platform: Literal["github"]) -> int: | ||
with session() as conn: | ||
if not conn.is_active: | ||
conn.rollback() | ||
conn.close() | ||
else: | ||
conn.commit() | ||
|
||
user = UserSchema( | ||
name=name, | ||
unique_id=unique_id, | ||
avatar=avatar, | ||
platform=platform, | ||
) | ||
conn.add(user) | ||
conn.flush() | ||
conn.commit() | ||
|
||
uid = user.uid | ||
|
||
return uid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from urllib.parse import urlencode | ||
|
||
import requests | ||
from fastapi import APIRouter, HTTPException, status | ||
from fastapi.responses import JSONResponse | ||
|
||
from src.api.model.response import StandardResponse | ||
from src.config.env import OAUTH2_GITHUB_CLIENT_ID, OAUTH2_GITHUB_CLIENT_SECRET | ||
from src.config.gbl import OAUTH2_GITHUB_AUTH_URL, OAUTH2_GITHUB_REDIRECT_URL, OAUTH2_GITHUB_TOKEN_URL, OAUTH2_GITHUB_USER_API | ||
|
||
from .common import login | ||
|
||
github_router = APIRouter(prefix="/github", tags=["oauth2"]) | ||
|
||
|
||
@github_router.get("/login") | ||
async def github_login() -> StandardResponse: | ||
query = { | ||
"client_id": OAUTH2_GITHUB_CLIENT_ID, | ||
"redirect_uri": OAUTH2_GITHUB_REDIRECT_URL, | ||
"scope": "user", | ||
} | ||
return StandardResponse( | ||
code=1, | ||
status="success", | ||
data={"url": f"{OAUTH2_GITHUB_AUTH_URL}?{urlencode(query)}"}, | ||
) | ||
|
||
|
||
@github_router.get("/callback") | ||
async def github_callback(code: str) -> JSONResponse: | ||
if not code: | ||
raise HTTPException(status_code=400, detail="Code is required") | ||
|
||
query = { | ||
"client_id": OAUTH2_GITHUB_CLIENT_ID, | ||
"client_secret": OAUTH2_GITHUB_CLIENT_SECRET, | ||
"code": code, | ||
} | ||
headers = {"Accept": "application/json"} | ||
response = requests.post(OAUTH2_GITHUB_TOKEN_URL, data=query, headers=headers) | ||
|
||
if response.status_code != 200: | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Failed to retrieve access token") | ||
|
||
data = response.json() | ||
access_token = data.get("access_token") | ||
if not access_token: | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Access token is missing in response") | ||
|
||
# Use the access token to get user info | ||
user_response = requests.get(OAUTH2_GITHUB_USER_API, headers={"Authorization": f"Bearer {access_token}"}) | ||
|
||
if user_response.status_code != 200: | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Failed to retrieve user information") | ||
|
||
user_data = user_response.json() | ||
token = login(user_data["login"], user_data["id"], user_data["avatar_url"], "github") | ||
return StandardResponse( | ||
code=1, | ||
status="success", | ||
data={"token": token}, | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters