~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/auth/authenticate.py

  • Committer: mattgiuca
  • Date: 2008-02-19 08:50:23 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:510
setup.py: Added 2 new config options for the LDAP authentication server.

authenticate.py: Added new auth module, ldap_auth. Authenticates against the
    LDAP server specified in setup.py.
    (Tested).

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
# Raising an AuthError implies a hard failure, with an appropriate error
41
41
# message. No more auth will be done.
42
42
 
 
43
import conf
43
44
import common.db
44
45
import common.user
 
46
import ldap
45
47
 
46
48
class AuthError(Exception):
47
49
    def __init__(self, message="Invalid username or password."):
115
117
    else:
116
118
        raise AuthError()
117
119
 
 
120
def ldap_auth(dbconn, login, password, user):
 
121
    """
 
122
    A plugin auth function, as described above.
 
123
    This one authenticates against an LDAP server.
 
124
    Returns user if successful. Raises AuthError if unsuccessful.
 
125
    Also raises AuthError if the LDAP server had an unexpected error.
 
126
    """
 
127
    try:
 
128
        l = ldap.initialize(conf.ldap_url)
 
129
        # ldap_format_string contains a "%s" to put the login name
 
130
        l.simple_bind_s(conf.ldap_format_string % login, password)
 
131
    except ldap.INVALID_CREDENTIALS:
 
132
        raise AuthError()
 
133
    except Exception, msg:
 
134
        raise AuthError("Internal error (LDAP auth): %s" % repr(msg))
 
135
    # Got here - Must have successfully authenticated with LDAP
 
136
    return user
 
137
 
118
138
# List of auth plugin modules, in the order to try them
119
139
auth_modules = [
120
140
    simple_db_auth,
 
141
    ldap_auth,
121
142
]