1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
1 |
# IVLE - Informatics Virtual Learning Environment
|
2 |
# Copyright (C) 2007-2008 The University of Melbourne
|
|
3 |
#
|
|
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.
|
|
8 |
#
|
|
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.
|
|
13 |
#
|
|
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
|
|
17 |
||
18 |
# Module: authenticate
|
|
19 |
# Author: Matt Giuca
|
|
20 |
# Date: 19/2/2008
|
|
21 |
||
22 |
# Provides a mechanism for authenticating a username and password, and
|
|
23 |
# returning a yes/no response.
|
|
24 |
||
25 |
# Has a plugin interface for authentication modules.
|
|
26 |
# An authentication module is a Python module with an "auth" function,
|
|
27 |
# which accepts 3 positional arguments.
|
|
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
28 |
# plugin_module.auth(store, login, password, user)
|
29 |
# store is an open store connected to the IVLE database.
|
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
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
|
|
39 |
# be found.
|
|
40 |
# Raising an AuthError implies a hard failure, with an appropriate error
|
|
41 |
# message. No more auth will be done.
|
|
42 |
||
43 |
import sys |
|
44 |
import os |
|
45 |
||
1080.1.12
by me at id
ivle.auth.autherror: Remove, moving AuthError into ivle.auth itself. |
46 |
from ivle.auth import AuthError |
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
47 |
import ivle.database |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
48 |
|
1278
by William Grant
No more ivle.conf in ivle.auth. |
49 |
def authenticate(config, store, login, password): |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
50 |
"""Determines whether a particular login/password combination is
|
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
51 |
valid for the given database. The password is in cleartext.
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
52 |
|
53 |
Returns a User object containing the user's details on success.
|
|
54 |
Raises an AuthError containing an appropriate error message on failure.
|
|
55 |
||
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
56 |
'store' is expected to be a storm.store.Store connected to the IVLE
|
57 |
database to which we should authenticate.
|
|
58 |
||
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
59 |
The User returned is guaranteed to be in the IVLE database.
|
60 |
This could be from reading or writing to the DB. If authenticate can't
|
|
61 |
find the user in the DB, it may get user data from some other source
|
|
62 |
(such as LDAP) and actually write it to the DB before returning.
|
|
63 |
"""
|
|
64 |
# WARNING: Both username and password may contain any characters, and must
|
|
65 |
# be sanitized within this function.
|
|
66 |
# (Not SQL-sanitized, just sanitized to our particular constraints).
|
|
67 |
# TODO Sanitize username
|
|
68 |
||
69 |
# Spawn a DB object just for making this call.
|
|
70 |
# (This should not spawn a DB connection on each page reload, only when
|
|
71 |
# there is no session object to begin with).
|
|
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
72 |
|
73 |
user = ivle.database.User.get_by_login(store, login) |
|
74 |
||
1278
by William Grant
No more ivle.conf in ivle.auth. |
75 |
for modname, m in get_auth_modules(config): |
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
76 |
# May raise an AuthError - allow to propagate
|
77 |
auth_result = m(store, login, password, user) |
|
78 |
if auth_result is None: |
|
79 |
# Can't auth with this module; try another
|
|
80 |
pass
|
|
81 |
elif auth_result == False: |
|
82 |
return None |
|
83 |
elif isinstance(auth_result, ivle.database.User): |
|
84 |
if user is not None and auth_result is not user: |
|
85 |
# If user is not None, then it must return the same user
|
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
86 |
raise AuthError("Internal error: " |
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
87 |
"Bad authentication module %s (changed user)" |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
88 |
% repr(modname)) |
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
89 |
elif user is None: |
90 |
# We just got ourselves some user details from an external
|
|
91 |
# source. Put them in the DB.
|
|
92 |
store.add(auth_result) |
|
1099.1.122
by William Grant
Don't allow authentication to succeed if the account has expired or is |
93 |
|
94 |
# Don't allow login if it is expired or disabled.
|
|
1099.1.124
by William Grant
Disallow login only when the user is disabled or expired, not just invalid. |
95 |
if auth_result.state == 'disabled' or auth_result.account_expired: |
1516
by William Grant
Make the error message for disabled accounts a little more descriptive. |
96 |
raise AuthError( |
97 |
"Your account is disabled. Please contact an "
|
|
98 |
"administrator if you believe this to be in error.") |
|
1099.1.122
by William Grant
Don't allow authentication to succeed if the account has expired or is |
99 |
|
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
100 |
return auth_result |
101 |
else: |
|
102 |
raise AuthError("Internal error: " |
|
103 |
"Bad authentication module %s (bad return type)" |
|
104 |
% repr(modname)) |
|
105 |
# No auths checked out; fail.
|
|
106 |
raise AuthError() |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
107 |
|
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
108 |
def simple_db_auth(store, login, password, user): |
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
109 |
"""
|
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.
|
|
115 |
"""
|
|
116 |
if user is None: |
|
117 |
# The login doesn't exist. Therefore return None so we can try other
|
|
118 |
# means of authentication.
|
|
119 |
return None |
|
1080.1.14
by me at id
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface |
120 |
|
121 |
# They should always match, but it's best to be sure!
|
|
122 |
assert(user.login == login) |
|
123 |
||
124 |
auth_result = user.authenticate(password) |
|
1079
by William Grant
Merge setup-refactor branch. This completely breaks existing installations; |
125 |
# auth_result is either True, False (fail) or None (try another)
|
126 |
if auth_result is None: |
|
127 |
return None |
|
128 |
elif auth_result: |
|
129 |
return user |
|
130 |
else: |
|
131 |
raise AuthError() |
|
132 |
||
1278
by William Grant
No more ivle.conf in ivle.auth. |
133 |
def get_auth_modules(config): |
134 |
"""Get the auth modules defined in the configuration.
|
|
135 |
||
136 |
Returns a list of (name, function object)s. This list consists of
|
|
137 |
simple_db_auth, plus the "auth" functions of all the plugin auth modules.
|
|
138 |
"""
|
|
139 |
||
140 |
oldpath = sys.path |
|
141 |
# Allow imports to get files from this directory.
|
|
142 |
# Get the directory that this module (authenticate) is in
|
|
143 |
authpath = os.path.split(sys.modules[__name__].__file__)[0] |
|
144 |
# Add it to sys.path
|
|
145 |
sys.path.append(authpath) |
|
146 |
||
147 |
auth_modules = [("simple_db_auth", simple_db_auth)] |
|
148 |
for modname in config['auth']['modules']: |
|
149 |
try: |
|
150 |
mod = __import__(modname) |
|
151 |
except ImportError: |
|
152 |
raise AuthError("Internal error: Can't import auth module %s" |
|
153 |
% repr(modname)) |
|
154 |
except ValueError: |
|
155 |
# If auth_modules is "", we may get an empty string - ignore
|
|
156 |
continue
|
|
157 |
try: |
|
158 |
authfunc = mod.auth |
|
159 |
except AttributeError: |
|
160 |
raise AuthError("Internal error: Auth module %s has no 'auth' " |
|
161 |
"function" % repr(modname)) |
|
162 |
auth_modules.append((modname, authfunc)) |
|
163 |
||
164 |
# Restore the old path, without this directory in it.
|
|
165 |
sys.path = oldpath |
|
166 |
return auth_modules |