1.15
[auf_django_saml.git] / auf / django / saml / middleware.py
CommitLineData
aa2ccaae
OL
1# -*- coding: utf-8 -*-
2
56de6221 3import logging
5350c773 4
56de6221 5from django.contrib.auth.signals import user_logged_in
aa2ccaae 6from django.contrib.auth.middleware import RemoteUserMiddleware
56de6221
OL
7from auf.django.saml import settings as saml_settings
8
5350c773
OL
9logger = logging.getLogger('SAML')
10
aa2ccaae
OL
11
12class SPMiddleware(RemoteUserMiddleware):
13
14 def process_request(self, request):
56de6221
OL
15 """
16 Log MELLON an REMOTE_USER
17 """
d5dab98c 18 info = u""
56de6221
OL
19 for k, v in request.META.items():
20 if k.startswith('MELLON') or k is 'REMOTE_USER':
80327e08 21 info += u"\n%s : %s" % (k, v)
56de6221 22
80327e08 23 logger.info(info)
56de6221 24 if not saml_settings.SAML_AUTH:
aa2ccaae 25 return
80327e08 26
aa2ccaae 27 return super(SPMiddleware, self).process_request(request)
56de6221
OL
28
29
30def configure_user(sender, request, user, *args, **kwargs):
31 """
32 Au login, on synchronise les infos du serveur d'identités
33 avec le user local
34 """
50992337
OL
35 if saml_settings.SAML_AUTH:
36 meta = request.META
37 user.email = meta['MELLON_mail']
38 user.first_name = meta['MELLON_gn']
39 user.last_name = meta['MELLON_sn']
40 user.save()
80327e08 41 logger.info(info)
56de6221
OL
42
43user_logged_in.connect(configure_user)