2015-01-30 20:20:36 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2015-08-11 18:58:52 +00:00
|
|
|
|
|
|
|
from oidc_provider import settings
|
2015-01-30 20:20:36 +00:00
|
|
|
|
|
|
|
|
2016-05-30 16:28:07 +00:00
|
|
|
class ScopeClaims(object):
|
2015-01-30 20:20:36 +00:00
|
|
|
|
|
|
|
def __init__(self, user, scopes):
|
|
|
|
self.user = user
|
2016-05-30 16:28:07 +00:00
|
|
|
self.userinfo = settings.get('OIDC_USERINFO', import_str=True).get_by_user(self.user)
|
2015-01-30 20:20:36 +00:00
|
|
|
self.scopes = scopes
|
|
|
|
|
|
|
|
def create_response_dic(self):
|
|
|
|
"""
|
|
|
|
Generate the dic that will be jsonify. Checking scopes given vs
|
|
|
|
registered.
|
|
|
|
|
|
|
|
Returns a dic.
|
|
|
|
"""
|
|
|
|
dic = {}
|
|
|
|
|
|
|
|
for scope in self.scopes:
|
|
|
|
if scope in self._scopes_registered():
|
2016-05-30 16:28:07 +00:00
|
|
|
dic.update(getattr(self, 'scope_' + scope)())
|
2015-01-30 20:20:36 +00:00
|
|
|
|
|
|
|
dic = self._clean_dic(dic)
|
|
|
|
|
|
|
|
return dic
|
|
|
|
|
|
|
|
def _scopes_registered(self):
|
|
|
|
"""
|
|
|
|
Return a list that contains all the scopes registered
|
|
|
|
in the class.
|
|
|
|
"""
|
|
|
|
scopes = []
|
|
|
|
|
|
|
|
for name in self.__class__.__dict__:
|
|
|
|
|
|
|
|
if name.startswith('scope_'):
|
|
|
|
scope = name.split('scope_')[1]
|
|
|
|
scopes.append(scope)
|
|
|
|
|
|
|
|
return scopes
|
|
|
|
|
|
|
|
def _clean_dic(self, dic):
|
|
|
|
"""
|
|
|
|
Clean recursively all empty or None values inside a dict.
|
|
|
|
"""
|
|
|
|
aux_dic = dic.copy()
|
2015-07-01 19:43:35 +00:00
|
|
|
for key, value in iter(dic.items()):
|
2015-01-30 20:20:36 +00:00
|
|
|
|
2015-07-23 13:03:01 +00:00
|
|
|
if value is None or value == '':
|
2015-01-30 20:20:36 +00:00
|
|
|
del aux_dic[key]
|
|
|
|
elif type(value) is dict:
|
2015-03-31 18:31:17 +00:00
|
|
|
aux_dic[key] = self._clean_dic(value)
|
2015-01-30 20:20:36 +00:00
|
|
|
|
|
|
|
return aux_dic
|
|
|
|
|
2015-08-11 18:58:52 +00:00
|
|
|
|
2016-05-30 16:28:07 +00:00
|
|
|
class StandardScopeClaims(ScopeClaims):
|
2015-01-30 20:20:36 +00:00
|
|
|
"""
|
|
|
|
Based on OpenID Standard Claims.
|
|
|
|
See: http://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
|
|
|
|
"""
|
2016-05-30 16:28:07 +00:00
|
|
|
|
|
|
|
def scope_profile(self):
|
2015-01-30 20:20:36 +00:00
|
|
|
dic = {
|
2015-08-11 18:58:52 +00:00
|
|
|
'name': getattr(self.userinfo, 'name', None),
|
|
|
|
'given_name': getattr(self.userinfo, 'given_name', None),
|
|
|
|
'family_name': getattr(self.userinfo, 'family_name', None),
|
|
|
|
'middle_name': getattr(self.userinfo, 'middle_name', None),
|
|
|
|
'nickname': getattr(self.userinfo, 'nickname', None),
|
|
|
|
'preferred_username': getattr(self.userinfo, 'preferred_username', None),
|
|
|
|
'profile': getattr(self.userinfo, 'profile', None),
|
|
|
|
'picture': getattr(self.userinfo, 'picture', None),
|
|
|
|
'website': getattr(self.userinfo, 'website', None),
|
|
|
|
'gender': getattr(self.userinfo, 'gender', None),
|
|
|
|
'birthdate': getattr(self.userinfo, 'birthdate', None),
|
|
|
|
'zoneinfo': getattr(self.userinfo, 'zoneinfo', None),
|
|
|
|
'locale': getattr(self.userinfo, 'locale', None),
|
|
|
|
'updated_at': getattr(self.userinfo, 'updated_at', None),
|
2015-01-30 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dic
|
|
|
|
|
2016-05-30 16:28:07 +00:00
|
|
|
def scope_email(self):
|
2015-01-30 20:20:36 +00:00
|
|
|
dic = {
|
2015-08-11 18:58:52 +00:00
|
|
|
'email': getattr(self.user, 'email', None),
|
|
|
|
'email_verified': getattr(self.userinfo, 'email_verified', None),
|
2015-01-30 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dic
|
|
|
|
|
2016-05-30 16:28:07 +00:00
|
|
|
def scope_phone(self):
|
2015-01-30 20:20:36 +00:00
|
|
|
dic = {
|
2015-08-11 18:58:52 +00:00
|
|
|
'phone_number': getattr(self.userinfo, 'phone_number', None),
|
|
|
|
'phone_number_verified': getattr(self.userinfo, 'phone_number_verified', None),
|
2015-01-30 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dic
|
|
|
|
|
2016-05-30 16:28:07 +00:00
|
|
|
def scope_address(self):
|
2015-01-30 20:20:36 +00:00
|
|
|
dic = {
|
|
|
|
'address': {
|
2015-08-11 18:58:52 +00:00
|
|
|
'formatted': getattr(self.userinfo, 'address_formatted', None),
|
|
|
|
'street_address': getattr(self.userinfo, 'address_street_address', None),
|
|
|
|
'locality': getattr(self.userinfo, 'address_locality', None),
|
|
|
|
'region': getattr(self.userinfo, 'address_region', None),
|
|
|
|
'postal_code': getattr(self.userinfo, 'address_postal_code', None),
|
|
|
|
'country': getattr(self.userinfo, 'address_country', None),
|
2015-01-30 20:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dic
|