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

« back to all changes in this revision

Viewing changes to ivle/auth/authenticate.py

Move ivle.webapp.groups' media to the new framework.

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:
90
91
                # We just got ourselves some user details from an external
91
92
                # source. Put them in the DB.
92
93
                store.add(auth_result)
93
 
 
94
 
            # Don't allow login if it is expired or disabled.
95
 
            if auth_result.state == 'disabled' or auth_result.account_expired:
96
 
                raise AuthError("Account is not valid.")
97
 
 
 
94
                pass
98
95
            return auth_result
99
96
        else:
100
97
            raise AuthError("Internal error: "
128
125
    else:
129
126
        raise AuthError()
130
127
 
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
 
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))