~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/auth/ldap_login.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
        aliasname_attribute=None, # ('displayName') ldap attribute we get the aliasname from
73
73
        email_attribute=None, # ('mail') ldap attribute we get the email address from
74
74
        email_callback=None, # called to make up email address
75
 
        name_callback=None, # called to use a Wiki name different from the login name
76
75
        coding='utf-8', # coding used for ldap queries and result values
77
76
        timeout=10, # how long we wait for the ldap server [s]
78
77
        start_tls=0, # 0 = No, 1 = Try, 2 = Required
79
 
        tls_cacertdir=None,
80
 
        tls_cacertfile=None,
81
 
        tls_certfile=None,
82
 
        tls_keyfile=None,
 
78
        tls_cacertdir='',
 
79
        tls_cacertfile='',
 
80
        tls_certfile='',
 
81
        tls_keyfile='',
83
82
        tls_require_cert=0, # 0 == ldap.OPT_X_TLS_NEVER (needed for self-signed certs)
84
83
        bind_once=False, # set to True to only do one bind - useful if configured to bind as the user on the first attempt
85
 
        autocreate=False, # set to True if you want to autocreate user profiles
86
 
        name='ldap', # use e.g. 'ldap_pdc' and 'ldap_bdc' (or 'ldap1' and 'ldap2') if you auth against 2 ldap servers
87
 
        report_invalid_credentials=True, # whether to emit "invalid username or password" msg at login time or not
88
84
        ):
89
85
        self.server_uri = server_uri
90
86
        self.bind_dn = bind_dn
99
95
        self.aliasname_attribute = aliasname_attribute
100
96
        self.email_attribute = email_attribute
101
97
        self.email_callback = email_callback
102
 
        self.name_callback = name_callback
103
98
 
104
99
        self.coding = coding
105
100
        self.timeout = timeout
112
107
        self.tls_require_cert = tls_require_cert
113
108
 
114
109
        self.bind_once = bind_once
115
 
        self.autocreate = autocreate
116
 
        self.name = name
117
110
 
118
 
        self.report_invalid_credentials = report_invalid_credentials
119
111
 
120
112
    def login(self, request, user_obj, **kw):
121
113
        username = kw.get('username')
148
140
                        (ldap.OPT_X_TLS, self.start_tls),
149
141
                        #(ldap.OPT_X_TLS_ALLOW, 1),
150
142
                    ):
151
 
                        if value is not None:
 
143
                        if value:
152
144
                            ldap.set_option(option, value)
153
145
 
154
146
                server = self.server_uri
195
187
                        logging.warning("Search found more than one (%d) matches for %r." % (result_length, filterstr))
196
188
                    if result_length == 0:
197
189
                        logging.debug("Search found no matches for %r." % (filterstr, ))
198
 
                    if self.report_invalid_credentials:
199
 
                        return ContinueLogin(user_obj, _("Invalid username or password."))
200
 
                    else:
201
 
                        return ContinueLogin(user_obj)
 
190
                    return ContinueLogin(user_obj, _("Invalid username or password."))
202
191
 
203
192
                dn, ldap_dict = lusers[0]
204
193
                if not self.bind_once:
228
217
                        aliasname = sn
229
218
                aliasname = aliasname.decode(coding)
230
219
 
231
 
                if self.name_callback:
232
 
                    username = self.name_callback(ldap_dict)
233
 
 
234
220
                if email:
235
221
                    u = user.User(request, auth_username=username, auth_method=self.name, auth_attribs=('name', 'password', 'email', 'mailto_author', ))
236
222
                    u.email = email
239
225
                u.name = username
240
226
                u.aliasname = aliasname
241
227
                u.remember_me = 0 # 0 enforces cookie_lifetime config param
242
 
                logging.debug("creating user object with name %r email %r alias %r" % (username, email, aliasname))
 
228
                logging.debug("creating userprefs with name %r email %r alias %r" % (username, email, aliasname))
243
229
 
244
230
            except ldap.INVALID_CREDENTIALS, err:
245
231
                logging.debug("invalid credentials (wrong password?) for dn %r (username: %r)" % (dn, username))
246
232
                return CancelLogin(_("Invalid username or password."))
247
233
 
248
 
            if u and self.autocreate:
249
 
                logging.debug("calling create_or_update to autocreate user %r" % u.name)
 
234
            if u:
250
235
                u.create_or_update(True)
251
236
            return ContinueLogin(u)
252
237
 
257
242
            # method).
258
243
            logging.error("LDAP server %s failed (%s). "
259
244
                          "Trying to authenticate with next auth list entry." % (server, str(err)))
260
 
            return ContinueLogin(user_obj, _("LDAP server %(server)s failed.") % {'server': server})
 
245
            return ContinueLogin(user_obj, _("LDAP server %(server)s failed." % {'server': server}))
261
246
 
262
247
        except:
263
248
            logging.exception("caught an exception, traceback follows...")