Skip to content

Commit

Permalink
redirect users to Maizey endpoint (iss. #15) (#16)
Browse files Browse the repository at this point in the history
* Redirecting to Maizey URL

* #15 redirecting to maizey endpoint
  • Loading branch information
pushyamig authored Jun 24, 2024
1 parent 1c60fe3 commit f31a69f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
23 changes: 9 additions & 14 deletions lti_redirect/maizey.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import jwt, logging, requests
import jwt, logging
from decouple import config
from jwt.exceptions import InvalidKeyError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -48,19 +49,13 @@ def get_restructured_data(self):
return restructured_data

def send_to_maizey(self):
maizey_url = None
if not self.maizey_jwt_secret:
logger.error("Maizey JWT secret is not configured")
return False
course_jwt = jwt.encode(self.get_restructured_data(), self.maizey_jwt_secret, algorithm='HS256')
maizey_url = f"{self.lti_custom_data['redirect_url']}t2/canvaslink?token={course_jwt}"
try:
response = requests.get(maizey_url)
response.raise_for_status()
# currently response from maizey endpoint upon success is retuning a HTML text
logger.info(f"Maizey response: {response.text}")
return True
except requests.exceptions.RequestException as e:
logger.error(f"Error sending course data to Maizey: {e}")
return False


course_jwt = jwt.encode(self.get_restructured_data(), self.maizey_jwt_secret, algorithm='HS256')
maizey_url = f"{self.lti_custom_data['redirect_url']}t2/canvaslink?token={course_jwt}"
logger.info(f"Maizey with course JWT URL: {maizey_url}")
except (InvalidKeyError,Exception) as e:
logger.error(f"Error encoding course data to JWT: {e}")
return maizey_url
8 changes: 8 additions & 0 deletions lti_redirect/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
<title>{% block title %}{% endblock %}</title>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'home.css' %}"/>
<script type=""text/javascript">
window.onload = function() {
let maizey_url = "{{ maizey_url }}"
if(maizey_url) {
window.open(maizey_url, '_blank')
}
}
</script>
</head>

<body>
Expand Down
12 changes: 10 additions & 2 deletions lti_redirect/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from lti_tool.views import LtiLaunchBaseView
from django.contrib.auth.models import User
from lti_redirect.maizey import SendToMaizey
from django.http import HttpResponseRedirect

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -70,9 +71,16 @@ def handle_resource_launch(self, request, lti_launch):
return redirect("error")
if not login_user_from_lti(request, launch_data):
return redirect("error")
if not SendToMaizey(launch_data).send_to_maizey():
maizey_url = SendToMaizey(launch_data).send_to_maizey()
if not maizey_url:
return redirect("error")
return redirect("home")
context = {
"maizey_url": maizey_url,
}
if request.user.is_superuser:
return render(request, "home.html", context)
else:
return HttpResponseRedirect(maizey_url)

def handle_deep_linking_launch(self, request, lti_launch):
... # Optional.
Expand Down

0 comments on commit f31a69f

Please sign in to comment.