Ticket #3619: sagenb-3619.patch

File sagenb-3619.patch, 5.0 KB (added by was, 12 years ago)

new patch against sagenb that simply adds calls to log.msg in a few places, which will properly log user login attempts using the standard twisted loging facility

  • sagenb/notebook/avatars.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1258670552 28800
    # Node ID 698c4163e276db4da2d1073251dc23cc684fdcb5
    # Parent  99c2146479b6f9ea86df9f088b4fb3793514cfed
    trac 3619 -- log user logins to the logfile
    
    diff -r 99c2146479b6 -r 698c4163e276 sagenb/notebook/avatars.py
    a b  
    8989    implements(checkers.ICredentialsChecker)
    9090    credentialInterfaces = (credentials.IUsernamePassword,)
    9191
     92    # last_user stores the name of the last user to successfully login
     93    # -- this is used only to not store redundant entries in the log
     94    last_user = None   
     95
    9296    def add_user(self, username, password, email, account_type='user'):
    9397        self.check_username(username)
    9498        U = twist.notebook.add_user(username, password, email, account_type)
     
    112116        try:
    113117            U = twist.notebook.user(username)
    114118        except KeyError:
     119            # Failed login due to invalid username; log this fact and
     120            # return FailedLogin object.
     121            log.msg("Login attempt by unknown user '%s'."%username)
     122            PasswordChecker.last_user = None           
    115123            return defer.succeed(FailedLogin(username, failure_type = 'user'))
    116124
    117125        if U.password_is(password):
     126            # Correct password
    118127            if twist.notebook.user(username).is_suspended():
     128                # However, the user is suspended, so log this and return
     129                # FailedLogin object.
     130                log.msg("Login attempt by suspended user '%s'."%username)
     131                PasswordChecker.last_user = None
    119132                return defer.succeed(FailedLogin(username, failure_type = 'suspended'))
     133            # Valid non-suspended user.  If they didn't just login already, log this.
     134            if PasswordChecker.last_user != username:
     135                log.msg("User '%s' logged in."%username)
     136                # don't report same login multiple times in a row
     137                PasswordChecker.last_user = username
    120138            return defer.succeed(username)
    121139        else:
     140            # Valid user failed to login by typing incorrect password.
     141            log.msg("Login attempt by '%s' failed due to invalid password."%username)
     142            PasswordChecker.last_user = None
    122143            return defer.succeed(FailedLogin(username,failure_type='password'))
    123144           
    124145
  • sagenb/notebook/guard.py

    diff -r 99c2146479b6 -r 698c4163e276 sagenb/notebook/guard.py
    a b  
    114114        if not self.sessions and self.tick.running:
    115115            self.tick.stop()
    116116
    117 class MindManager(object):
    118     """Might want to use this"""
    119     def __init__(self, uid):
    120         self.uid = uid #uid is the session id (the cookie)
    121 
    122117class MySessionWrapper(object):
    123118    implements(iweb.IResource)
    124119   
    125120    cookieManager = None
    126     mindFactory = MindManager
    127121    sessionManager = SessionsManager()
    128122
    129123    # The interface to cred for when logging into the portal
     
    189183        def _success(avatar, request, segments):
    190184            iface, rsrc, logout = avatar
    191185            return rsrc, segments
    192         #mind = self.mindFactory(request, creds)
    193186        mind = [session.get_uid(), request.args, segments]
    194187        d = self.portal.login(creds, mind, self.credInterface)
    195188        d.addCallback(_success, request, segments)
     
    222215        #log.msg("=== requestPasswordAuthentication ===")
    223216        creds = self.getCredentials(request)
    224217        session, newCookie = self.sessionManager.createSession()
    225         mind = [newCookie, request.args, segments] 
     218        mind = [newCookie, request.args, segments]
    226219        d = self.portal.login(creds, mind, self.credInterface)
     220        # TODO: Note that self._loginSuccess gets called even if the login fails. 
    227221        d.addCallback(self._loginSuccess, session, creds, segments)
    228222        return d
    229223
     
    286280        session.set_authCreds(creds)
    287281        return rsrc, ()
    288282   
    289     def _loginFailure(self, *x): #TODO
    290         pass
    291         #log.msg("=== _loginFailure ===")
    292         #log.msg(str(x))
    293                  
    294283    def incorrectLoginError(self, error, ctx, segments, loginFailure):
    295284        pass
    296285
  • sagenb/notebook/twist.py

    diff -r 99c2146479b6 -r 698c4163e276 sagenb/notebook/twist.py
    a b  
    3737import bz2
    3838from cgi import escape
    3939
     40from twisted.python import log
    4041from twisted.web2 import server, http, resource, channel
    4142from twisted.web2 import static, http_headers, responsecode
    4243from twisted.web2.filter import gzip
     
    21372138                            **template_dict)
    21382139            return HTMLResponse(stream = form)
    21392140
     2141        log.msg("Created new user '%s'"%username)
     2142
    21402143        # POST-VALIDATION hooks.  All required fields should be valid.
    21412144        if notebook.conf()['email']:
    21422145            from sagenb.notebook.smtpsend import send_mail