-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Pearson-Advance/vue/PADV-262
PADV-262: Import required utilities for LTI 1.3 authentication
- Loading branch information
Showing
16 changed files
with
267 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""Authentication for `openedx_lti_tool_plugin`.""" | ||
from django.contrib.auth.backends import ModelBackend | ||
|
||
from .models import LtiProfile # pylint: disable=unused-import | ||
|
||
|
||
class LtiAuthenticationBackend(ModelBackend): | ||
"""Custom LTI 1.3 Django authentication backend. | ||
Returns a user platform if any LTI profile instance matches | ||
with the requested LTI user identity claims (iss, aud, sub). | ||
Returns None if no user profile is found. | ||
""" | ||
|
||
# pylint: disable=arguments-renamed | ||
def authenticate(self, request, iss=None, aud=None, sub=None, **kwargs): | ||
"""Authenticate using LTI launch claims corresponding to a LTIProfile instance. | ||
Args: | ||
request: HTTP request object | ||
iss (str, optional): LTI issuer claim. Defaults to None. | ||
aud (str, optional): LTI audience claim. Defaults to None. | ||
sub (str, optional): LTI subject claim. Defaults to None. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Models for `openedx_lti_tool_plugin`.""" | ||
from django.contrib.auth import get_user_model | ||
from django.db import models | ||
|
||
|
||
class LtiProfileManager(models.Manager): | ||
"""LTI 1.3 profile model manager.""" | ||
|
||
|
||
class LtiProfile(models.Model): | ||
"""LTI 1.3 profile for Open edX users. | ||
A unique representation of the LTI subject | ||
that initiated an LTI launch. | ||
""" | ||
|
||
objects = LtiProfileManager() | ||
user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE) | ||
|
||
def __str__(self): | ||
"""Get a string representation of this model instance.""" | ||
return f'<Lti1p3Profile, ID: {self.id}>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,5 @@ | |
'NAME': 'db.sqlite3', | ||
}, | ||
} | ||
|
||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Tests for the `openedx_lti_tool_plugin` auth module.""" | ||
from django.test import TestCase | ||
|
||
from openedx_lti_tool_plugin.auth import LtiAuthenticationBackend # pylint: disable=unused-import | ||
|
||
|
||
class TestLtiAuthenticationBackend(TestCase): | ||
"""Test LTI 1.3 profile authentication backend.""" | ||
|
||
def test_authenticate(self): | ||
"""Test authenticate method.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""Tests for the `openedx_lti_tool_plugin` models module.""" | ||
from django.test import TestCase | ||
|
||
from openedx_lti_tool_plugin.models import LtiProfile, LtiProfileManager # pylint: disable=unused-import | ||
|
||
|
||
class TestLtiProfileManager(TestCase): | ||
"""Test LTI profile model manager.""" | ||
|
||
|
||
class TestLtiProfile(TestCase): | ||
"""Test LTI 1.3 profile model.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Tests for the `openedx_lti_tool_plugin` views module.""" | ||
from django.test import TestCase | ||
|
||
from openedx_lti_tool_plugin.views import ( # pylint: disable=unused-import | ||
LtiToolBaseView, | ||
LtiToolLaunchView, | ||
LtiToolLoginView, | ||
) | ||
|
||
|
||
class TestLtiToolBaseView(TestCase): | ||
"""Test base LTI 1.3 view.""" | ||
|
||
|
||
class TestLtiToolLoginView(TestCase): | ||
"""Test LTI 1.3 third-party login view.""" | ||
|
||
def test_get(self): | ||
"""Test GET method.""" | ||
|
||
def test_post(self): | ||
"""Test POST method.""" | ||
|
||
|
||
class TestLtiToolLaunchView(TestCase): | ||
"""Test LTI 1.3 platform tool launch view.""" | ||
|
||
def test_authenticate_and_login(self): | ||
"""Test LTI 1.3 launch user authentication and authorization.""" | ||
|
||
|
||
class TestLtiToolJwksView(TestCase): | ||
"""Test LTI 1.3 JSON Web Key Sets view.""" | ||
|
||
def test_get(self): | ||
"""Test GET method.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
""" | ||
URL configuration for `openedx_lti_tool_plugin`. | ||
"""URL configuration for `openedx_lti_tool_plugin`.""" | ||
from django.urls import path | ||
|
||
For more information on this file, see: | ||
https://docs.djangoproject.com/en/3.2/topics/http/urls/ | ||
""" | ||
from openedx_lti_tool_plugin import views | ||
|
||
urlpatterns = [] | ||
urlpatterns = [ | ||
path('1.3/login/', views.LtiToolLoginView.as_view(), name='lti1p3-login'), | ||
path('1.3/launch/', views.LtiToolLaunchView.as_view(), name='lti1p3-launch'), | ||
path('1.3/pub/jwks/', views.LtiToolJwksView.as_view(), name='lti1p3-pub-jwks'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Views for `openedx_lti_tool_plugin`.""" | ||
from django.contrib.auth import authenticate # pylint: disable=unused-import | ||
from django.views.generic.base import TemplateResponseMixin, View | ||
from pylti1p3.contrib.django import DjangoCacheDataStorage # pylint: disable=unused-import | ||
from pylti1p3.contrib.django import DjangoDbToolConf # pylint: disable=unused-import | ||
from pylti1p3.contrib.django import DjangoMessageLaunch # pylint: disable=unused-import | ||
from pylti1p3.contrib.django import DjangoOIDCLogin # pylint: disable=unused-import | ||
from pylti1p3.exception import LtiException # pylint: disable=unused-import | ||
from pylti1p3.exception import OIDCException # pylint: disable=unused-import | ||
|
||
|
||
class LtiToolBaseView(View): | ||
"""Base LTI view initializing common LTI tool attributes.""" | ||
|
||
def setup(self, request, *args, **kwargs): | ||
"""Initialize attributes shared by all LTI views.""" | ||
|
||
|
||
class LtiToolLoginView(LtiToolBaseView): | ||
""" | ||
LTI 1.3 third-party login view. | ||
The LTI platform will start the OpenID Connect flow by redirecting the User | ||
Agent (UA) to this view. The redirect may be a form POST or a GET. On | ||
success the view should redirect the UA to the LTI platform's authentication | ||
URL. | ||
""" | ||
|
||
def get(self, request): | ||
"""Get request.""" | ||
return self.post(request) | ||
|
||
def post(self, request): | ||
"""Initialize 3rd-party login requests to redirect.""" | ||
|
||
|
||
class LtiToolLaunchView(TemplateResponseMixin, LtiToolBaseView): | ||
"""LTI 1.3 platform tool launch view. | ||
Returns a rendered view of a requested XBlock LTI launch, | ||
unless authentication or authorization fails. | ||
""" | ||
|
||
def _authenticate_and_login(self): | ||
"""Authenticate and authorize the user for this LTI message launch.""" | ||
|
||
|
||
class LtiToolJwksView(LtiToolBaseView): | ||
"""LTI 1.3 JSON Web Key Sets view. | ||
Returns the LTI tool public key. | ||
""" | ||
|
||
def get(self, request): | ||
"""Return the public JWKS.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.