18
18
# Module: authenticate
19
19
# Author: Matt Giuca
22
22
# Provides a mechanism for authenticating a username and password, and
23
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.
27
def authenticate(username, password):
28
"""Determines whether a particular username/password combination is
46
class AuthError(Exception):
47
def __init__(self, message="Invalid username or password."):
48
self.message = message
49
self.args = (message,)
51
def authenticate(login, password):
52
"""Determines whether a particular login/password combination is
29
53
valid. The password is in cleartext.
31
Returns None if failed to authenticate.
32
55
Returns a User object containing the user's details on success.
56
Raises an AuthError containing an appropriate error message on failure.
58
The User returned is guaranteed to be in the IVLE database.
59
This could be from reading or writing to the DB. If authenticate can't
60
find the user in the DB, it may get user data from some other source
61
(such as LDAP) and actually write it to the DB before returning.
36
# Just authenticate against the DB at the moment.
37
# Later we will provide other auth options such as LDAP.
39
63
# WARNING: Both username and password may contain any characters, and must
40
64
# be sanitized within this function.
41
65
# (Not SQL-sanitized, just sanitized to our particular constraints).
66
# TODO Sanitize username
43
68
# Spawn a DB object just for making this call.
44
69
# (This should not spawn a DB connection on each page reload, only when
45
70
# there is no session object to begin with).
46
71
dbconn = common.db.DB()
72
user = dbconn.get_user(login)
48
if not dbconn.user_authenticate(username, password):
50
return dbconn.get_user(username)
74
for m in auth_modules:
75
# May raise an AuthError - allow to propagate
76
auth_result = m(dbconn, login, password, user)
77
if auth_result is None:
78
# Can't auth with this module; try another
80
elif auth_result == False:
82
elif isinstance(auth_result, common.user.User):
83
if user is not None and auth_result is not user:
84
# If user is not None, then it must return the same user
85
raise AuthError("Internal error: "
86
"Bad authentication module (changed user)")
88
# We just got ourselves some user details from an external
89
# source. Put them in the DB.
90
# TODO: Write user to DB
94
raise AuthError("Internal error: "
95
"Bad authentication module (bad return type)")
96
# No auths checked out; fail.
101
def simple_db_auth(dbconn, login, password, user):
103
A plugin auth function, as described above.
104
This one just authenticates against the local database.
105
Returns None if the password in the DB is NULL, indicating that another
106
auth method should be used.
107
Raises an AuthError if mismatched, indicating failure to auth.
109
auth_result = dbconn.user_authenticate(login, password)
110
# auth_result is either True, False (fail) or None (try another)
111
if auth_result is None:
118
# List of auth plugin modules, in the order to try them