From da4cfb54c8fa53e64a552007370cea3a515ea7cc Mon Sep 17 00:00:00 2001 From: Qwann Date: Wed, 28 Mar 2018 13:07:36 +0200 Subject: [PATCH 1/5] typo --- example/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/settings.py b/example/settings.py index 0f6a73d..d5402e7 100644 --- a/example/settings.py +++ b/example/settings.py @@ -84,7 +84,7 @@ AUTH_PASSWORD_VALIDATORS = [ { 'NAME': ( 'django.contrib.auth.password_validation' - '.UserAttributeSimilarityValidator', + '.UserAttributeSimilarityValidator' ), }, { From 2779ce5d7dc7b6808249aff380f263411ac3dd0a Mon Sep 17 00:00:00 2001 From: Qwann Date: Wed, 28 Mar 2018 17:11:39 +0200 Subject: [PATCH 2/5] fetch name and last_name from ldap --- allauth_ens/providers/clipper/provider.py | 37 ++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/allauth_ens/providers/clipper/provider.py b/allauth_ens/providers/clipper/provider.py index 85b83d9..5c75c41 100644 --- a/allauth_ens/providers/clipper/provider.py +++ b/allauth_ens/providers/clipper/provider.py @@ -1,9 +1,12 @@ # -*- coding: utf-8 -*- +import ldap + from allauth.account.models import EmailAddress from allauth.socialaccount.providers.base import ProviderAccount - from allauth_cas.providers import CASProvider +from django.conf import settings + class ClipperAccount(ProviderAccount): pass @@ -19,8 +22,40 @@ class ClipperProvider(CASProvider): return '{}@clipper.ens.fr'.format(uid.strip().lower()) def extract_common_fields(self, data): + def get_names(clipper): + try: + ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, + ldap.OPT_X_TLS_NEVER) + l = ldap.initialize("ldaps://ldap.spi.ens.fr:636") + l.set_option(ldap.OPT_REFERRALS, 0) + l.set_option(ldap.OPT_PROTOCOL_VERSION, 3) + l.set_option(ldap.OPT_X_TLS, ldap.OPT_X_TLS_DEMAND) + l.set_option(ldap.OPT_X_TLS_DEMAND, True) + l.set_option(ldap.OPT_DEBUG_LEVEL, 255) + l.set_option(ldap.OPT_NETWORK_TIMEOUT, 10) + l.set_option(ldap.OPT_TIMEOUT, 10) + + info = l.search_s('dc=spi,dc=ens,dc=fr', + ldap.SCOPE_SUBTREE, + ('(uid=%s)' % (clipper,)), + [str("cn"), ]) + + if len(info) > 0: + fullname = info[0][1].get('cn', [''])[0].decode("utf-8") + first_name, last_name = fullname.split(' ', 1) + return first_name, last_name + + except ldap.LDAPError: + pass + + return '', '' + common = super(ClipperProvider, self).extract_common_fields(data) + fn, ln = get_names(common['username']) common['email'] = self.extract_email(data) + common['name'] = fn + common['last_name'] = ln + print(common) return common def extract_email_addresses(self, data): From 9ec1dae1c50938293a9562a299bc81661c84a421 Mon Sep 17 00:00:00 2001 From: Qwann Date: Wed, 28 Mar 2018 17:31:03 +0200 Subject: [PATCH 3/5] add ldap as requirement --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index ed56dc6..6b8231b 100644 --- a/setup.py +++ b/setup.py @@ -47,5 +47,6 @@ setup( 'django-allauth', 'django-allauth-cas>=1.0.0b2,<1.1', 'django-widget-tweaks', + 'python-ldap', ], ) From 3be031957fd5403beb7003eaf83276232697adc8 Mon Sep 17 00:00:00 2001 From: Qwann Date: Wed, 28 Mar 2018 19:09:46 +0200 Subject: [PATCH 4/5] remove debug print --- allauth_ens/providers/clipper/provider.py | 1 - 1 file changed, 1 deletion(-) diff --git a/allauth_ens/providers/clipper/provider.py b/allauth_ens/providers/clipper/provider.py index 5c75c41..49aa834 100644 --- a/allauth_ens/providers/clipper/provider.py +++ b/allauth_ens/providers/clipper/provider.py @@ -55,7 +55,6 @@ class ClipperProvider(CASProvider): common['email'] = self.extract_email(data) common['name'] = fn common['last_name'] = ln - print(common) return common def extract_email_addresses(self, data): From 6b66ae326477bab79dbe8e290d21fef140b3cc02 Mon Sep 17 00:00:00 2001 From: Qwann Date: Thu, 29 Mar 2018 19:06:36 +0200 Subject: [PATCH 5/5] check valid clipper --- allauth_ens/providers/clipper/provider.py | 1 + 1 file changed, 1 insertion(+) diff --git a/allauth_ens/providers/clipper/provider.py b/allauth_ens/providers/clipper/provider.py index 49aa834..d14bee2 100644 --- a/allauth_ens/providers/clipper/provider.py +++ b/allauth_ens/providers/clipper/provider.py @@ -23,6 +23,7 @@ class ClipperProvider(CASProvider): def extract_common_fields(self, data): def get_names(clipper): + assert clipper.isalnum() try: ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)