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

« back to all changes in this revision

Viewing changes to ivle/auth/authenticate.py

  • Committer: William Grant
  • Date: 2009-02-27 05:07:09 UTC
  • Revision ID: grantw@unimelb.edu.au-20090227050709-k16kvhyl50nzjbwm
Subject URLs now contain the short name (eg. info1) rather than the code
(eg. 600151).

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
import os
45
45
 
46
46
from ivle.auth import AuthError
 
47
import ivle.conf
47
48
import ivle.database
48
49
 
49
 
def authenticate(config, store, login, password):
 
50
def authenticate(store, login, password):
50
51
    """Determines whether a particular login/password combination is
51
52
    valid for the given database. The password is in cleartext.
52
53
 
72
73
 
73
74
    user = ivle.database.User.get_by_login(store, login)
74
75
 
75
 
    for modname, m in get_auth_modules(config):
 
76
    for modname, m in auth_modules:
76
77
        # May raise an AuthError - allow to propagate
77
78
        auth_result = m(store, login, password, user)
78
79
        if auth_result is None:
93
94
 
94
95
            # Don't allow login if it is expired or disabled.
95
96
            if auth_result.state == 'disabled' or auth_result.account_expired:
96
 
                raise AuthError(
97
 
                    "Your account is disabled. Please contact an "
98
 
                    "administrator if you believe this to be in error.")
 
97
                raise AuthError("Account is not valid.")
99
98
 
100
99
            return auth_result
101
100
        else:
130
129
    else:
131
130
        raise AuthError()
132
131
 
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
 
132
# Allow imports to get files from this directory.
 
133
# Get the directory that this module (authenticate) is in
 
134
authpath = os.path.split(sys.modules[__name__].__file__)[0]
 
135
# Add it to sys.path
 
136
sys.path.append(authpath)
 
137
 
 
138
# Create a global variable "auth_modules", a list of (name, function object)s.
 
139
# This list consists of simple_db_auth, plus the "auth" functions of all the
 
140
# plugin auth modules.
 
141
 
 
142
auth_modules = [("simple_db_auth", simple_db_auth)]
 
143
for modname in ivle.conf.auth_modules.split(','):
 
144
    try:
 
145
        mod = __import__(modname)
 
146
    except ImportError:
 
147
        raise AuthError("Internal error: Can't import auth module %s"
 
148
            % repr(modname))
 
149
    except ValueError:
 
150
        # If auth_modules is "", we may get an empty string - ignore
 
151
        continue
 
152
    try:
 
153
        authfunc = mod.auth
 
154
    except AttributeError:
 
155
        raise AuthError("Internal error: Auth module %s has no 'auth' "
 
156
            "function" % repr(modname))
 
157
    auth_modules.append((modname, authfunc))