Skip to content

Commit

Permalink
Use GRADER_API_TOKEN for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
meffmadd committed Sep 5, 2024
1 parent fee3997 commit 21b4f92
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion grader_labextension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def get_grader_config():
"GET",
f"{handler_config.service_base_url}/config",
header=dict(
Authorization="Token " + HandlerConfig.instance().hub_api_token),
Authorization="Token " + HandlerConfig.instance().grader_api_token),
)
except HTTPClientError as e:
log.error("Error: could not get grader config")
Expand Down
42 changes: 22 additions & 20 deletions grader_labextension/handlers/base_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class HandlerConfig(SingletonConfigurable):
hub_api_token = Unicode(os.environ.get("JUPYTERHUB_API_TOKEN"),
help="The authorization token to access the hub api").tag(config=True)
hub_user = Unicode(os.environ.get("JUPYTERHUB_USER"), help="The user name in jupyter hub.").tag(config=True)
grader_api_token = Unicode(os.environ.get("GRADER_API_TOKEN"),
help="The authorization token to access the grader service api").tag(config=True)
service_base_url = Unicode(
os.environ.get("GRADER_BASE_URL", "/services/grader"),
help="Base URL to use for each request to the grader service",
Expand Down Expand Up @@ -83,7 +85,7 @@ def grader_authentication_header(self):
:rtype: dict
"""

return dict(Authorization="Token " + HandlerConfig.instance().hub_api_token)
return dict(Authorization="Token " + HandlerConfig.instance().grader_api_token)

@property
def user_name(self):
Expand Down Expand Up @@ -112,23 +114,23 @@ async def get_assignment(self, lecture_id, assignment_id):
except HTTPClientError as e:
self.log.error(e.response)
raise HTTPError(e.code, reason=e.response.reason)

def write_error(self, status_code, **kwargs):
"""APIHandler errors are JSON, not human pages"""
self.set_header("Content-Type", "application/json")
message = responses.get(status_code, "Unknown HTTP Error")
reply: dict = {
"message": message,
}
exc_info = kwargs.get("exc_info")
if exc_info:
e = exc_info[1]
if isinstance(e, HTTPError):
reply["message"] = e.log_message or message
reply["reason"] = e.reason
else:
reply["message"] = "Unhandled error"
reply["reason"] = None
reply["traceback"] = "".join(traceback.format_exception(*exc_info))
self.log.warning("wrote error: %r", reply["message"], exc_info=True)
self.finish(json.dumps(reply))
"""APIHandler errors are JSON, not human pages"""
self.set_header("Content-Type", "application/json")
message = responses.get(status_code, "Unknown HTTP Error")
reply: dict = {
"message": message,
}
exc_info = kwargs.get("exc_info")
if exc_info:
e = exc_info[1]
if isinstance(e, HTTPError):
reply["message"] = e.log_message or message
reply["reason"] = e.reason
else:
reply["message"] = "Unhandled error"
reply["reason"] = None
reply["traceback"] = "".join(traceback.format_exception(*exc_info))
self.log.warning("wrote error: %r", reply["message"], exc_info=True)
self.finish(json.dumps(reply))
2 changes: 1 addition & 1 deletion grader_labextension/services/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RemoteFileStatus(enum.Enum):
divergent = 3

class GitService(Configurable):
git_access_token = Unicode(os.environ.get("JUPYTERHUB_API_TOKEN"), allow_none=False).tag(config=True)
git_access_token = Unicode(os.environ.get("GRADER_API_TOKEN"), allow_none=False).tag(config=True)
git_service_url = Unicode(
f'{os.environ.get("GRADER_HOST_URL", "http://127.0.0.1:4010")}{os.environ.get("GRADER_GIT_BASE_URL", "/services/grader/git")}',
allow_none=False).tag(config=True)
Expand Down

0 comments on commit 21b4f92

Please sign in to comment.