Commits

Sylvain Hellegouarch committed a717fb2

Implemented a login attribute for the request and answered #653

Comments (0)

Files changed (5)

cherrypy/_cprequest.py

     The 'relative path' portion of the Request-URI. This is relative
     to the script_name ('mount point') of the application which is
     handling this request."""
+
+    login = None
+    login__doc = """
+    When authentication is used during the request processing this is
+    set to 'False' if it failed and to the 'username' value if it succeeded.
+    The default 'None' implies that no authentication happened."""
     
     app = None
     app__doc = \

cherrypy/lib/auth.py

         # and compare it with what the user-agent provided
         if httpauth.checkResponse(ah, password, method=cherrypy.request.method,
                                   encrypt=encrypt):
+            cherrypy.request.login = ah["username"]
             return True
     
+        cherrypy.request.login = False
     return False
 
 def basic_auth(realm, users, encrypt=None):

cherrypy/lib/cptools.py

                 del cherrypy.response.headers["Content-Length"]
             return True
         else:
-            cherrypy.session[self.session_key] = username
+            cherrypy.session[self.session_key] = cherrypy.request.login = username
             self.on_login(username)
             raise cherrypy.HTTPRedirect(from_page or "/")
     
         username = sess.get(self.session_key)
         sess[self.session_key] = None
         if username:
+            cherrypy.request.login = None
             self.on_logout(username)
         raise cherrypy.HTTPRedirect(from_page)
     
                 # Delete Content-Length header so finalize() recalcs it.
                 del cherrypy.response.headers["Content-Length"]
             return True
-        
+        cherrypy.request.login = username
         self.on_check(username)
     
     def run(self):

cherrypy/test/test_httpauth.py

 
     class DigestProtected:
         def index(self):
-            return "This is protected by Digest auth."
+            return "Hello %s, you've been authorized." % cherrypy.request.login
         index.exposed = True
 
     class BasicProtected:
         def index(self):
-            return "This is protected by Basic auth."
+            return "Hello %s, you've been authorized." % cherrypy.request.login
         index.exposed = True
 
     def fetch_users():
         
         self.getPage('/basic/', [('Authorization', 'Basic dGVzdDp0ZXN0')])
         self.assertStatus('200 OK')
-        self.assertBody('This is protected by Basic auth.')
+        self.assertBody("Hello test, you've been authorized.")
 
     def testDigest(self):
         self.getPage("/digest/")
         auth = base_auth % (nonce, response, '00000001')
         self.getPage('/digest/', [('Authorization', auth)])
         self.assertStatus('200 OK')
-        self.assertBody('This is protected by Digest auth.')
+        self.assertBody("Hello test, you've been authorized.")
             
 if __name__ == "__main__":
     setup_server()

cherrypy/test/test_sessionauthenticate.py

     
     def check(username, password):
         # Dummy check_username_and_password function
-        if username != 'login' or password != 'password':
+        if username != 'test' or password != 'password':
             return u'Wrong login/password'
     
     class Test:
                       }
         
         def index(self):
-            return "Hi, you are logged in"
+            return "Hi %s, you are logged in" % cherrypy.request.login
         index.exposed = True
     
     cherrypy.tree.mount(Test())
         self.assertInBody('<form method="post" action="do_login">')
         
         # setup credentials
-        login_body = 'username=login&password=password&from_page=/'
+        login_body = 'username=test&password=password&from_page=/'
         
         # attempt a login
         self.getPage('/do_login', method='POST', body=login_body)
         
         # get the page now that we are logged in
         self.getPage('/', self.cookies)
-        self.assertBody('Hi, you are logged in')
+        self.assertBody('Hi test, you are logged in')
         
         # do a logout
         self.getPage('/do_logout', self.cookies)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.