]>
Commit | Line | Data |
---|---|---|
2a644ab1 AD |
1 | import subprocess |
2 | ||
606f938b | 3 | from django.contrib.auth.middleware import RemoteUserMiddleware |
34b7ba86 AD |
4 | from django.contrib.auth.backends import RemoteUserBackend |
5 | from django.contrib import auth | |
358136f9 | 6 | from django.core.exceptions import ObjectDoesNotExist |
606f938b AD |
7 | |
8 | def zephyr(msg, clas='remit', instance='log', rcpt='adehnert',): | |
2a644ab1 AD |
9 | proc = subprocess.Popen( |
10 | ['zwrite', '-d', '-n', '-c', clas, '-i', instance, rcpt, ], | |
11 | stdin=subprocess.PIPE, stdout=subprocess.PIPE | |
12 | ) | |
13 | proc.communicate(msg) | |
606f938b AD |
14 | |
15 | class ScriptsRemoteUserMiddleware(RemoteUserMiddleware): | |
16 | header = 'SSL_CLIENT_S_DN_Email' | |
34b7ba86 AD |
17 | |
18 | class ScriptsRemoteUserBackend(RemoteUserBackend): | |
19 | def clean_username(self, username, ): | |
606f938b AD |
20 | if '@' in username: |
21 | name, domain = username.split('@') | |
22 | assert domain.upper() == 'MIT.EDU' | |
23 | return name | |
24 | else: | |
34b7ba86 | 25 | return username |
9c504e48 AD |
26 | def configure_user(self, user, ): |
27 | username = user.username | |
48dbd9dc | 28 | user.password = "ScriptsSSLAuth" |
9c504e48 AD |
29 | import ldap |
30 | con = ldap.open('ldap.mit.edu') | |
31 | con.simple_bind_s("", "") | |
32 | dn = "dc=mit,dc=edu" | |
33 | fields = ['cn', 'sn', 'givenName', 'mail', ] | |
34 | result = con.search_s('dc=mit,dc=edu', ldap.SCOPE_SUBTREE, 'uid=%s'%username, fields) | |
35 | if len(result) == 1: | |
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] | |
358136f9 AD |
39 | try: |
40 | user.groups.add(auth.models.Group.objects.get(name='mit')) | |
41 | except ObjectDoesNotExist: | |
42 | print "Failed to retrieve mit group" | |
358136f9 AD |
43 | try: |
44 | user.groups.add(auth.models.Group.objects.get(name='autocreated')) | |
45 | except ObjectDoesNotExist: | |
46 | print "Failed to retrieve autocreated group" | |
48dbd9dc | 47 | user.save() |
9c504e48 | 48 | return user |