23
23
# returning a yes/no response.
25
25
# Has a plugin interface for authentication modules.
26
# An authentication module is a callable object which accepts 3 positional
28
# plugin_auth_func(dbconn, login, password, user)
26
# An authentication module is a Python module with an "auth" function,
27
# which accepts 3 positional arguments.
28
# plugin_module.auth(dbconn, login, password, user)
29
29
# dbconn is an open connection to the IVLE database.
30
30
# login and password are required strings, password is cleartext.
31
31
# user is a User object or None.
40
40
# Raising an AuthError implies a hard failure, with an appropriate error
41
41
# message. No more auth will be done.
43
from autherror import AuthError
48
class AuthError(Exception):
49
def __init__(self, message="Invalid username or password."):
50
self.message = message
51
self.args = (message,)
53
48
def authenticate(login, password):
54
49
"""Determines whether a particular login/password combination is
76
71
user = dbconn.get_user(login)
77
for m in auth_modules:
72
for modname, m in auth_modules:
78
73
# May raise an AuthError - allow to propagate
79
74
auth_result = m(dbconn, login, password, user)
80
75
if auth_result is None:
86
81
if user is not None and auth_result is not user:
87
82
# If user is not None, then it must return the same user
88
83
raise AuthError("Internal error: "
89
"Bad authentication module (changed user)")
84
"Bad authentication module %s (changed user)"
91
87
# We just got ourselves some user details from an external
92
88
# source. Put them in the DB.
97
93
raise AuthError("Internal error: "
98
"Bad authentication module (bad return type)")
94
"Bad authentication module %s (bad return type)"
99
96
# No auths checked out; fail.
101
98
except common.db.DBException:
123
120
raise AuthError()
125
def ldap_auth(dbconn, login, password, user):
127
A plugin auth function, as described above.
128
This one authenticates against an LDAP server.
129
Returns user if successful. Raises AuthError if unsuccessful.
130
Also raises AuthError if the LDAP server had an unexpected error.
133
l = ldap.initialize(conf.ldap_url)
134
# ldap_format_string contains a "%s" to put the login name
135
l.simple_bind_s(conf.ldap_format_string % login, password)
136
except ldap.INVALID_CREDENTIALS:
138
except Exception, msg:
139
raise AuthError("Internal error (LDAP auth): %s" % repr(msg))
140
# Got here - Must have successfully authenticated with LDAP
122
# Create a global variable "auth_modules", a list of (name, function object)s.
123
# This list consists of simple_db_auth, plus the "auth" functions of all the
124
# plugin auth modules.
143
# List of auth plugin modules, in the order to try them
126
auth_modules = [("simple_db_auth", simple_db_auth)]
127
for modname in conf.auth_modules.split(','):
129
mod = __import__(modname)
131
raise AuthError("Internal error: Can't import auth module %s"
135
except AttributeError:
136
raise AuthError("Internal error: Auth module %s has no 'auth' "
137
"function" % repr(modname))
138
auth_modules.append((modname, authfunc))