From 21b4f9249aeaed0eadc60db0d984f434031fd083 Mon Sep 17 00:00:00 2001 From: "Matt, Matthias" Date: Thu, 5 Sep 2024 09:33:22 +0200 Subject: [PATCH] Use GRADER_API_TOKEN for authentication --- grader_labextension/__init__.py | 2 +- grader_labextension/handlers/base_handler.py | 42 ++++++++++---------- grader_labextension/services/git.py | 2 +- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/grader_labextension/__init__.py b/grader_labextension/__init__.py index 4ae2e49..212d466 100644 --- a/grader_labextension/__init__.py +++ b/grader_labextension/__init__.py @@ -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") diff --git a/grader_labextension/handlers/base_handler.py b/grader_labextension/handlers/base_handler.py index a8535f7..317265e 100644 --- a/grader_labextension/handlers/base_handler.py +++ b/grader_labextension/handlers/base_handler.py @@ -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", @@ -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): @@ -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)) \ No newline at end of file + """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)) diff --git a/grader_labextension/services/git.py b/grader_labextension/services/git.py index 9dbf322..6297b9a 100644 --- a/grader_labextension/services/git.py +++ b/grader_labextension/services/git.py @@ -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)