From 2c1d58247598d09592863cabbc9fb8ab12d6dba6 Mon Sep 17 00:00:00 2001 From: Florent Jouatte Date: Wed, 17 Aug 2016 12:13:33 +0200 Subject: [PATCH 1/2] #113: omit claim when empty --- oidc_provider/lib/claims.py | 5 +++- oidc_provider/tests/test_claims.py | 46 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 oidc_provider/tests/test_claims.py diff --git a/oidc_provider/lib/claims.py b/oidc_provider/lib/claims.py index 4330ee3..8c8263a 100644 --- a/oidc_provider/lib/claims.py +++ b/oidc_provider/lib/claims.py @@ -60,8 +60,11 @@ class ScopeClaims(object): if value is None or value == '': del aux_dic[key] elif type(value) is dict: + cleaned_dic = self._clean_dic(value) + if not cleaned_dic: + del aux_dic[key] + continue aux_dic[key] = self._clean_dic(value) - return aux_dic @classmethod diff --git a/oidc_provider/tests/test_claims.py b/oidc_provider/tests/test_claims.py new file mode 100644 index 0000000..92429b3 --- /dev/null +++ b/oidc_provider/tests/test_claims.py @@ -0,0 +1,46 @@ +from django.test import TestCase +from oidc_provider.lib.claims import ScopeClaims +from oidc_provider.tests.app.utils import create_fake_user + + +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) + + def test_clean_dic(self): + """ assert that _clean_dic function returns a clean dictionnary + (no empty claims) """ + dict_to_clean = { + 'phone_number_verified': '', + 'middle_name': '', + 'name': 'John Doe', + 'website': '', + 'profile': '', + 'family_name': 'Doe', + 'birthdate': '', + 'preferred_username': '', + 'picture': '', + 'zoneinfo': '', + 'locale': '', + 'gender': '', + 'updated_at': '', + 'address': {}, + 'given_name': 'John', + 'email_verified': '', + 'nickname': '', + 'email': u'johndoe@example.com', + 'phone_number': '', + } + clean_dict = self.scopeClaims._clean_dic(dict_to_clean) + self.assertEquals( + clean_dict, + { + 'family_name': 'Doe', + 'given_name': 'John', + 'name': 'John Doe', + 'email': u'johndoe@example.com' + } + ) From 2872d2e10b880d5f37788ecf0c5d54d1cf721cbf Mon Sep 17 00:00:00 2001 From: Florent Jouatte Date: Wed, 17 Aug 2016 12:24:00 +0200 Subject: [PATCH 2/2] #113: tiny improvement --- oidc_provider/lib/claims.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/oidc_provider/lib/claims.py b/oidc_provider/lib/claims.py index 8c8263a..16e3919 100644 --- a/oidc_provider/lib/claims.py +++ b/oidc_provider/lib/claims.py @@ -60,11 +60,11 @@ class ScopeClaims(object): if value is None or value == '': del aux_dic[key] elif type(value) is dict: - cleaned_dic = self._clean_dic(value) - if not cleaned_dic: + cleaned_dict = self._clean_dic(value) + if not cleaned_dict: del aux_dic[key] continue - aux_dic[key] = self._clean_dic(value) + aux_dic[key] = cleaned_dict return aux_dic @classmethod