Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 804b3e6

Browse files
committed
Clean up possible authentication issues
1 parent 805966b commit 804b3e6

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

bookserver/routers/assessment.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# Third-party imports
2323
# -------------------
2424
from bleach import clean
25-
from fastapi import APIRouter, HTTPException, Request, status
25+
from fastapi import APIRouter, Depends, HTTPException, Request, status
2626
from pydantic import BaseModel
2727

2828
# Local application imports
@@ -56,7 +56,8 @@
5656
from ..internal.utils import make_json_response
5757
from ..models import runestone_component_dict
5858
from ..schemas import AssessmentRequest, SelectQRequest
59-
from ..session import is_instructor
59+
from ..session import is_instructor, auth_manager
60+
6061

6162
# Routing
6263
# =======
@@ -73,12 +74,8 @@
7374
async def get_assessment_results(
7475
request_data: AssessmentRequest,
7576
request: Request,
77+
user=Depends(auth_manager),
7678
):
77-
user = request.state.user
78-
if not user:
79-
return make_json_response(
80-
status=status.HTTP_401_UNAUTHORIZED, detail="not logged in"
81-
)
8279
# if the user is not logged in an HTTP 401 will be returned.
8380
# Otherwise if the user is an instructor then use the provided
8481
# sid (it could be any student in the class). If none is provided then
@@ -129,7 +126,9 @@ class HistoryRequest(BaseModel):
129126

130127

131128
@router.post("/gethist")
132-
async def get_history(request: Request, request_data: HistoryRequest):
129+
async def get_history(
130+
request: Request, request_data: HistoryRequest, user=Depends(auth_manager)
131+
):
133132
"""
134133
return the history of saved code by this user for a particular
135134
active code id (acid) -- known as div_id elsewhere
@@ -154,20 +153,18 @@ async def get_history(request: Request, request_data: HistoryRequest):
154153
# if request_data.sid then we know this is being called from the grading interface
155154
# so verify that the actual user is an instructor.
156155
if sid:
157-
if request.state.user and request.state.user.username != sid:
156+
if user.username != sid:
158157
if await is_instructor(request):
159-
course_id = request.state.user.course_id
158+
course_id = user.course_id
160159
else:
161160
raise HTTPException(401)
162161
else:
163162
raise HTTPException(401)
164163
# In this case, the request is simply from a student, so we will use
165164
# their logged in username
166-
elif request.state.user:
167-
sid = request.state.user.username
168-
course_id = request.state.user.course_id
169165
else:
170-
raise HTTPException(401)
166+
sid = user.username
167+
course_id = user.course_id
171168

172169
res: Dict[str, Any] = {}
173170
res["acid"] = acid

0 commit comments

Comments
 (0)