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

« back to all changes in this revision

Viewing changes to www/auth/authenticate.py

  • Committer: mattgiuca
  • Date: 2008-02-21 07:08:07 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:533
auth/authenticate: Much done!
    Fixed immediate failure if user not in DB (now sets user to None to give
    it the chance to pulldown).
    Now writes the user to the DB if a user object is returned.
    Fixed simple_db_auth raising if the user is not in the DB. It now returns
    None, again, to give chance to pulldown.
    Fixed importing modules - the auth dir is not in sys.path so there are
    import errors. Did some foo (a LOT of magic) to get the auth dir in
    sys.path.

auth/ldap_auth: Fixed missing import.
auth/guest_auth: Added new auth module, guest. This is more of a toy.
    It auths guest/guest and also returns a new user which is written to the
    DB - so it's like a user pulldown module.

userdb/users.sql: Fixed rolenms allowed - these did not match caps.Role.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
import common.db
46
46
import common.user
47
47
 
 
48
import sys
 
49
import os
 
50
 
48
51
def authenticate(login, password):
49
52
    """Determines whether a particular login/password combination is
50
53
    valid. The password is in cleartext.
69
72
 
70
73
    try:
71
74
        user = dbconn.get_user(login)
 
75
    except common.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:
72
82
        for modname, m in auth_modules:
73
83
            # May raise an AuthError - allow to propagate
74
84
            auth_result = m(dbconn, login, password, user)
86
96
                elif user is None:
87
97
                    # We just got ourselves some user details from an external
88
98
                    # source. Put them in the DB.
89
 
                    # TODO: Write user to DB
 
99
                    dbconn.create_user(auth_result)
90
100
                    pass
91
101
                return auth_result
92
102
            else:
95
105
                    % repr(modname))
96
106
        # No auths checked out; fail.
97
107
        raise AuthError()
98
 
    except common.db.DBException:
99
 
        # If our attempt to get the named user from the db fails,
100
 
        # then the login name is unknown.
101
 
        raise AuthError()
102
108
    finally:
103
109
        dbconn.close()
104
110
 
110
116
    auth method should be used.
111
117
    Raises an AuthError if mismatched, indicating failure to auth.
112
118
    """
 
119
    if user is None:
 
120
        # The login doesn't exist. Therefore return None so we can try other
 
121
        # means of authentication.
 
122
        return None
113
123
    auth_result = dbconn.user_authenticate(login, password)
114
124
    # auth_result is either True, False (fail) or None (try another)
115
125
    if auth_result is None:
119
129
    else:
120
130
        raise AuthError()
121
131
 
 
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
 
122
138
# Create a global variable "auth_modules", a list of (name, function object)s.
123
139
# This list consists of simple_db_auth, plus the "auth" functions of all the
124
140
# plugin auth modules.