Return to Snippet

Revision: 22759
at January 21, 2010 05:33 by manatlan


Initial Code
import Cookie,hashlib
md5 = lambda x : hashlib.md5(x).hexdigest()

class ControlAccess:
    def __init__(self, appReal,password):
        self.appReal = appReal
        self.password=password

    def __call__(self, environ, start_response):
        try:
            password = Cookie.SimpleCookie(environ.get("HTTP_COOKIE",""))["pass"].value
        except:
            password = ""

        if password==md5(self.password):
            for i in self.appReal(environ, start_response):
                yield i
        else:
            try:
                passw=environ['wsgi.input'].read(int(environ['CONTENT_LENGTH'])).split("=")[-1]
            except:
                passw=""
            if passw == self.password:
                cookie = Cookie.SimpleCookie()
                cookie["pass"] = md5(self.password)
                start_response('200 OK', [('Content-type','text/html'),('Set-Cookie',cookie["pass"].OutputString())])
                yield """<html><head><meta http-equiv="refresh" content="0; url=/" /></head><body>welcome</body></html>"""
            else:
                start_response('200 OK', [('Content-type','text/html')])
                yield """<form method="post">
                            Password <input type='password' name="password">
                            <input type='submit' value="ok">
                        </form>"""

Initial URL


Initial Description
Not very secure, but very handy ...
Use like that :

    app = your_wsgi_app()
    app = ControlAccess( app, "your_password")
    httpserve( app ) # wsgi server

Initial Title
Simplest wsgi middleware to control access with a form/cookie

Initial Tags
python, web

Initial Language
Python