Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add handlers for dynamic registration #114

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions pylti1p3/contrib/django/lti1p3_tool_config/dynamic_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from django.urls import reverse_lazy
from django.templatetags.static import static
import json
from .models import LtiTool, LtiToolKey
from pylti1p3.dynamic_registration import DynamicRegistration, generate_key_pair
from typing import Any, Dict

class DjangoDynamicRegistration(DynamicRegistration):

initiate_login_url = reverse_lazy('lti:login')

jwks_url = reverse_lazy('lti:jwks')

launch_url = reverse_lazy('lti:launch')

# The path of the tool's logo image, under ``STATIC_ROOT``.
logo_file = 'lti/logo.png'

def __init__(self, request):
super().__init__()

self.request = request

def get_issuer_keys(self, issuer_name : str):
key_obj, created = LtiToolKey.objects.get_or_create(name=issuer_name)
if created:
private_key, public_key = generate_key_pair()
key_obj.private_key = private_key
key_obj.public_key = public_key
key_obj.save()
return key_obj

def get_initiate_login_uri(self) -> str:
return self.request.build_absolute_uri(str(self.initiate_login_url))

def get_jwks_uri(self) -> str:
return self.request.build_absolute_uri(str(self.jwks_url))

def get_redirect_uris(self) -> list[str]:
return [self.get_target_link_uri()]

def get_domain(self) -> str:
return self.request.get_host()

def get_target_link_uri(self) -> str:
return self.request.build_absolute_uri(self.launch_url)

def get_logo_uri(self) -> str:
return self.request.build_absolute_uri(static(self.logo_file))

def get_openid_configuration_endpoint(self):
return self.request.GET.get('openid_configuration')

def get_registration_token(self):
return self.request.GET.get('registration_token')

def get_platform_name(self, openid_configuration: Dict[str, Any]) -> str:
"""
Get the name of the platform this tool is registering with.
"""
return openid_configuration.get("https://purl.imsglobal.org/spec/lti-platform-configuration",{}).get("product_family_code",'')

def complete_registration(self, openid_configuration: Dict[str, Any], openid_registration: Dict[str, Any]):
title = self.get_platform_name(openid_configuration)

tool_key = self.get_issuer_keys(openid_configuration['issuer'])

tool_spec = "https://purl.imsglobal.org/spec/lti-tool-configuration"
deployment_ids = [openid_registration[tool_spec]['deployment_id']]

platform_config, created = LtiTool.objects.update_or_create(
issuer=openid_configuration['issuer'],
client_id=openid_registration['client_id'],
defaults = {
'title': title,
'auth_login_url': openid_configuration['authorization_endpoint'],
'auth_token_url': openid_configuration['token_endpoint'],
'auth_audience': openid_configuration['token_endpoint'],
'key_set_url': openid_configuration['jwks_uri'],
'tool_key': tool_key,
'deployment_ids': json.dumps(deployment_ids),
}
)

platform_config.save() # type: ignore
return platform_config

def keys_for_issuer(issuer_name: str) -> LtiToolKey:
"""
Get the public and private keys for a given issuer.

If they don't exist yet, then create them.
"""
key_obj, created = LtiToolKey.objects.get_or_create(name=issuer_name)
if created:
key_pair = generate_key_pair()
key_obj.private_key = key_pair['private']
key_obj.public_key = key_pair['public']
key_obj.save()
return key_obj

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.1.7 on 2023-02-21 08:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('lti1p3_tool_config', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='ltitool',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='ltitoolkey',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
Loading