Python example code to query LDAP for a user's affiliation:

## This simple example program demonstrates how to take the name of a given authenticated user
## and querying ldap.mit.edu to determine if the user is a employee, student, or affiliate.
 
## This is one way to make an authorization decision that is not soley based on a 
## successful authentication.
 
## If authenticate with a certificate the environment variable that I care 
## about isSSL_CLIENT_S_DN_Email and in my case the value will be pbh@MIT.EDU
 
 
import os, cgi
 
# Headers and an extra blank line
print 'Content-type: text/html'
print
 
# Body
print '<html><body>'
keys = os.environ.keys()
keys.sort()
## Uncomment the following two lines if you want to display all of the environment variables
## that are available to your python code from the web server:
##for k in keys:
##    print '<p>%s: %s</p>' % (cgi.escape(k), cgi.escape(os.environ[k]))
 
##print '<hr>'
##print '<p> %s:' % (cgi.escape('SSL_CLIENT_S_DN_Email'))
##print ' %s</p>' % (os.environ['SSL_CLIENT_S_DN_Email'])
 
 
## The following line places the undecorated username of the authenticated user into "searchid"
searchid = os.environ['SSL_CLIENT_S_DN_Email'].split('@')
 
print '<p>The authenticated username is:'
print ' %s</p>' % (searchid[0])
 
 
import ldap
 
## first you must open a connection to the server
try:
    l = ldap.open("ldap.mit.edu")
    ## searching doesn't require a bind in LDAP V3.  If you're using LDAP v2, set the next line appropriately
    ## and do a bind as shown in the above example.
    # you can also set this to ldap.VERSION2 if you're using a v2 directory
    # you should  set the next option to ldap.VERSION2 if you're using a v2 directory
    l.protocol_version = ldap.VERSION3    
except ldap.LDAPError, e:
    print e
    # handle error however you like
 
 
## The next lines will also need to be changed to support your search requirements and directory
baseDN = "dc=mit,dc=edu"
searchScope = ldap.SCOPE_SUBTREE
## retrieve all attributes - again adjust to your needs - see documentation for more options
retrieveAttributes = None 
searchFilter = "uid=" + searchid[0]
 
try:
    ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
    result_set = []
    while 1:
        result_type, result_data = l.result(ldap_result_id, 0)
        if (result_data == []):
            break
        else:
            ## here you don't have to append to a list
            ## you could do whatever you want with the individual entry
            ## The appending to list is just for illustration. 
            if result_type == ldap.RES_SEARCH_ENTRY:
                result_set.append(result_data)
                print '<p>The entire directory was searched and %s was found</p>' % searchid[0]
    print '<p>The detailed ldap information about this user, that you find find while searching anonymously is:</p>'
    print result_set
 
except ldap.LDAPError, e:
    print e
 
print '<p>Now were going to search specific portions of the directory to determine what type of affiliation this user has to MIT.'
print ' This information can be used to make an authorization decision, if this is appropriate for you application or service.</p>'
 
 
## Now let's explicitly check to see if the user is an employee
## Note that we change the baseDN and searchScope
baseDN = "ou=employees,dc=mit,dc=edu"
searchScope = ldap.SCOPE_ONELEVEL
## retrieve all attributes - again adjust to your needs - see documentation for more options
retrieveAttributes = None 
searchFilter = "uid=" + searchid[0]
 
try:
    foundp = 0
    ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
    result_set = []
    while 1:
        result_type, result_data = l.result(ldap_result_id, 0)
        if (result_data == []):
            if (foundp == 0):
                print '<p>%s is not an employee</p>' % searchid[0]
            break
        else:
            ## here you don't have to append to a list
            ## you could do whatever you want with the individual entry
            ## The appending to list is just for illustration. 
            if result_type == ldap.RES_SEARCH_ENTRY:
                result_set.append(result_data)
                print '<p>%s is an employee</p>' % searchid[0]
                foundp = 1
 
 
except ldap.LDAPError, e:
    print e
 
## Now let's explicitly check to see if the user is a student
## Note that we change the baseDN and searchScope
baseDN = "ou=students,dc=mit,dc=edu"
searchScope = ldap.SCOPE_ONELEVEL
## retrieve all attributes - again adjust to your needs - see documentation for more options
retrieveAttributes = None 
searchFilter = "uid=" + searchid[0]
 
try:
    foundp = 0
    ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
    result_set = []
    while 1:
        result_type, result_data = l.result(ldap_result_id, 0)
        if (result_data == []):
            if (foundp == 0):
                print '<p>%s is not a student</P>' % searchid[0]
            break
        else:
            ## here you don't have to append to a list
            ## you could do whatever you want with the individual entry
            ## The appending to list is just for illustration. 
            if result_type == ldap.RES_SEARCH_ENTRY:
                result_set.append(result_data)
                print '<p>%s is a student</p>' % searchid[0]
                foundp = 1
 
 
except ldap.LDAPError, e:
    print e
 
 
## Now let's explicitly check to see if the user is an affiliate
## Note that we change the baseDN and searchScope
baseDN = "ou=affiliates,dc=mit,dc=edu"
searchScope = ldap.SCOPE_ONELEVEL
## retrieve all attributes - again adjust to your needs - see documentation for more options
retrieveAttributes = None 
searchFilter = "uid=" + searchid[0]
 
try:
    foundp = 0
    ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
    result_set = []
    while 1:
        result_type, result_data = l.result(ldap_result_id, 0)
        if (result_data == []):
            if (foundp == 0):
                print '<p>%s is not an affiliate</p>' % searchid[0]
            break
        else:
            ## here you don't have to append to a list
            ## you could do whatever you want with the individual entry
            ## The appending to list is just for illustration. 
            if result_type == ldap.RES_SEARCH_ENTRY:
                result_set.append(result_data)
                print '<p>%s is an affiliate</p>' % searchid[0]
                foundp = 1
 
 
except ldap.LDAPError, e:
    print e
 
print '</body></html>'