2017-08-03 12:40:52 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-03-28 17:11:39 +02:00
|
|
|
import ldap
|
|
|
|
|
2017-08-03 12:40:52 +02:00
|
|
|
from allauth.account.models import EmailAddress
|
|
|
|
from allauth.socialaccount.providers.base import ProviderAccount
|
|
|
|
from allauth_cas.providers import CASProvider
|
|
|
|
|
2018-03-28 17:11:39 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2017-08-03 12:40:52 +02:00
|
|
|
|
|
|
|
class ClipperAccount(ProviderAccount):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ClipperProvider(CASProvider):
|
|
|
|
id = 'clipper'
|
|
|
|
name = 'Clipper'
|
|
|
|
account_class = ClipperAccount
|
|
|
|
|
|
|
|
def extract_email(self, data):
|
2018-01-02 17:06:12 +01:00
|
|
|
uid, extra = data
|
|
|
|
return '{}@clipper.ens.fr'.format(uid.strip().lower())
|
2017-08-03 12:40:52 +02:00
|
|
|
|
2018-04-18 22:31:24 +02:00
|
|
|
def extract_uid(self, data):
|
|
|
|
# Normalize UID
|
|
|
|
uid, _ = data
|
|
|
|
uid = uid.lower().strip()
|
|
|
|
return uid
|
|
|
|
|
2017-08-03 12:40:52 +02:00
|
|
|
def extract_common_fields(self, data):
|
2018-03-28 17:11:39 +02:00
|
|
|
def get_names(clipper):
|
2018-03-29 19:06:36 +02:00
|
|
|
assert clipper.isalnum()
|
2018-03-28 17:11:39 +02:00
|
|
|
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 '', ''
|
|
|
|
|
2017-08-03 12:40:52 +02:00
|
|
|
common = super(ClipperProvider, self).extract_common_fields(data)
|
2018-03-28 17:11:39 +02:00
|
|
|
fn, ln = get_names(common['username'])
|
2017-08-03 12:40:52 +02:00
|
|
|
common['email'] = self.extract_email(data)
|
2018-03-28 17:11:39 +02:00
|
|
|
common['name'] = fn
|
|
|
|
common['last_name'] = ln
|
2017-08-03 12:40:52 +02:00
|
|
|
return common
|
|
|
|
|
|
|
|
def extract_email_addresses(self, data):
|
|
|
|
return [
|
|
|
|
EmailAddress(
|
2018-01-02 17:06:12 +01:00
|
|
|
email=self.extract_email(data),
|
|
|
|
verified=True, primary=True,
|
|
|
|
),
|
2017-08-03 12:40:52 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
def extract_extra_data(self, data):
|
2018-01-02 17:06:12 +01:00
|
|
|
extra_data = super(ClipperProvider, self).extract_extra_data(data)
|
|
|
|
extra_data['email'] = self.extract_email(data)
|
|
|
|
return extra_data
|
|
|
|
|
|
|
|
def message_suggest_caslogout_on_logout(self, request):
|
|
|
|
return (
|
|
|
|
self.get_settings()
|
|
|
|
.get('MESSAGE_SUGGEST_CASLOGOUT_ON_LOGOUT', True)
|
|
|
|
)
|
2017-08-03 12:40:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
provider_classes = [ClipperProvider]
|