Skip to content

Commit

Permalink
Merge pull request #336 from johngian/pr-322
Browse files Browse the repository at this point in the history
Django compat
  • Loading branch information
John Giannelos committed Jan 2, 2020
2 parents 8cb98ad + d3dd793 commit 63de5b0
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 147 deletions.
48 changes: 35 additions & 13 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ common_steps: &common_steps
command: echo 127.0.0.1 testprovider | tee -a /etc/hosts
- run:
name: Install latest lib build
command: pip install /tmp/workspace/mozilla-django-oidc-dev.tar.gz
command: . /testrp_env/bin/activate && pip install /tmp/workspace/mozilla-django-oidc-dev.tar.gz
- run:
name: Override django version
command: pip install $DJANGO_VERSION
command: . /testrp_env/bin/activate && pip install $DJANGO_VERSION
- run:
name: Run testprovider service
command: ./bin/run.sh
command: . /testprovider_env/bin/activate && ./bin/run.sh
background: True
working_directory: /testprovider
- run:
name: Run testrp service
command: ./bin/run.sh
command: . /testrp_env/bin/activate && ./bin/run.sh
background: True
working_directory: /testrp
- run:
Expand All @@ -35,7 +35,7 @@ common_steps: &common_steps
command: wait-for-it -p 8081 -h localhost -t 60
- run:
name: Run integration tests
command: python integration_tests.py
command: . /testrp_env/bin/activate && python integration_tests.py
working_directory: ~/project/integration_tests

jobs:
Expand All @@ -53,68 +53,84 @@ jobs:
- mozilla-django-oidc-dev.tar.gz
e2e_test_py2_rs_django111:
docker:
- image: mozillaparsys/oidc_e2e_setup:py2
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py2-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=rs
- DJANGO_VERSION=Django>=1.11,<2.0.0
<<: *common_steps
e2e_test_py3_rs_django111:
docker:
- image: mozillaparsys/oidc_e2e_setup:py3
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py3-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=rs
- DJANGO_VERSION=Django>=1.11,<2.0.0
<<: *common_steps
e2e_test_py3_rs_django200:
docker:
- image: mozillaparsys/oidc_e2e_setup:py3
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py3-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=rs
- DJANGO_VERSION=Django>=2.0.0,<2.1
<<: *common_steps
e2e_test_py3_rs_django210:
docker:
- image: mozillaparsys/oidc_e2e_setup:py3
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py3-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=rs
- DJANGO_VERSION=Django>=2.1.0,<2.2
<<: *common_steps
e2e_test_py2_hs_django111:
docker:
- image: mozillaparsys/oidc_e2e_setup:py2
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py2-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=hs
- DJANGO_VERSION=Django>=1.11,<2.0.0
<<: *common_steps
e2e_test_py3_hs_django111:
docker:
- image: mozillaparsys/oidc_e2e_setup:py3
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py3-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=hs
- DJANGO_VERSION=Django>=1.11,<2.0.0
<<: *common_steps
e2e_test_py3_hs_django200:
docker:
- image: mozillaparsys/oidc_e2e_setup:py3
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py3-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=hs
- DJANGO_VERSION=Django>=2.0.0,<2.1
<<: *common_steps
e2e_test_py3_hs_django210:
docker:
- image: mozillaparsys/oidc_e2e_setup:py3
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py3-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=hs
- DJANGO_VERSION=Django>=2.1.0,<2.2
<<: *common_steps
e2e_test_py3_hs_django220:
docker:
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py3-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=hs
- DJANGO_VERSION=Django>=2.2.0,<3.0
<<: *common_steps
e2e_test_py3_hs_django300:
docker:
- image: mozilla/oidc-testprovider:oidc_e2e_setup_py3-latest
name: testoidcsetup
environment:
- TEST_OIDC_ALGO=hs
- DJANGO_VERSION=Django>=3.0.0,<3.1
<<: *common_steps

