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

« back to all changes in this revision

Viewing changes to ivle/auth/authenticate.py

  • Committer: Matt Giuca
  • Date: 2009-12-01 04:27:58 UTC
  • mfrom: (1164.2.46 sphinx-docs)
  • Revision ID: matt.giuca@gmail.com-20091201042758-wuxd9bdec00c283i
Merged sphinx-docs branch. This adds Sphinx documentation for the entire IVLE system (for system administrators and developers), and removes all of our random old document files (all either irrelevant, or moved into the Sphinx docs nicely). Currently incomplete, but ready to merge.

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
48
47
import ivle.database
49
48
 
50
 
def authenticate(store, login, password):
 
49
def authenticate(config, store, login, password):
51
50
    """Determines whether a particular login/password combination is
52
51
    valid for the given database. The password is in cleartext.
53
52
 
73
72
 
74
73
    user = ivle.database.User.get_by_login(store, login)
75
74
 
76
 
    for modname, m in auth_modules:
 
75
    for modname, m in get_auth_modules(config):
77
76
        # May raise an AuthError - allow to propagate
78
77
        auth_result = m(store, login, password, user)
79
78
        if auth_result is None:
129
128
    else:
130
129
        raise AuthError()
131
130
 
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))
 
131
def get_auth_modules(config):
 
132
    """Get the auth modules defined in the configuration.
 
133
 
 
134
    Returns a list of (name, function object)s. This list consists of
 
135
    simple_db_auth, plus the "auth" functions of all the plugin auth modules.
 
136
    """
 
137
 
 
138
    oldpath = sys.path
 
139
    # Allow imports to get files from this directory.
 
140
    # Get the directory that this module (authenticate) is in
 
141
    authpath = os.path.split(sys.modules[__name__].__file__)[0]
 
142
    # Add it to sys.path
 
143
    sys.path.append(authpath)
 
144
 
 
145
    auth_modules = [("simple_db_auth", simple_db_auth)]
 
146
    for modname in config['auth']['modules']:
 
147
        try:
 
148
            mod = __import__(modname)
 
149
        except ImportError:
 
150
            raise AuthError("Internal error: Can't import auth module %s"
 
151
                % repr(modname))
 
152
        except ValueError:
 
153
            # If auth_modules is "", we may get an empty string - ignore
 
154
            continue
 
155
        try:
 
156
            authfunc = mod.auth
 
157
        except AttributeError:
 
158
            raise AuthError("Internal error: Auth module %s has no 'auth' "
 
159
                "function" % repr(modname))
 
160
        auth_modules.append((modname, authfunc))
 
161
 
 
162
    # Restore the old path, without this directory in it.
 
163
    sys.path = oldpath
 
164
    return auth_modules