Skip to content

Instantly share code, notes, and snippets.

@tarunc
Forked from mjallday/auth_server.py
Last active December 25, 2015 07:49
Show Gist options
  • Save tarunc/6942422 to your computer and use it in GitHub Desktop.
Save tarunc/6942422 to your computer and use it in GitHub Desktop.
import SimpleHTTPServer
import SocketServer
PORT = 8000
class Server(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
headers = self.headers.dict
print headers.get('x-original-uri')
# curl http://127.0.0.1:8000 -u username:password
if self.headers.dict.get('authorization') != 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=' or self.headers.dict.get('x-original-uri') == '/not?e=1':
self.send_response(401)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("")
else:
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("")
def serve_forever(port):
httpd = SocketServer.TCPServer(('', port), Server)
httpd.serve_forever()
if __name__ == "__main__":
serve_forever(8000)
import SimpleHTTPServer
import SocketServer
PORT = 8000
class Server(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
headers = self.headers.dict
# curl http://127.0.0.1:8000 -u username:password
if self.headers.dict.get('authorization') != 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=':
self.send_response(401)
else:
self.send_response(200)
def serve_forever(port):
httpd = SocketServer.TCPServer(('', port), Server)
httpd.serve_forever()
if __name__ == "__main__":
serve_forever(8000)
master_process off;
daemon off;
# Number of worker connections. 1024 is a good default
events {
worker_connections 1024;
}
http {
server { # simple load balancing
listen 80;
server_name localhost;
location / {
auth_request /auth;
proxy_pass http://notification_service;
proxy_read_timeout 300;
}
location = /auth {
proxy_pass http://auth_service;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_read_timeout 300;
}
}
upstream notification_service {
server 127.0.0.1:5000;
}
upstream auth_service {
server 127.0.0.1:8000;
}
}

ngx_http_auth_request_module

nginx.conf

location /private/ {
    auth_request /auth;
    ...
}

location = /auth {
    proxy_pass ...
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment