Skip to content

Commit

Permalink
Merge pull request #129 from torreco/v0.4.x
Browse files Browse the repository at this point in the history
Make Client available when using OIDC_EXTRA_SCOPE_CLAIMS
  • Loading branch information
juanifioren committed Oct 13, 2016
2 parents 7405d79 + 99d7194 commit bb91982
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ All notable changes to this project will be documented in this file.
##### Fixed
- 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

##### Changed
Expand Down
1 change: 1 addition & 0 deletions docs/sections/scopesclaims.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Inside your oidc_provider_settings.py file add the following class::
# self.user - Django user instance.
# self.userinfo - Dict returned by OIDC_USERINFO function.
# self.scopes - List of scopes requested.
# self.client - Client requesting this claims.
dic = {
'bar': 'Something dynamic here',
}
Expand Down
7 changes: 4 additions & 3 deletions oidc_provider/lib/claims.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

class ScopeClaims(object):

def __init__(self, user, scopes):
self.user = user
def __init__(self, token):
self.user = token.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):
"""
Expand Down
15 changes: 14 additions & 1 deletion oidc_provider/tests/app/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import random
import string


try:
from urlparse import parse_qs, urlsplit
except ImportError:
from urllib.parse import parse_qs, urlsplit

from django.utils import timezone
from django.contrib.auth.models import User

from oidc_provider.models import (
Client,
Code,
)
Token)


FAKE_NONCE = 'cb584e44c43ed6bd0bc2d9c7e242837d'
Expand Down Expand Up @@ -58,6 +61,16 @@ def create_fake_client(response_type, is_public=False):
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):
"""
Check if the code inside the url is valid. Supporting both query string and fragment.
Expand Down
7 changes: 5 additions & 2 deletions oidc_provider/tests/test_claims.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from django.test import TestCase

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):

def setUp(self):
self.user = create_fake_user()
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):
""" assert that _clean_dic function returns a clean dictionnary
Expand Down
4 changes: 2 additions & 2 deletions oidc_provider/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ def userinfo(request, *args, **kwargs):
'sub': token.id_token.get('sub'),
}

standard_claims = StandardScopeClaims(token.user, token.scope)
standard_claims = StandardScopeClaims(token)
dic.update(standard_claims.create_response_dic())

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())

response = JsonResponse(dic, status=200)
Expand Down

0 comments on commit bb91982

Please sign in to comment.