Skip to content

Commit ca63ad2

Browse files
committed
fix(backend): Throw error on incorrect password
1 parent d28f0f3 commit ca63ad2

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

backend/src/SlcmSwitch.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from bs4 import BeautifulSoup
55
from httpx import AsyncClient
66

7-
from .models import SlcmCookies, SlcmCookiesWithName
7+
from .models import IncorrectPassword, SlcmCookies, SlcmCookiesWithName
88

99

1010
class SlcmSwitch:
@@ -77,6 +77,9 @@ async def student_login(self, username: str, password: str) -> SlcmCookiesWithNa
7777
follow_redirects=False,
7878
)
7979

80+
if res.status_code != 302:
81+
raise IncorrectPassword()
82+
8083
return SlcmCookiesWithName(
8184
verification_token=cookies.verification_token,
8285
asp_net_id=res.cookies["ASP.NET_SessionId"],

backend/src/main.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import uvicorn
2-
from fastapi import FastAPI, HTTPException
3-
from fastapi.encoders import jsonable_encoder
2+
from fastapi import FastAPI
43
from fastapi.middleware.cors import CORSMiddleware
5-
from fastapi.responses import JSONResponse, RedirectResponse
4+
from fastapi.responses import RedirectResponse
65

76
from .routes import router
87

@@ -18,13 +17,6 @@
1817
)
1918

2019

21-
@app.exception_handler(HTTPException)
22-
async def validation_exception_handler(exc: HTTPException):
23-
return JSONResponse(
24-
status_code=exc.status_code, content=jsonable_encoder(exc.detail)
25-
)
26-
27-
2820
@app.get("/")
2921
async def redirect():
3022
return RedirectResponse("https://github.com/whyredfire/betterslcm")

backend/src/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ class StudentLogin(ParentLogin):
3131

3232
class ParentLoginOTP(SlcmCookies):
3333
otp: str = Field(..., description="The OTP for the login")
34+
35+
36+
class IncorrectPassword(Exception):
37+
pass

backend/src/routes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from fastapi import APIRouter
1+
from fastapi import APIRouter, HTTPException
22

3-
from .models import ParentLogin, SlcmCookies, StudentLogin
3+
from .models import IncorrectPassword, ParentLogin, SlcmCookies, StudentLogin
44
from .SlcmSwitch import SlcmSwitch
55

66
router = APIRouter()
@@ -9,8 +9,11 @@
99

1010
@router.post("/login/student")
1111
async def student_login(login: StudentLogin) -> dict[str, str]:
12-
cookies = await slcm.student_login(login.username, login.password)
13-
return cookies.model_dump(by_alias=True)
12+
try:
13+
cookies = await slcm.student_login(login.username, login.password)
14+
return cookies.model_dump(by_alias=True)
15+
except IncorrectPassword:
16+
raise HTTPException(status_code=401, detail="Incorrect Password")
1417

1518

1619
@router.post("/login/parent")

0 commit comments

Comments
 (0)