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

« back to all changes in this revision

Viewing changes to ivle/auth/authenticate.py

  • Committer: mattgiuca
  • Date: 2008-01-12 15:35:53 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:201
Added "test" directory.
Added make_date_test.py, a short script I wrote to test the date format
algorithm committed in the previous revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
28
 
# plugin_module.auth(store, login, password, user)
29
 
# store is an open store connected 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
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
 
 
46
 
from ivle.auth import AuthError
47
 
import ivle.conf
48
 
import ivle.database
49
 
 
50
 
def authenticate(store, login, password):
51
 
    """Determines whether a particular login/password combination is
52
 
    valid for the given database. The password is in cleartext.
53
 
 
54
 
    Returns a User object containing the user's details on success.
55
 
    Raises an AuthError containing an appropriate error message on failure.
56
 
 
57
 
    'store' is expected to be a storm.store.Store connected to the IVLE
58
 
    database to which we should authenticate.
59
 
 
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.
64
 
    """
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
69
 
 
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
 
 
74
 
    user = ivle.database.User.get_by_login(store, login)
75
 
 
76
 
    for modname, m in auth_modules:
77
 
        # May raise an AuthError - allow to propagate
78
 
        auth_result = m(store, login, password, user)
79
 
        if auth_result is None:
80
 
            # Can't auth with this module; try another
81
 
            pass
82
 
        elif auth_result == False:
83
 
            return None
84
 
        elif isinstance(auth_result, ivle.database.User):
85
 
            if user is not None and auth_result is not user:
86
 
                # If user is not None, then it must return the same user
87
 
                raise AuthError("Internal error: "
88
 
                    "Bad authentication module %s (changed user)"
89
 
                    % repr(modname))
90
 
            elif user is None:
91
 
                # We just got ourselves some user details from an external
92
 
                # source. Put them in the DB.
93
 
                store.add(auth_result)
94
 
                pass
95
 
            return auth_result
96
 
        else:
97
 
            raise AuthError("Internal error: "
98
 
                "Bad authentication module %s (bad return type)"
99
 
                % repr(modname))
100
 
    # No auths checked out; fail.
101
 
    raise AuthError()
102
 
 
103
 
def simple_db_auth(store, login, password, user):
104
 
    """
105
 
    A plugin auth function, as described above.
106
 
    This one just authenticates against the local database.
107
 
    Returns None if the password in the DB is NULL, indicating that another
108
 
    auth method should be used.
109
 
    Raises an AuthError if mismatched, indicating failure to auth.
110
 
    """
111
 
    if user is None:
112
 
        # The login doesn't exist. Therefore return None so we can try other
113
 
        # means of authentication.
114
 
        return None
115
 
 
116
 
    # They should always match, but it's best to be sure!
117
 
    assert(user.login == login)
118
 
 
119
 
    auth_result = user.authenticate(password)
120
 
    # auth_result is either True, False (fail) or None (try another)
121
 
    if auth_result is None:
122
 
        return None
123
 
    elif auth_result:
124
 
        return user
125
 
    else:
126
 
        raise AuthError()
127
 
 
128
 
# Allow imports to get files from this directory.
129
 
# Get the directory that this module (authenticate) is in
130
 
authpath = os.path.split(sys.modules[__name__].__file__)[0]
131
 
# Add it to sys.path
132
 
sys.path.append(authpath)
133
 
 
134
 
# Create a global variable "auth_modules", a list of (name, function object)s.
135
 
# This list consists of simple_db_auth, plus the "auth" functions of all the
136
 
# plugin auth modules.
137
 
 
138
 
auth_modules = [("simple_db_auth", simple_db_auth)]
139
 
for modname in ivle.conf.auth_modules.split(','):
140
 
    try:
141
 
        mod = __import__(modname)
142
 
    except ImportError:
143
 
        raise AuthError("Internal error: Can't import auth module %s"
144
 
            % repr(modname))
145
 
    except ValueError:
146
 
        # If auth_modules is "", we may get an empty string - ignore
147
 
        continue
148
 
    try:
149
 
        authfunc = mod.auth
150
 
    except AttributeError:
151
 
        raise AuthError("Internal error: Auth module %s has no 'auth' "
152
 
            "function" % repr(modname))
153
 
    auth_modules.append((modname, authfunc))