#!/bin/sh
''''which python  >/dev/null && exec python  "$0" "$@" # '''

# Copyright (C) 2014-2015 Nginx, Inc.

# Example of an application working on port 9000
# To interact with nginx-ldap-auth-daemon this application
# 1) accepts GET  requests on /login and responds with a login form
# 2) accepts POST requests on /login, sets a cookie, and responds with redirect

import sys, os, signal, base64, Cookie, cgi, urlparse
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

Listen = ('localhost', 9000)

import threading
from SocketServer import ThreadingMixIn
class AuthHTTPServer(ThreadingMixIn, HTTPServer):
    pass

class AppHandler(BaseHTTPRequestHandler):

    def do_GET(self):

        url = urlparse.urlparse(self.path)

        if url.path.startswith("/login"):
            return self.auth_form()

        self.send_response(200)
        self.end_headers()
        self.wfile.write('Hello, world! Requested URL: ' + self.path + '\n')


    # send login form html
    def auth_form(self, target = None):

        # try to get target location from header
        if target == None:
            target = self.headers.get('X-Target')

        # form cannot be generated if target is unknown
        if target == None:
            self.log_error('target url is not passed')
            self.send_response(500)
            return

        html="""
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv=Content-Type content="text/html;charset=UTF-8">
    <title>Auth form example</title>
  </head>
  <body>
    <form action="/login" method="post">
      <table>
        <tr>
          <td>Username: <input type="text" name="username"/></td>
        <tr>
          <td>Password: <input type="text" name="password"/></td>
        <tr>
          <td><input type="submit" value="Login"></td>
      </table>
        <input type="hidden" name="target" value="TARGET">
    </form>
  </body>
</html>"""

        self.send_response(200)
        self.end_headers()
        self.wfile.write(html.replace('TARGET', target))


    # processes posted form and sets the cookie with login/password
    def do_POST(self):

        # prepare arguments for cgi module to read posted form
        env = {'REQUEST_METHOD':'POST',
               'CONTENT_TYPE': self.headers['Content-Type'],}

        # read the form contents
        form = cgi.FieldStorage(fp = self.rfile, headers = self.headers,
                                environ = env)

        # extract required fields
        user = form.getvalue('username')
        passwd = form.getvalue('password')
        target = form.getvalue('target')

        if user != None and passwd != None and target != None:

            # form is filled, set the cookie and redirect to target
            # so that auth daemon will be able to use information from cookie

            self.send_response(302)

            # WARNING WARNING WARNING
            #
            # base64 is just an example method that allows to pack data into
            # a cookie. You definitely want to perform some encryption here
            # and share a key with auth daemon that extracts this information
            #
            # WARNING WARNING WARNING
            enc = base64.b64encode(user + ':' + passwd)
            self.send_header('Set-Cookie', 'nginxauth=' + enc + '; httponly')

            self.send_header('Location', target)
            self.end_headers()

            return

        self.log_error('some form fields are not provided')
        self.auth_form(target)


    def log_message(self, format, *args):
        if len(self.client_address) > 0:
            addr = BaseHTTPRequestHandler.address_string(self)
        else:
            addr = "-"

        sys.stdout.write("%s - - [%s] %s\n" % (addr,
                         self.log_date_time_string(), format % args))

    def log_error(self, format, *args):
        self.log_message(format, *args)


def exit_handler(signal, frame):
    sys.exit(0)

if __name__ == '__main__':
    server = AuthHTTPServer(Listen, AppHandler)
    signal.signal(signal.SIGINT, exit_handler)
    server.serve_forever()

@@ -397,44 +397,305 @@
 64, 
-Cookie, cgi, urlparse%0Afrom BaseHTTPS
+cgi%0Aif sys.version_info.major == 2:%0A    from urlparse import urlparse%0A    from Cookie import BaseCookie%0A    from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler%0Aelif sys.version_info.major == 3:%0A    from urllib.parse import urlparse%0A    from http.cookies import BaseCookie%0A    from http.s
 erve
@@ -786,16 +786,52 @@
 reading%0A
+if sys.version_info.major == 2:%0A    
 from Soc
@@ -862,16 +862,195 @@
 ngMixIn%0A
+elif sys.version_info.major == 3:%0A    from socketserver import ThreadingMixIn%0A%0A%0Adef ensure_bytes(data):%0A    return data if sys.version_info.major == 2 else data.encode(%22utf-8%22)%0A%0A%0A
 class Au
@@ -1185,17 +1185,8 @@
 l = 
-urlparse.
 urlp
@@ -1365,16 +1365,29 @@
 e.write(
+ensure_bytes(
 'Hello, 
@@ -1429,16 +1429,17 @@
  + '%5Cn')
+)
 %0A%0A%0A    #
@@ -2556,16 +2556,29 @@
 e.write(
+ensure_bytes(
 html.rep
@@ -2600,16 +2600,17 @@
 target))
+)
 %0A%0A%0A    #
@@ -3794,16 +3794,29 @@
 4encode(
+ensure_bytes(
 user + '
@@ -3827,16 +3827,17 @@
  passwd)
+)
 %0A       
@@ -3872,16 +3872,17 @@
 ookie', 
+b
 'nginxau
@@ -3894,16 +3894,17 @@
 + enc + 
+b
 '; httpo

Add Python3 compatibility to sample app