clean module
[auf_django_saml.git] / auf / django / saml / middleware.py
1 # -*- coding: utf-8 -*-
2
3 import logging
4
5 from django.contrib.auth.signals import user_logged_in
6 from django.contrib.auth.middleware import RemoteUserMiddleware
7 from auf.django.saml import settings as saml_settings
8
9 logger = logging.getLogger('SAML')
10
11
12 class SPMiddleware(RemoteUserMiddleware):
13
14 def process_request(self, request):
15 """
16 Log MELLON an REMOTE_USER
17 """
18 info = u""
19 for k, v in request.META.items():
20 if k.startswith('MELLON') or k is 'REMOTE_USER':
21 info += u"\n%s : %s" % (k, v.decode('utf-8'))
22
23 logger.info(info)
24 if not saml_settings.SAML_AUTH:
25 return
26
27 return super(SPMiddleware, self).process_request(request)
28
29
30 def 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 """
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()
41 logger.info(u"")
42
43 user_logged_in.connect(configure_user)