workflows:
version: 2
Expand Down Expand Up @@ -145,3 +161,9 @@ workflows:
- e2e_test_py3_hs_django210:
requires:
- build_lib
- e2e_test_py3_hs_django220:
requires:
- build_lib
- e2e_test_py3_hs_django300:
requires:
- build_lib
9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ python:
- "3.4"
- "3.5"
- "3.6"

# Workaround for python 3.7
matrix:
include:
- python: "3.7"
dist: xenial
sudo: true
- "3.7"
- "3.8"

install:
- pip install codecov tox-travis
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Before you submit a pull request, check that it meets these guidelines:
2. If the pull request adds functionality, the docs should be updated. Put
your new functionality into a function with a docstring, and add the
feature to the list in README.rst.
3. The pull request should work for Python 2.6, 2.7, and 3.3, and for PyPy. Check
3. The pull request should work for Python 2.7, and 3.4+, and for PyPy. Check
`<https://travis-ci.org/mozilla/mozilla-django-oidc/pull_requests>`_
and make sure that the tests pass for all supported Python versions.

Expand All @@ -120,7 +120,7 @@ We use tox to run tests::

To run a specific environment, use the ``-e`` argument::

$ tox -e py27-django18
$ tox -e py27-django111


You can also run the tests in a virtual environment without tox::
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ services:
testprovider:
stdin_open: true
tty: true
image: mozillaparsys/oidc_testprovider
image: mozilla/oidc-testprovider:oidc_testprovider-latest
ports:
- "8080:8080"
testrp:
stdin_open: true
tty: true
image: mozillaparsys/oidc_testrp:py${PYTHON_VERSION:-3}
image: mozilla/oidc-testprovider:oidc_testrp_py${PYTHON_VERSION:-3}-latest
ports:
- "8081:8081"
volumes:
- ./:/mozilla-django-oidc
environment:
- TEST_OIDC_ALGO=${TEST_OIDC_ALGO:-hs}
- DJANGO_VERSION=${DJANGO_VERSION:-Django>=2.0}
- DJANGO_VERSION=${DJANGO_VERSION:-Django>=2.2}
command: >-
bash -c "cd /mozilla-django-oidc/ &&
pip install -I . &&
Expand All @@ -29,7 +29,7 @@ services:
testrunner:
stdin_open: true
tty: true
image: mozillaparsys/oidc_testrunner
image: mozilla/oidc-testprovider:oidc_testrunner-latest
volumes:
- ./integration_tests:/integration_tests
links:
Expand Down
18 changes: 6 additions & 12 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ documentation for the appropriate values.

You can find more info about `cookie-based sessions`_ in Django's documentation.

.. _cookie-based sessions: https://docs.djangoproject.com/en/1.11/topics/http/sessions/#using-cookie-based-sessions
.. _cookie-based sessions: https://docs.djangoproject.com/en/stable/topics/http/sessions/#using-cookie-based-sessions


These values relate to your site.
Expand Down Expand Up @@ -288,30 +288,24 @@ dotted path to the function you want to use.
The function takes in an email address as a text (Python 2 unicode or Python 3
string) and returns a text (Python 2 unicode or Python 3 string).

Here's an example function for Python 3 and Django 1.11 that doesn't convert
the email address at all:
Here's an example function for Python 3 that doesn't convert the email address
at all:

.. code-block:: python
import unicodedata
def generate_username(email):
# Using Python 3 and Django 1.11, usernames can contain alphanumeric
# Using Python 3 and Django 1.11+, usernames can contain alphanumeric
# (ascii and unicode), _, @, +, . and - characters. So we normalize
# it and slice at 150 characters.
return unicodedata.normalize('NFKC', email)[:150]
.. seealso::

Django 1.8 username:
https://docs.djangoproject.com/en/1.8/ref/contrib/auth/#django.contrib.auth.models.User.username

Django 1.11 username:
https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#django.contrib.auth.models.User.username

