-
Notifications
You must be signed in to change notification settings - Fork 19
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
TLT-4135 (feat) add setting to exclude views #49
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Quality Checks | ||
on: [push] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
django-version: ['3.2', '3.1', '3.0', '2.2', '2.1', '2.0'] | ||
python-version: ['3.7', '3.8', '3.9'] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
cache: 'pip' | ||
cache-dependency-path: 'setup.py' | ||
- name: Install Python Dependencies | ||
run: | | ||
pip install --upgrade pip wheel | ||
pip install Django~=${{ matrix.django-version }} | ||
python setup.py install | ||
- name: Run tests | ||
run: python run_tests.py |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,88 @@ | ||
|
||
# django-auth-lti | ||
|
||
django_auth_lti is a package that provides Django authentication middleware and backend classes for building tools that work with an LTI consumer. | ||
django_auth_lti is a package that provides Django authentication middleware and backend classes for building tools that work with an LTI consumer. | ||
|
||
To use LTI authentication with a Django app, edit settings.py as follows: | ||
|
||
* add 'django_auth_lti.middleware_patched.MultiLTILaunchAuthMiddleware' to your MIDDLEWARE (Django >= 1.10) or MIDDLEWARE_CLASSES (Django < 1.10), making sure that it appears AFTER 'django.contrib.auth.middleware.AuthenticationMiddleware' | ||
* add `django_auth_lti.middleware_patched.MultiLTILaunchAuthMiddleware` to your MIDDLEWARE (Django >= 1.10), making sure that it appears AFTER `django.contrib.auth.middleware.AuthenticationMiddleware` | ||
|
||
* add 'django_auth_lti.backends.LTIAuthBackend' to your BACKEND_CLASSES | ||
* add 'django_auth_lti.backends.LTIAuthBackend' to your `BACKEND_CLASSES` | ||
|
||
* configure the OAuth credentials - add something like this to your project configuration: | ||
``` | ||
```python | ||
LTI_OAUTH_CREDENTIALS = { | ||
'test': 'secret', | ||
'test2': 'reallysecret' | ||
} | ||
``` | ||
|
||
* OPTIONALLY, you can define a custom role key at the project level. Doing so will cause the middleware to look for any roles associated with that key during the launch request and merge them into the default LTI roles list. You can declare such a key by adding this to your project configuration: | ||
``` | ||
```python | ||
LTI_CUSTOM_ROLE_KEY = 'my-custom-role-key-change-me' | ||
``` | ||
|
||
The MultiLTILaunchAuthMiddleware will ensure that all users of your app are authenticated before they can access any page. Upon successful authentication, a Django user record is created (or updated) and the user is allowed to access the application. The middleware will also make the LTI launch parameters available to any request via a 'LTI' parameter on the request object. | ||
``` | ||
The `MultiLTILaunchAuthMiddleware` will ensure that all users of your app are authenticated before they can access any page. Upon successful authentication, a Django user record is created (or updated) and the user is allowed to access the application. The middleware will also make the LTI launch parameters available to any request via a 'LTI' parameter on the request object. | ||
```python | ||
request.LTI.get('resource_link_id') | ||
``` | ||
|
||
# Excluding paths | ||
|
||
The middleware and reverse monkeypatch will skip checks for the `LTI` parameter if: | ||
|
||
* `request.path` is blank (i.e. the empty string `""`) | ||
* `request.path` exactly matches one of the paths in the `EXCLUDE_PATHS` setting. | ||
|
||
To provide custom paths to exclude (e.g. `/w/ping/` for watchman, or `/app/tool_config/`), add the following in your django project condfiguration: | ||
|
||
```python | ||
DJANGO_AUTH_LTI_EXCLUDE_PATHS = [ | ||
'/lti_tool/tool_config/', | ||
'/w/ping/', | ||
] | ||
``` | ||
|
||
# Local development | ||
|
||
Bootstrapping a local Python development environment on your host machine for testing (`USE_PYTHON_VERSION` can correspond to the Python version under test): | ||
|
||
```sh | ||
USE_PYTHON_VERSION="3.9.10" | ||
VENV_DIR=".venv" | ||
pyenv install --skip-existing ${USE_PYTHON_VERSION} | ||
rm -Rf "${VENV_DIR}" && PYENV_VERSION=${USE_PYTHON_VERSION} python -m venv "${VENV_DIR}" | ||
. "${VENV_DIR}"/bin/activate && pip install --upgrade pip wheel | ||
. "${VENV_DIR}"/bin/activate && python setup.py install | ||
``` | ||
|
||
To test against a specific version of Django, you can `pip install` it before running `python setup.py install`. | ||
|
||
# Testing | ||
|
||
To run tests in a single environment: | ||
|
||
```sh | ||
python run_tests.py | ||
``` | ||
|
||
To run a test matrix across Python and Django versions: | ||
|
||
```sh | ||
pip install tox | ||
|
||
# You'll need to have all the Python versions specified in tox installed. | ||
# For pyenv, you can use e.g. | ||
# | ||
# pyenv install --skip-existing 3.7.x | ||
# pyenv install --skip-existing 3.8.x | ||
# pyenv install --skip-existing 3.9.x | ||
# pyenv install --skip-existing 3.10.x | ||
# | ||
# ... where x is the specific version available to you in pyenv. | ||
# Then make them available to tox like so: | ||
# | ||
# pyenv local 3.7.x 3.8.x 3.9.x 3.10.x | ||
|
||
tox # --parallel (optional) | ||
``` |
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,9 @@ | ||
from django.conf import settings | ||
|
||
|
||
def get_excluded_paths(): | ||
excluded = getattr(settings, "DJANGO_AUTH_LTI_EXCLUDE_PATHS", []) | ||
excluded.append("") | ||
# add a blank path to the list by default, as `path` can sometimes not | ||
# exist, e.g. when rendering django-debug-toolbar templates | ||
return excluded |
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,19 +1,17 @@ | ||
import logging | ||
|
||
import django_auth_lti.patch_reverse | ||
|
||
from django.contrib import auth | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.conf import settings | ||
from django.utils.deprecation import MiddlewareMixin | ||
|
||
from .conf import get_excluded_paths | ||
from .timer import Timer | ||
|
||
from .thread_local import set_current_request | ||
|
||
try: | ||
from django.utils.deprecation import MiddlewareMixin | ||
except ImportError: # Django < 1.10 | ||
MiddlewareMixin = object | ||
# importing here will ensure that django.urls.reverse is patched | ||
# for other libraries and parts of django, e.g. `url` template tag, | ||
# when the middleware is loaded | ||
import django_auth_lti.patch_reverse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for highlighting this. I wouldn't have given a second thought to the change in import order had this not been there. |
||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -41,6 +39,12 @@ class MultiLTILaunchAuthMiddleware(MiddlewareMixin): | |
def process_request(self, request): | ||
logger.debug('inside process_request %s' % request.path) | ||
|
||
# note that `path` can sometimes not exist, e.g. when rendering | ||
# django-debug-toolbar templates | ||
if getattr(request, "path", "") in get_excluded_paths(): | ||
set_current_request(request) | ||
return | ||
|
||
# AuthenticationMiddleware is required so that request.user exists. | ||
if not hasattr(request, 'user'): | ||
logger.debug('improperly configured: request has no user attr') | ||
|
This file was deleted.
Oops, something went wrong.
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
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,55 @@ | ||
from unittest import TestCase | ||
from unittest.mock import patch | ||
|
||
from django.test import override_settings, RequestFactory | ||
from django.urls import reverse | ||
|
||
from . import helpers | ||
from django_auth_lti.middleware_patched import MultiLTILaunchAuthMiddleware | ||
|
||
class TestReverse(TestCase): | ||
def setUp(self): | ||
self.mw = MultiLTILaunchAuthMiddleware() | ||
|
||
def build_lti_launch_request(self, post_data, url): | ||
return helpers.build_lti_launch_request(post_data, url) | ||
|
||
@patch('django_auth_lti.middleware.logger') | ||
@patch('django_auth_lti.middleware_patched.auth') | ||
def test_patched_reverse_adds_resource_link_id(self, mock_auth, mock_logger): | ||
""" | ||
`django.urls.reverse()` should add the `resource_link_id` from the LTI session to the URL | ||
(`django.urls.reverse` should be patched automatically by importing MultiLTILaunchAuthMiddleware) | ||
""" | ||
mock_auth.authenticate.return_value = True | ||
request = self.build_lti_launch_request({"resource_link_id": 'abc123'}, url='/lti_launch/') | ||
self.mw.process_request(request) | ||
url = reverse('lti_launch') | ||
self.assertEqual(url, '/lti_launch/?resource_link_id=abc123') | ||
|
||
@override_settings(DJANGO_AUTH_LTI_EXCLUDE_PATHS=['/skip_lti/']) | ||
@patch('django_auth_lti.middleware.logger') | ||
@patch('django_auth_lti.middleware_patched.auth') | ||
def test_patched_reverse_exclude_paths_settings(self, mock_auth, mock_logger): | ||
""" | ||
`django.urls.reverse()` should not check the LTI session for the `resource_link_id` if the `request.path` is excluded by project settings | ||
(`django.urls.reverse` should be patched automatically by importing MultiLTILaunchAuthMiddleware) | ||
""" | ||
mock_auth.authenticate.return_value = True | ||
request = RequestFactory().get('/skip_lti/') | ||
self.mw.process_request(request) | ||
url = reverse('lti_launch') | ||
self.assertEqual(url, '/lti_launch/') | ||
|
||
@patch('django_auth_lti.middleware.logger') | ||
@patch('django_auth_lti.middleware_patched.auth') | ||
def test_patched_reverse_exclude_resource_link_id_param(self, mock_auth, mock_logger): | ||
""" | ||
`django.urls.reverse()` should not check the LTI session for the `resource_link_id` if the `exclude_resource_link_id` is set to `True` when called | ||
(`django.urls.reverse` should be patched automatically by importing MultiLTILaunchAuthMiddleware) | ||
""" | ||
mock_auth.authenticate.return_value = True | ||
request = self.build_lti_launch_request({"resource_link_id": 'abc123'}, url='/lti_launch/') | ||
self.mw.process_request(request) | ||
url = reverse('lti_launch', exclude_resource_link_id=True) | ||
self.assertEqual(url, '/lti_launch/') |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious: should this be 2022?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, good point. TBH I'm not sure when, exactly, we need to update the copyright, and some libraries/resources I've seen don't even include a date/year.
Seems we've had discussions of this in the past and don't always bump it -- there's some set of criteria that are used, that I'm not aware of/can't remember. Maybe @cmurtaugh has some insight?
I'll update to 2022 for now.