#!/usr/bin/env python import re from cgi import escape import urllib # ----------------------------------------------------------------------------- def dict_to_table(ava, width=1): txt = [] txt.append('\n' % width) for prop, valarr in ava.items(): txt.append("\n") if isinstance(valarr, basestring): txt.append("\n" % str(prop)) try: txt.append("\n" % valarr.encode("utf8")) except AttributeError: txt.append("\n" % valarr) elif isinstance(valarr, list): i = 0 n = len(valarr) for val in valarr: if i == 0: txt.append("\n") if isinstance(val, dict): txt.append("\n") else: try: txt.append("\n" % val.encode("utf8")) except AttributeError: txt.append("\n" % val) if n > 1: txt.append("\n") n -= 1 i += 1 elif isinstance(valarr, dict): txt.append("\n" % prop) txt.append("\n") txt.append("\n") txt.append('
%s%s%s%s\n" % (len(valarr),prop)) else: txt.append("
\n") txt.extend(dict_to_table(val, lev+1, width-1)) txt.append("%s%s
%s\n") txt.extend(dict_to_table(valarr, lev+1, width-1)) txt.append("
\n') return txt def whoami(environ, start_response, user, logger): start_response('200 OK', [('Content-Type', 'text/html')]) identity = environ["repoze.who.identity"]["user"] response = dict_to_table(identity) return response[:] def not_found(environ, start_response): """Called if no URL matches.""" start_response('404 NOT FOUND', [('Content-Type', 'text/plain')]) return ['Not Found'] def not_authn(environ, start_response): start_response('401 Unauthorized', [('Content-Type', 'text/plain')]) return ['Unknown user'] # ---------------------------------------------------------------------------- # map urls to functions urls = [ (r'whoami$', whoami), ] # ---------------------------------------------------------------------------- def application(environ, start_response): """ The main WSGI application. Dispatch the current request to the functions from above and store the regular expression captures in the WSGI environment as `myapp.url_args` so that the functions from above can access the url placeholders. If nothing matches call the `not_found` function. :param environ: The HTTP application environment :param start_response: The application to run when the handling of the request is done :return: The response as a list of lines """ user = environ.get("REMOTE_USER", "") if not user: user = environ.get("repoze.who.identity", "") path = environ.get('PATH_INFO', '').lstrip('/') logger = environ.get('repoze.who.logger') logger and logger.info( " PATH: %s" % path) for regex, callback in urls: if user: match = re.search(regex, path) if match is not None: try: environ['myapp.url_args'] = match.groups()[0] except IndexError: environ['myapp.url_args'] = path return callback(environ, start_response, user, logger) else: return not_authn(environ, start_response) return not_found(environ, start_response) # ---------------------------------------------------------------------------- from repoze.who.config import make_middleware_with_config app_with_auth = make_middleware_with_config(application, {"here":"."}, './who.ini', log_file="repo.log") # ---------------------------------------------------------------------------- if __name__ == '__main__': from wsgiref.simple_server import make_server srv = make_server('localhost', 8087, app_with_auth) srv.serve_forever()