1.13
[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 logger.info(u"\nProcess request")
19 logger.info(u"===============")
20 for k, v in request.META.items():
21 if k.startswith('MELLON') or k is 'REMOTE_USER':
22 logger.info('%s : %s' % (k, v))
23
24 if not saml_settings.SAML_AUTH:
25 return
26 return super(SPMiddleware, self).process_request(request)
27
28
29 def configure_user(sender, request, user, *args, **kwargs):
30 """
31 Au login, on synchronise les infos du serveur d'identités
32 avec le user local
33 """
34 if saml_settings.SAML_AUTH:
35 logger.info(u"* synchro du user local avec les infos de id.auf")
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
42 user_logged_in.connect(configure_user)