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

Changes to Django 3.0 and python 3.7 #4

Open
wants to merge 4 commits into
base: score
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
8 changes: 8 additions & 0 deletions oidc_auth/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.apps import AppConfig


class OidcAuthConfig(AppConfig):
name = 'oidc_auth'
6 changes: 3 additions & 3 deletions oidc_auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def authenticate(self, **kwargs):
id_token = provider.verify_id_token(credentials['id_token'])

oidc_user = OpenIDUser.get_or_create(id_token,
credentials['access_token'],
credentials.get('refresh_token', ''),
provider)
credentials['access_token'],
credentials.get('refresh_token', ''),
provider)

return oidc_user.user
except Exception as e:
Expand Down
7 changes: 5 additions & 2 deletions oidc_auth/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ class Migration(migrations.Migration):
('sub', models.CharField(unique=True, max_length=255)),
('access_token', models.CharField(max_length=255)),
('refresh_token', models.CharField(max_length=255)),
('issuer', models.ForeignKey(to='oidc_auth.OpenIDProvider')),
('user', models.OneToOneField(related_name='oidc_account', to=settings.AUTH_USER_MODEL)),
('issuer', models.ForeignKey(to='oidc_auth.OpenIDProvider',
on_delete=models.PROTECT)),
('user', models.OneToOneField(related_name='oidc_account',
on_delete=models.PROTECT,
to=settings.AUTH_USER_MODEL)),
],
),
]
18 changes: 12 additions & 6 deletions oidc_auth/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import string
import random
import json
from urlparse import urljoin
from urllib.parse import urljoin
import requests
from django.db import models, IntegrityError
from django.conf import settings
Expand Down Expand Up @@ -31,7 +31,7 @@ def generate(cls, redirect_url, issuer, length=oidc_settings.NONCE_LENGTH):
"""This method generates and returns a nonce, an unique generated
string. If the maximum of retries is exceeded, it returns None.
"""
CHARS = string.letters + string.digits
CHARS = string.ascii_letters + string.digits

for i in range(5):
_hash = ''.join(random.choice(CHARS) for n in range(length))
Expand Down Expand Up @@ -147,7 +147,7 @@ def verify_id_token(self, token):

id_token = JWS().verify_compact(token, self.signing_keys)
log.debug('Token verified, %s' % id_token)
return json.loads(id_token)
return id_token

@staticmethod
def _get_issuer(token):
Expand Down Expand Up @@ -196,9 +196,15 @@ def get_default_provider():

class OpenIDUser(models.Model):
sub = models.CharField(max_length=255, unique=True)
issuer = models.ForeignKey(OpenIDProvider)
user = models.OneToOneField(getattr(settings, 'AUTH_USER_MODEL', 'auth.User'),
related_name='oidc_account')
issuer = models.ForeignKey(
OpenIDProvider,
on_delete=models.PROTECT
)
user = models.OneToOneField(
getattr(settings, 'AUTH_USER_MODEL', 'auth.User'),
related_name='oidc_account',
on_delete=models.PROTECT
)

access_token = models.CharField(max_length=255)
refresh_token = models.CharField(max_length=255)
Expand Down
2 changes: 1 addition & 1 deletion oidc_auth/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf.urls import url

import views
from oidc_auth import views

urlpatterns = [
url(r'^login/$', views.login_begin, name='oidc-login'),
Expand Down
11 changes: 6 additions & 5 deletions oidc_auth/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from urllib import urlencode
from urllib.parse import urlencode
from django.conf import settings
from django.http import HttpResponseBadRequest
from django.contrib.auth import REDIRECT_FIELD_NAME, authenticate, login as django_login
from django.core.urlresolvers import reverse
from django.contrib.auth import REDIRECT_FIELD_NAME, login as django_login
from django.urls import reverse
from django.shortcuts import render, redirect
import requests

from . import errors
from . import utils
from .auth import OpenIDConnectBackend
from .utils import log
from .settings import oidc_settings
from .forms import OpenIDConnectForm
Expand Down Expand Up @@ -91,8 +92,8 @@ def login_complete(request, login_complete_view='oidc-complete',
log.debug('Token exchange done, proceeding authentication')
credentials = response.json()
credentials['provider'] = provider
user = authenticate(credentials=credentials)
django_login(request, user)
user = OpenIDConnectBackend().authenticate(credentials=credentials)
django_login(request, user, backend='oidc_auth.auth.OpenIDConnectBackend')

return redirect(nonce.redirect_url)

Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Production requirements
Django>1.9
pyjwkest==0.6.2
requests==2.2.1
Django>=3.0,<4.2
pyjwkest==1.1.5
requests==2.22.0
South==1.0.2

# Test requirements
Expand Down
13 changes: 7 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

setup(
name='django-oidc-auth',
version='0.0.11',
version='1.0.0',
description='OpenID Connect client for Django applications',
long_description='WIP',
author='Lucas S. Magalhães',
author_email='[email protected]',
author='Lucas S. Magalhães, Daniel Pimentel, João Paulo de Araújo, Romero Malaquias',
author_email='[email protected], [email protected], [email protected], '
'[email protected]',
packages=find_packages(exclude=['*.tests']),
include_package_data=True,
install_requires=[
'Django>=1.5',
'Django>=3.0,<4.2',
'South==1.0.2',
'pyjwkest==0.6.2',
'requests',
'pyjwkest==1.1.5',
'requests==2.22.0',
],
zip_safe=True
)