From 78f1013ab1755fa53add844e3f094fe7d0f28d25 Mon Sep 17 00:00:00 2001 From: Evarin Date: Thu, 19 Apr 2018 12:34:52 +0200 Subject: [PATCH] =?UTF-8?q?Allauth=20pour=20g=C3=A9rer=20les=20archicubes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- allauth_archiens/__init__.py | 0 allauth_archiens/admin.py | 6 ++ allauth_archiens/apps.py | 8 +++ allauth_archiens/migrations/__init__.py | 0 allauth_archiens/models.py | 6 ++ allauth_archiens/provider.py | 80 +++++++++++++++++++++++++ allauth_archiens/tests.py | 6 ++ allauth_archiens/urls.py | 6 ++ allauth_archiens/views.py | 15 +++++ 9 files changed, 127 insertions(+) create mode 100644 allauth_archiens/__init__.py create mode 100644 allauth_archiens/admin.py create mode 100644 allauth_archiens/apps.py create mode 100644 allauth_archiens/migrations/__init__.py create mode 100644 allauth_archiens/models.py create mode 100644 allauth_archiens/provider.py create mode 100644 allauth_archiens/tests.py create mode 100644 allauth_archiens/urls.py create mode 100644 allauth_archiens/views.py diff --git a/allauth_archiens/__init__.py b/allauth_archiens/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/allauth_archiens/admin.py b/allauth_archiens/admin.py new file mode 100644 index 0000000..13be29d --- /dev/null +++ b/allauth_archiens/admin.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.contrib import admin + +# Register your models here. diff --git a/allauth_archiens/apps.py b/allauth_archiens/apps.py new file mode 100644 index 0000000..5339aac --- /dev/null +++ b/allauth_archiens/apps.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.apps import AppConfig + + +class AllauthArchiensConfig(AppConfig): + name = 'allauth_archiens' diff --git a/allauth_archiens/migrations/__init__.py b/allauth_archiens/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/allauth_archiens/models.py b/allauth_archiens/models.py new file mode 100644 index 0000000..1dfab76 --- /dev/null +++ b/allauth_archiens/models.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models + +# Create your models here. diff --git a/allauth_archiens/provider.py b/allauth_archiens/provider.py new file mode 100644 index 0000000..0b92e3e --- /dev/null +++ b/allauth_archiens/provider.py @@ -0,0 +1,80 @@ +from allauth_ens.providers.clipper.provider import ClipperProvider +import ldap + +class LongTermClipperProvider(ClipperProvider): + id = 'longterm_clipper' + + def extract_uid(self, data): + # Normalize UID + uid, _ = data + uid = uid.lower().strip() + more_data = self.get_ldap_infos(uid) + uid = "%s_%s" % (uid, more_data.get("annee", "00")) + print(uid) + return uid + + def get_ldap_infos(self, clipper): + if hasattr(self, "_ldap_infos"): + return self._ldap_infos + data = {} + assert clipper.isalnum() + 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"), + str("mailRoutingAddress"), + str("homeDirectory") ]) + + if len(info) > 0: + infos = info[0][1] + + # Nom + data['nom'] = infos.get('cn', [''])[0].decode("utf-8") + + # Parsing du homeDirectory pour la promotion + annee = '00' + promotion = 'Inconnue' + + if 'homeDirectory' in infos: + dirs = infos['homeDirectory'][0].split('/') + if dirs[1] == 'users': + annee = dirs[2] + dep = dirs[3] + dep = dict(DEPARTEMENTS_DEFAUT).get(dep.lower(), '') + promotion = u'%s %s' % (dep, annee) + data['annee'] = annee + data['promotion'] = promotion + + # Mail + pmail = infos.get('mailRoutingAddress', []) + data['email'] = '{}@clipper.ens.fr'.format(clipper.strip().lower()) if len(pmail) == 0 else pmail[0] + + + except ldap.LDAPError: + pass + + self._ldap_infos = data + return data + + def extract_common_fields(self, data): + common = super(ClipperProvider, self).extract_common_fields(data) + infos = self.get_ldap_infos(common['username']) + common['email'] = infos.get('email', + '{}@clipper.ens.fr'.format(common['username'].strip().lower())) + common['name'] = infos.get('nom', common['username']) + return common + + +provider_classes = [LongTermClipperProvider] diff --git a/allauth_archiens/tests.py b/allauth_archiens/tests.py new file mode 100644 index 0000000..5982e6b --- /dev/null +++ b/allauth_archiens/tests.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.test import TestCase + +# Create your tests here. diff --git a/allauth_archiens/urls.py b/allauth_archiens/urls.py new file mode 100644 index 0000000..07f0264 --- /dev/null +++ b/allauth_archiens/urls.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from allauth_cas.urls import default_urlpatterns + +from .provider import LongTermClipperProvider + +urlpatterns = default_urlpatterns(LongTermClipperProvider) diff --git a/allauth_archiens/views.py b/allauth_archiens/views.py new file mode 100644 index 0000000..7b48532 --- /dev/null +++ b/allauth_archiens/views.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +from allauth_cas import views + +from .provider import LongTermClipperProvider + + +class LongTermClipperCASAdapter(views.CASAdapter): + provider_id = LongTermClipperProvider.id + url = 'https://cas.eleves.ens.fr' + version = 3 + + +login = views.CASLoginView.adapter_view(LongTermClipperCASAdapter) +callback = views.CASCallbackView.adapter_view(LongTermClipperCASAdapter) +logout = views.CASLogoutView.adapter_view(LongTermClipperCASAdapter)