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

« back to all changes in this revision

Viewing changes to www/auth/authenticate.py

  • Committer: stevenbird
  • Date: 2008-02-19 21:17:21 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:512
Renaming of problems to exercises (initial commit).
Fix up module naming (exercises sometimes called tutorials).

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
# returning a yes/no response.
24
24
 
25
25
# Has a plugin interface for authentication modules.
26
 
# An authentication module is a Python module with an "auth" function,
27
 
# which accepts 3 positional arguments.
28
 
# plugin_module.auth(dbconn, login, password, user)
 
26
# An authentication module is a callable object which accepts 3 positional
 
27
# arguments.
 
28
# plugin_auth_func(dbconn, login, password, user)
29
29
# dbconn is an open connection to the IVLE database.
30
30
# login and password are required strings, password is cleartext.
31
31
# user is a User object or None.
40
40
# Raising an AuthError implies a hard failure, with an appropriate error
41
41
# message. No more auth will be done.
42
42
 
43
 
from autherror import AuthError
44
43
import conf
45
44
import common.db
46
45
import common.user
 
46
import ldap
47
47
 
48
 
import sys
49
 
import os
 
48
class AuthError(Exception):
 
49
    def __init__(self, message="Invalid username or password."):
 
50
        self.message = message
 
51
        self.args = (message,)
50
52
 
51
53
def authenticate(login, password):
52
54
    """Determines whether a particular login/password combination is
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
73
    dbconn = common.db.DB()
72
 
 
73
 
    try:
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:
82
 
        for modname, m in auth_modules:
 
74
    user = dbconn.get_user(login)
 
75
    try:
 
76
        for m in auth_modules:
83
77
            # May raise an AuthError - allow to propagate
84
78
            auth_result = m(dbconn, login, password, user)
85
79
            if auth_result is None:
91
85
                if user is not None and auth_result is not user:
92
86
                    # If user is not None, then it must return the same user
93
87
                    raise AuthError("Internal error: "
94
 
                        "Bad authentication module %s (changed user)"
95
 
                        % repr(modname))
96
 
                elif user is None:
 
88
                        "Bad authentication module (changed user)")
 
89
                if user is None:
97
90
                    # We just got ourselves some user details from an external
98
91
                    # source. Put them in the DB.
99
 
                    dbconn.create_user(auth_result)
 
92
                    # TODO: Write user to DB
100
93
                    pass
101
94
                return auth_result
102
95
            else:
103
96
                raise AuthError("Internal error: "
104
 
                    "Bad authentication module %s (bad return type)"
105
 
                    % repr(modname))
 
97
                    "Bad authentication module (bad return type)")
106
98
        # No auths checked out; fail.
107
99
        raise AuthError()
108
100
    finally:
116
108
    auth method should be used.
117
109
    Raises an AuthError if mismatched, indicating failure to auth.
118
110
    """
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
123
111
    auth_result = dbconn.user_authenticate(login, password)
124
112
    # auth_result is either True, False (fail) or None (try another)
125
113
    if auth_result is None:
129
117
    else:
130
118
        raise AuthError()
131
119
 
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 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))
 
120
def ldap_auth(dbconn, login, password, user):
 
121
    """
 
122
    A plugin auth function, as described above.
 
123
    This one authenticates against an LDAP server.
 
124
    Returns user if successful. Raises AuthError if unsuccessful.
 
125
    Also raises AuthError if the LDAP server had an unexpected error.
 
126
    """
 
127
    try:
 
128
        l = ldap.initialize(conf.ldap_url)
 
129
        # ldap_format_string contains a "%s" to put the login name
 
130
        l.simple_bind_s(conf.ldap_format_string % login, password)
 
131
    except ldap.INVALID_CREDENTIALS:
 
132
        raise AuthError()
 
133
    except Exception, msg:
 
134
        raise AuthError("Internal error (LDAP auth): %s" % repr(msg))
 
135
    # Got here - Must have successfully authenticated with LDAP
 
136
    return user
 
137
 
 
138
# List of auth plugin modules, in the order to try them
 
139
auth_modules = [
 
140
    simple_db_auth,
 
141
    ldap_auth,
 
142
]