Skip to content

Commit

Permalink
fix: logout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai committed Dec 26, 2024
1 parent 2e4b284 commit 88b6ece
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
20 changes: 19 additions & 1 deletion server/auth/clients/auth0.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from fastapi.responses import RedirectResponse
import httpx
import secrets

Expand Down Expand Up @@ -35,9 +36,26 @@ def __init__(self):

async def login(self, request):
return await self._client.auth0.authorize_redirect(
request, redirect_uri=CALLBACK_URL
request, redirect_uri=CALLBACK_URL, prompt='login'
)

async def logout(self, request, redirect):
url = f'https://{AUTH0_DOMAIN}/v2/logout'
headers = {"content-type": "application/x-www-form-urlencoded"}

Check warning on line 44 in server/auth/clients/auth0.py

View check run for this annotation

Codecov / codecov/patch

server/auth/clients/auth0.py#L42-L44

Added lines #L42 - L44 were not covered by tests
data = {
'client_id': CLIENT_ID,
'returnTo': redirect,
}

async with httpx.AsyncClient() as client:
resp = await client.get(url, params=data, headers=headers)
if redirect:

Check warning on line 52 in server/auth/clients/auth0.py

View check run for this annotation

Codecov / codecov/patch

server/auth/clients/auth0.py#L50-L52

Added lines #L50 - L52 were not covered by tests
if resp.status_code == 302:
return RedirectResponse(url=resp.headers['Location'])
else:
return RedirectResponse(url=redirect) # 如果出错,直接重定向到 fallback 地址
return {"success": True}

Check warning on line 58 in server/auth/clients/auth0.py

View check run for this annotation

Codecov / codecov/patch

server/auth/clients/auth0.py#L55-L58

Added lines #L55 - L58 were not covered by tests
async def get_oauth_token(self):
url = f'https://{AUTH0_DOMAIN}/oauth/token'
headers = {"content-type": "application/x-www-form-urlencoded"}
Expand Down
4 changes: 4 additions & 0 deletions server/auth/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ async def anonymouseLogin(self, request: Request) -> dict:
async def login(self, request: Request):
pass

@abstractmethod
async def logout(self, request: Request, redirect: str):
pass

Check warning on line 43 in server/auth/clients/base.py

View check run for this annotation

Codecov / codecov/patch

server/auth/clients/base.py#L43

Added line #L43 was not covered by tests

@abstractmethod
async def get_oauth_token(self) -> str:
pass
Expand Down
5 changes: 5 additions & 0 deletions server/auth/clients/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ async def login(self, request: Request):
request.session["user"] = data

return RedirectResponse(url=f"{WEB_LOGIN_SUCCESS_URL}", status_code=302)

async def logout(self, request: Request, redirect: str):
if redirect:
return RedirectResponse(url=f"{redirect}", status_code=302)
return {"success": True}

Check warning on line 28 in server/auth/clients/local.py

View check run for this annotation

Codecov / codecov/patch

server/auth/clients/local.py#L26-L28

Added lines #L26 - L28 were not covered by tests

async def get_user_info(self, user_id):
token = PETERCAT_LOCAL_UID
Expand Down
6 changes: 2 additions & 4 deletions server/auth/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ async def login(request: Request, auth_client = Depends(get_auth_client)):
return await auth_client.login(request)

@router.get("/logout")
async def logout(request: Request):
async def logout(request: Request, auth_client = Depends(get_auth_client)):
request.session.pop("user", None)
redirect = request.query_params.get("redirect")
if redirect:
return RedirectResponse(url=f"{redirect}", status_code=302)
return {"success": True}
return await auth_client.logout(request, redirect)

Check warning on line 38 in server/auth/router.py

View check run for this annotation

Codecov / codecov/patch

server/auth/router.py#L38

Added line #L38 was not covered by tests


@router.get("/callback")
Expand Down

0 comments on commit 88b6ece

Please sign in to comment.