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

« back to all changes in this revision

Viewing changes to ivle/auth/authenticate.py

  • Committer: David Coles
  • Date: 2010-08-30 03:26:13 UTC
  • Revision ID: coles.david@gmail.com-20100830032613-d14vng0jkelniu3l
python-console: Fix globals broken with new JSON library.

simplejson always returns unicode strings. cJSON would return ordinary strings 
if possible. cPickle.loads() only accepts strings. At present we use pickle 
version 0 so they should all works as ASCII strings. Higher versions of pickle 
are not plain ASCII and are likely to break this and so this should be fixed 
at some point.

Also replaced unconditional exception with one that catches Pickle errors. Not 
sure the best way to report failures of these functions.

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:
91
90
                # We just got ourselves some user details from an external
92
91
                # source. Put them in the DB.
93
92
                store.add(auth_result)
94
 
                pass
 
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(
 
97
                    "Your account is disabled. Please contact an "
 
98
                    "administrator if you believe this to be in error.")
 
99
 
95
100
            return auth_result
96
101
        else:
97
102
            raise AuthError("Internal error: "
125
130
    else:
126
131
        raise AuthError()
127
132
 
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))
 
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