~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-05-05 06:46:58 UTC
  • mto: (1165.3.65 submissions-admin)
  • mto: This revision was merged to the branch mainline in revision 1247.
  • Revision ID: grantw@unimelb.edu.au-20090505064658-o913x4ooxxbjfo1q
Avoid clobbering the submission owner's privileges if they have offering privs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
# Has a plugin interface for authentication modules.
26
26
# An authentication module is a Python module with an "auth" function,
27
27
# which accepts 3 positional arguments.
28
 
# plugin_module.auth(dbconn, login, password, user)
29
 
# dbconn is an open connection to the IVLE database.
 
28
# plugin_module.auth(store, login, password, user)
 
29
# store is an open store connected to the IVLE database.
30
30
# login and password are required strings, password is cleartext.
31
31
# user is a User object or None.
32
32
# If it's a User object, it must return the same object if it returns a user.
43
43
import sys
44
44
import os
45
45
 
46
 
from autherror import AuthError
 
46
from ivle.auth import AuthError
47
47
import ivle.conf
48
 
import ivle.db
49
 
import ivle.user
 
48
import ivle.database
50
49
 
51
 
def authenticate(login, password):
 
50
def authenticate(store, login, password):
52
51
    """Determines whether a particular login/password combination is
53
 
    valid. The password is in cleartext.
 
52
    valid for the given database. The password is in cleartext.
54
53
 
55
54
    Returns a User object containing the user's details on success.
56
55
    Raises an AuthError containing an appropriate error message on failure.
57
56
 
 
57
    'store' is expected to be a storm.store.Store connected to the IVLE
 
58
    database to which we should authenticate.
 
59
 
58
60
    The User returned is guaranteed to be in the IVLE database.
59
61
    This could be from reading or writing to the DB. If authenticate can't
60
62
    find the user in the DB, it may get user data from some other source
68
70
    # Spawn a DB object just for making this call.
69
71
    # (This should not spawn a DB connection on each page reload, only when
70
72
    # there is no session object to begin with).
71
 
    dbconn = ivle.db.DB()
72
 
 
73
 
    try:
74
 
        user = dbconn.get_user(login)
75
 
    except ivle.db.DBException:
76
 
        # If our attempt to get the named user from the db fails,
77
 
        # then set user to None.
78
 
        # We may still auth (eg. by pulling details from elsewhere and writing
79
 
        # to DB).
80
 
        user = None
81
 
    try:
82
 
        for modname, m in auth_modules:
83
 
            # May raise an AuthError - allow to propagate
84
 
            auth_result = m(dbconn, login, password, user)
85
 
            if auth_result is None:
86
 
                # Can't auth with this module; try another
87
 
                pass
88
 
            elif auth_result == False:
89
 
                return None
90
 
            elif isinstance(auth_result, ivle.user.User):
91
 
                if user is not None and auth_result is not user:
92
 
                    # If user is not None, then it must return the same user
93
 
                    raise AuthError("Internal error: "
94
 
                        "Bad authentication module %s (changed user)"
95
 
                        % repr(modname))
96
 
                elif user is None:
97
 
                    # We just got ourselves some user details from an external
98
 
                    # source. Put them in the DB.
99
 
                    dbconn.create_user(auth_result)
100
 
                    pass
101
 
                return auth_result
102
 
            else:
 
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
103
87
                raise AuthError("Internal error: "
104
 
                    "Bad authentication module %s (bad return type)"
 
88
                    "Bad authentication module %s (changed user)"
105
89
                    % repr(modname))
106
 
        # No auths checked out; fail.
107
 
        raise AuthError()
108
 
    finally:
109
 
        dbconn.close()
110
 
 
111
 
def simple_db_auth(dbconn, login, password, user):
 
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
 
 
95
            # Don't allow login if it is expired or disabled.
 
96
            if auth_result.state == 'disabled' or auth_result.account_expired:
 
97
                raise AuthError("Account is not valid.")
 
98
 
 
99
            return auth_result
 
100
        else:
 
101
            raise AuthError("Internal error: "
 
102
                "Bad authentication module %s (bad return type)"
 
103
                % repr(modname))
 
104
    # No auths checked out; fail.
 
105
    raise AuthError()
 
106
 
 
107
def simple_db_auth(store, login, password, user):
112
108
    """
113
109
    A plugin auth function, as described above.
114
110
    This one just authenticates against the local database.
120
116
        # The login doesn't exist. Therefore return None so we can try other
121
117
        # means of authentication.
122
118
        return None
123
 
    auth_result = dbconn.user_authenticate(login, password)
 
119
 
 
120
    # They should always match, but it's best to be sure!
 
121
    assert(user.login == login)
 
122
 
 
123
    auth_result = user.authenticate(password)
124
124
    # auth_result is either True, False (fail) or None (try another)
125
125
    if auth_result is None:
126
126
        return None