| [e68bc7a] | 1 | from django.contrib.auth.middleware import RemoteUserMiddleware |
|---|
| [c5898ff] | 2 | from django.contrib.auth.backends import RemoteUserBackend |
|---|
| 3 | from django.contrib import auth |
|---|
| [e02a4b2] | 4 | from django.core.exceptions import ObjectDoesNotExist |
|---|
| [e68bc7a] | 5 | |
|---|
| 6 | def zephyr(msg, clas='remit', instance='log', rcpt='adehnert',): |
|---|
| 7 | import os |
|---|
| 8 | os.system("zwrite -d -c '%s' -i '%s' '%s' -m '%s'" % (clas, instance, rcpt, msg, )) |
|---|
| 9 | |
|---|
| 10 | class ScriptsRemoteUserMiddleware(RemoteUserMiddleware): |
|---|
| 11 | header = 'SSL_CLIENT_S_DN_Email' |
|---|
| [c5898ff] | 12 | |
|---|
| 13 | class ScriptsRemoteUserBackend(RemoteUserBackend): |
|---|
| 14 | def clean_username(self, username, ): |
|---|
| [e68bc7a] | 15 | if '@' in username: |
|---|
| 16 | name, domain = username.split('@') |
|---|
| 17 | assert domain.upper() == 'MIT.EDU' |
|---|
| 18 | return name |
|---|
| 19 | else: |
|---|
| [c5898ff] | 20 | return username |
|---|
| [e8632f2] | 21 | def configure_user(self, user, ): |
|---|
| 22 | username = user.username |
|---|
| [213c1e0] | 23 | user.password = "ScriptsSSLAuth" |
|---|
| [e8632f2] | 24 | import ldap |
|---|
| 25 | con = ldap.open('ldap.mit.edu') |
|---|
| 26 | con.simple_bind_s("", "") |
|---|
| 27 | dn = "dc=mit,dc=edu" |
|---|
| 28 | fields = ['cn', 'sn', 'givenName', 'mail', ] |
|---|
| 29 | result = con.search_s('dc=mit,dc=edu', ldap.SCOPE_SUBTREE, 'uid=%s'%username, fields) |
|---|
| 30 | if len(result) == 1: |
|---|
| 31 | user.first_name = result[0][1]['givenName'][0] |
|---|
| 32 | user.last_name = result[0][1]['sn'][0] |
|---|
| 33 | user.email = result[0][1]['mail'][0] |
|---|
| [e02a4b2] | 34 | try: |
|---|
| 35 | user.groups.add(auth.models.Group.objects.get(name='mit')) |
|---|
| 36 | except ObjectDoesNotExist: |
|---|
| 37 | print "Failed to retrieve mit group" |
|---|
| 38 | try: |
|---|
| 39 | user.groups.add(auth.models.Group.objects.get(name='autocreated')) |
|---|
| 40 | except ObjectDoesNotExist: |
|---|
| 41 | print "Failed to retrieve autocreated group" |
|---|
| [213c1e0] | 42 | user.save() |
|---|
| [e8632f2] | 43 | return user |
|---|