3 from django.contrib.auth.middleware import RemoteUserMiddleware
4 from django.contrib.auth.backends import RemoteUserBackend
5 from django.contrib import auth
6 from django.core.exceptions import ObjectDoesNotExist
8 def zephyr(msg, clas='remit', instance='log', rcpt='adehnert',):
9 proc = subprocess.Popen(
10 ['zwrite', '-d', '-n', '-c', clas, '-i', instance, rcpt, ],
11 stdin=subprocess.PIPE, stdout=subprocess.PIPE
15 class ScriptsRemoteUserMiddleware(RemoteUserMiddleware):
16 header = 'SSL_CLIENT_S_DN_Email'
18 class ScriptsRemoteUserBackend(RemoteUserBackend):
19 def clean_username(self, username, ):
21 name, domain = username.split('@')
22 assert domain.upper() == 'MIT.EDU'
26 def configure_user(self, user, ):
27 username = user.username
28 user.password = "ScriptsSSLAuth"
30 con = ldap.open('ldap.mit.edu')
31 con.simple_bind_s("", "")
33 fields = ['cn', 'sn', 'givenName', 'mail', ]
34 result = con.search_s('dc=mit,dc=edu', ldap.SCOPE_SUBTREE, 'uid=%s'%username, fields)
36 user.first_name = result[0][1]['givenName'][0]
37 user.last_name = result[0][1]['sn'][0]
38 user.email = result[0][1]['mail'][0]
40 user.groups.add(auth.models.Group.objects.get(name='mit'))
41 except ObjectDoesNotExist:
42 print "Failed to retrieve mit group"
44 user.groups.add(auth.models.Group.objects.get(name='autocreated'))
45 except ObjectDoesNotExist:
46 print "Failed to retrieve autocreated group"