Django 2.0 username:
https://docs.djangoproject.com/en/2.0/ref/contrib/auth/#django.contrib.auth.models.User.username
Django username:
https://docs.djangoproject.com/en/stable/ref/contrib/auth/#django.contrib.auth.models.User.username


Changing how Django users are created
Expand Down
8 changes: 4 additions & 4 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ of ``mozilla-django-oidc``.

.. seealso::

https://docs.djangoproject.com/en/1.11/ref/settings/#login-redirect-url
https://docs.djangoproject.com/en/stable/ref/settings/#login-redirect-url

.. py:attribute:: LOGIN_REDIRECT_URL_FAILURE
Expand All @@ -179,13 +179,13 @@ of ``mozilla-django-oidc``.

.. py:attribute:: LOGOUT_REDIRECT_URL
:default: ``/`` (Django <= 1.9) ``None`` (Django 1.10+)
:default: ``None``

After the logout view has logged the user out, it redirects to this url path.

.. seealso::

https://docs.djangoproject.com/en/1.11/ref/settings/#logout-redirect-url
https://docs.djangoproject.com/en/stable/ref/settings/#logout-redirect-url

.. py:attribute:: OIDC_OP_LOGOUT_URL_METHOD
Expand All @@ -206,7 +206,7 @@ of ``mozilla-django-oidc``.

.. seealso::

https://docs.djangoproject.com/en/2.0/topics/http/urls/#url-namespaces
https://docs.djangoproject.com/en/stable/topics/http/urls/#url-namespaces

.. py:attribute:: OIDC_ALLOW_UNSECURED_JWT
Expand Down
8 changes: 2 additions & 6 deletions mozilla_django_oidc/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
import json
import logging
import requests
import six
from requests.auth import HTTPBasicAuth

from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
try:
from django.urls import reverse
except ImportError:
# Django < 2.0.0
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.utils.encoding import force_bytes, smart_text, smart_bytes
from django.utils.module_loading import import_string
from django.utils import six

from josepy.b64 import b64decode
from josepy.jwk import JWK
Expand Down
14 changes: 3 additions & 11 deletions mozilla_django_oidc/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
# Python < 3
from urllib import urlencode

try:
from django.urls import reverse
except ImportError:
# Django < 2.0.0
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.contrib.auth import BACKEND_SESSION_KEY
from django.http import HttpResponseRedirect, JsonResponse
from django.utils.crypto import get_random_string
Expand All @@ -19,11 +15,7 @@
from django.utils.module_loading import import_string

from mozilla_django_oidc.auth import OIDCAuthenticationBackend
from mozilla_django_oidc.utils import (
absolutify,
import_from_settings,
is_authenticated
)
from mozilla_django_oidc.utils import absolutify, import_from_settings


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -81,7 +73,7 @@ def is_refreshable_url(self, request):

return (
request.method == 'GET' and
is_authenticated(request.user) and
request.user.is_authenticated and
is_oidc_enabled and
request.path not in self.exempt_urls
)
Expand Down
13 changes: 5 additions & 8 deletions mozilla_django_oidc/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import warnings

try:
from urllib.request import parse_http_list, parse_keqv_list
except ImportError:
# python < 3
from urllib2 import parse_http_list, parse_keqv_list

from django import VERSION
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

Expand Down Expand Up @@ -38,19 +39,15 @@ def absolutify(request, path):
return request.build_absolute_uri(path)


# Computed once, reused in every request
_less_than_django_1_10 = VERSION < (1, 10)


def is_authenticated(user):
"""return True if the user is authenticated.
This is necessary because in Django 1.10 the `user.is_authenticated`
stopped being a method and is now a property.
Actually `user.is_authenticated()` actually works, thanks to a backwards
compat trick in Django. But in Django 2.0 it will cease to work
as a callable method.
"""
if _less_than_django_1_10:
return user.is_authenticated()

msg = '`is_authenticated()` is going to be removed in mozilla-django-oidc v 2.x'
warnings.warn(msg, DeprecationWarning)
return user.is_authenticated

0 comments on commit 63de5b0

Please sign in to comment.