1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# Module: authenticate
22
# Provides a mechanism for authenticating a username and password, and
23
# returning a yes/no response.
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)
29
# dbconn is an open connection to the IVLE database.
30
# login and password are required strings, password is cleartext.
31
# user is a User object or None.
32
# If it's a User object, it must return the same object if it returns a user.
33
# This object should describe the user logging in.
34
# It may be None if the user is not known to this DB.
35
# Returns either a User object or None, or raises an AuthError.
36
# Returning a User object implies success, and also gives details about the
37
# user if none were known to the DB (such details will be written to the DB).
38
# Returning None implies a soft failure, and that another auth method should
40
# Raising an AuthError implies a hard failure, with an appropriate error
41
# message. No more auth will be done.
48
class AuthError(Exception):
49
def __init__(self, message="Invalid username or password."):
50
self.message = message
51
self.args = (message,)
53
def authenticate(login, password):
54
"""Determines whether a particular login/password combination is
55
valid. The password is in cleartext.
57
Returns a User object containing the user's details on success.
58
Raises an AuthError containing an appropriate error message on failure.
60
The User returned is guaranteed to be in the IVLE database.
61
This could be from reading or writing to the DB. If authenticate can't
62
find the user in the DB, it may get user data from some other source
63
(such as LDAP) and actually write it to the DB before returning.
65
# WARNING: Both username and password may contain any characters, and must
66
# be sanitized within this function.
67
# (Not SQL-sanitized, just sanitized to our particular constraints).
68
# TODO Sanitize username
70
# Spawn a DB object just for making this call.
71
# (This should not spawn a DB connection on each page reload, only when
72
# there is no session object to begin with).
73
dbconn = common.db.DB()
76
user = dbconn.get_user(login)
77
for m in auth_modules:
78
# May raise an AuthError - allow to propagate
79
auth_result = m(dbconn, login, password, user)
80
if auth_result is None:
81
# Can't auth with this module; try another
83
elif auth_result == False:
85
elif isinstance(auth_result, common.user.User):
86
if user is not None and auth_result is not user:
87
# If user is not None, then it must return the same user
88
raise AuthError("Internal error: "
89
"Bad authentication module (changed user)")
91
# We just got ourselves some user details from an external
92
# source. Put them in the DB.
93
# TODO: Write user to DB
97
raise AuthError("Internal error: "
98
"Bad authentication module (bad return type)")
99
# No auths checked out; fail.
101
except common.db.DBException:
102
# If our attempt to get the named user from the db fails,
103
# then the login name is unknown.
108
def simple_db_auth(dbconn, login, password, user):
110
A plugin auth function, as described above.
111
This one just authenticates against the local database.
112
Returns None if the password in the DB is NULL, indicating that another
113
auth method should be used.
114
Raises an AuthError if mismatched, indicating failure to auth.
116
auth_result = dbconn.user_authenticate(login, password)
117
# auth_result is either True, False (fail) or None (try another)
118
if auth_result is None:
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
143
# List of auth plugin modules, in the order to try them