Merge pull request #129 from torreco/v0.4.x

Make Client available when using OIDC_EXTRA_SCOPE_CLAIMS
This commit is contained in:
Juan Ignacio Fiorentino 2016-10-13 11:15:38 -03:00 committed by GitHub
commit bb91982a8e
6 changed files with 32 additions and 8 deletions

View file

@ -10,6 +10,12 @@ All notable changes to this project will be documented in this file.
##### Fixed ##### Fixed
- Bug when generating secret_key value using admin. - Bug when generating secret_key value using admin.
##### Changed
- Client is available to OIDC_EXTRA_SCOPE_CLAIMS implementations via `self.client`.
##### Incompatible changes
- The constructor signature for `ScopeClaims` has changed, it now is called with the `Token` as its single argument.
### [0.4.1] - 2016-10-03 ### [0.4.1] - 2016-10-03
##### Changed ##### Changed

View file

@ -96,6 +96,7 @@ Inside your oidc_provider_settings.py file add the following class::
# self.user - Django user instance. # self.user - Django user instance.
# self.userinfo - Dict returned by OIDC_USERINFO function. # self.userinfo - Dict returned by OIDC_USERINFO function.
# self.scopes - List of scopes requested. # self.scopes - List of scopes requested.
# self.client - Client requesting this claims.
dic = { dic = {
'bar': 'Something dynamic here', 'bar': 'Something dynamic here',
} }

View file

@ -14,10 +14,11 @@ STANDARD_CLAIMS = {
class ScopeClaims(object): class ScopeClaims(object):
def __init__(self, user, scopes): def __init__(self, token):
self.user = user self.user = token.user
self.userinfo = settings.get('OIDC_USERINFO', import_str=True)(STANDARD_CLAIMS, self.user) self.userinfo = settings.get('OIDC_USERINFO', import_str=True)(STANDARD_CLAIMS, self.user)
self.scopes = scopes self.scopes = token.scope
self.client = token.client
def create_response_dic(self): def create_response_dic(self):
""" """

View file

@ -1,16 +1,19 @@
import random import random
import string import string
try: try:
from urlparse import parse_qs, urlsplit from urlparse import parse_qs, urlsplit
except ImportError: except ImportError:
from urllib.parse import parse_qs, urlsplit from urllib.parse import parse_qs, urlsplit
from django.utils import timezone
from django.contrib.auth.models import User from django.contrib.auth.models import User
from oidc_provider.models import ( from oidc_provider.models import (
Client, Client,
Code, Code,
) Token)
FAKE_NONCE = 'cb584e44c43ed6bd0bc2d9c7e242837d' FAKE_NONCE = 'cb584e44c43ed6bd0bc2d9c7e242837d'
@ -58,6 +61,16 @@ def create_fake_client(response_type, is_public=False):
return client return client
def create_fake_token(user, scopes, client):
expires_at = timezone.now() + timezone.timedelta(seconds=60)
token = Token(user=user, client=client, expires_at=expires_at)
token.scope = scopes
token.save()
return token
def is_code_valid(url, user, client): def is_code_valid(url, user, client):
""" """
Check if the code inside the url is valid. Supporting both query string and fragment. Check if the code inside the url is valid. Supporting both query string and fragment.

View file

@ -1,6 +1,7 @@
from django.test import TestCase from django.test import TestCase
from oidc_provider.lib.claims import ScopeClaims from oidc_provider.lib.claims import ScopeClaims
from oidc_provider.tests.app.utils import create_fake_user from oidc_provider.tests.app.utils import create_fake_user, create_fake_client, create_fake_token
class ClaimsTestCase(TestCase): class ClaimsTestCase(TestCase):
@ -8,7 +9,9 @@ class ClaimsTestCase(TestCase):
def setUp(self): def setUp(self):
self.user = create_fake_user() self.user = create_fake_user()
self.scopes = ['openid', 'address', 'email', 'phone', 'profile'] self.scopes = ['openid', 'address', 'email', 'phone', 'profile']
self.scopeClaims = ScopeClaims(self.user, self.scopes) self.client = create_fake_client('code')
self.token = create_fake_token(self.user, self.scopes, self.client)
self.scopeClaims = ScopeClaims(self.token)
def test_clean_dic(self): def test_clean_dic(self):
""" assert that _clean_dic function returns a clean dictionnary """ assert that _clean_dic function returns a clean dictionnary

View file

@ -162,11 +162,11 @@ def userinfo(request, *args, **kwargs):
'sub': token.id_token.get('sub'), 'sub': token.id_token.get('sub'),
} }
standard_claims = StandardScopeClaims(token.user, token.scope) standard_claims = StandardScopeClaims(token)
dic.update(standard_claims.create_response_dic()) dic.update(standard_claims.create_response_dic())
if settings.get('OIDC_EXTRA_SCOPE_CLAIMS'): if settings.get('OIDC_EXTRA_SCOPE_CLAIMS'):
extra_claims = settings.get('OIDC_EXTRA_SCOPE_CLAIMS', import_str=True)(token.user, token.scope) extra_claims = settings.get('OIDC_EXTRA_SCOPE_CLAIMS', import_str=True)(token)
dic.update(extra_claims.create_response_dic()) dic.update(extra_claims.create_response_dic())
response = JsonResponse(dic, status=200) response = JsonResponse(dic, status=200)