Esempio n. 1
0
 def checkpassword(realm, username, password):
     auth_providers = get_configured_providers()
     try:
         for auth_provider in auth_providers:
             try:
                 auth_provider.authenticate(username, password)
             except exc.InsufficientPrivileges:
                 # Fail fast in this case; we don't want to continue on to try other authenticators.
                 raise _LoginFailed()
             except exc.AuthError:
                 # Swallow other auth errors so it goes onto next authenticator in the list.
                 pass
             except:
                 # Other exceptions needs to get logged at least.
                 log.exception("Unexpected error authenticating user using {0!r}".format(auth_provider))
             else:
                 log.info("Authentication succeeded for username {0} using provider {1}".format(username, auth_provider))
                 break
         else:
             log.debug("Authenticators exhausted; login failed.")
             raise _LoginFailed()
     except _LoginFailed:
         auditlog.log(auditlog.CODE_AUTH_FAILED, comment=username)
         return False
     else:
         # Resolve the user using the *current value* for auth_provider (as that is the one that passed the auth.
         user = auth_provider.resolve_user(username)
         
         log.debug("Setting up cherrypy session with username={0}, user_id={1}".format(username, user.id))    
         cherrypy.session['username'] = username # @UndefinedVariable
         cherrypy.session['user_id'] = user.id # @UndefinedVariable
         
         auditlog.log(auditlog.CODE_AUTH_LOGIN)
         return True
Esempio n. 2
0
    def process_login(self, **kwargs):
        form = LoginForm(request_params())

        # TODO: Refactor to combine with the ensconce.server:checkpassword method.  Lots of duplicate
        # logic here.  AT MINIMUM MAKE SURE THAT ANY CHANGES HERE ARE REFLECTED THERE
        
        # This is a "flow-control" exception. ... You'll see. :)        
        class _LoginFailed(Exception):
            pass
        
        try:
            if not form.validate():
                raise _LoginFailed()
        
            username = form.username.data
            password = form.password.data
            
            for auth_provider in get_configured_providers():
                try:
                    auth_provider.authenticate(username, password)
                except exc.InsufficientPrivileges:
                    form.username.errors.append(ValidationError("Insufficient privileges to log in."))
                    # Fail fast in this case; we don't want to continue on to try other authenticators.
                    raise _LoginFailed()
                except exc.AuthError:
                    # Swallow other auth errors so it goes onto next authenticator in the list.
                    pass
                except:
                    # Other exceptions needs to get logged at least.
                    log.exception("Unexpected error authenticating user using {0!r}".format(auth_provider))
                else:
                    log.info("Authentication succeeded for username {0} using provider {1}".format(username, auth_provider))
                    break
            else:
                log.debug("Authenticators exhausted; login failed.")
                form.password.errors.append(ValidationError("Invalid username/password."))
                raise _LoginFailed()
            
        except _LoginFailed:
            auditlog.log(auditlog.CODE_AUTH_FAILED, comment=username)
            return render("login.html", {'auth_provider': config['auth.provider'], 'form': form})
        else:
            
            # Resolve the user using the *current value* for auth_provider (as that is the one that passed the auth.
            user = auth_provider.resolve_user(username)
            
            log.debug("Setting up cherrypy session with username={0}, user_id={1}".format(username, user.id))    
            cherrypy.session['username'] = username # @UndefinedVariable
            cherrypy.session['user_id'] = user.id # @UndefinedVariable
            
            auditlog.log(auditlog.CODE_AUTH_LOGIN)
            
            if form.redirect.data:
                raise cherrypy.HTTPRedirect(form.redirect.data)
            else:
                raise cherrypy.HTTPRedirect("/")