Skip to content

Instantly share code, notes, and snippets.

@tomekowal
Last active May 22, 2019 06:40
Show Gist options
  • Save tomekowal/1577f9cef7326a38b285 to your computer and use it in GitHub Desktop.
Save tomekowal/1577f9cef7326a38b285 to your computer and use it in GitHub Desktop.
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDGfYKCcOkzpH+Ps31sn82IHu03ykpt1/sydC2I2xDQ7WYNffGP
0wOfVpPHYv+frfRM0J7QiHlJsVe/5/aCAt3lsdlBDBgbTyt0ZURWzPfDYZAZduaa
SYvi3TYmX2X6pnTdielpgeLUm4hMixQn4rSU92ZYjRM6BzEDvaRBtlCEDQIDAQAB
AoGABV6t4jOFy5TespeLhB3KpK0Zg9HgM5qNPC8ycCplCRHzGdhXaamuxmfENbey
7LH7Evzt3vxiM6Etm45998R2Fk1vMZptCNK+z+6GDbPMfbZYBWPHugZxaOxJMXEm
kOKY/sMk9c8yHAgXu3CY821emTqKGw3vlOKfJ5xlzsc0BCECQQD/tLw1GsOt4VQF
G9hyvMarf8Nev/dOXKoByeKZNJ0rQa517EkH/dNIW+3K1iWnWPwi5bg1RBrHSD5d
Ur6cnZrFAkEAxrfvBRoKIB1prR5/h2VdCra1tTpyJ03shQ/lS3Y7q7tKM/BIcvnD
pFp/VcWV5QKRTU6VEyIG0ZdNdFPw+mV4qQJANqqyDfxh1zu0Fb+FP8piAuo2n3wU
6Bjhc65wlmZsL4JERQu19VJrqC2XIeU0eZmJlgBcqG6d9vrX8mWtQJhlLQJBALNQ
0sylvLFSGUPwm1+QJi8+xCUvAkdZV66Ss3Qu0pWawdCwaI/ntZUfXHwHecVXVcqO
uJK1R7XldC0eeftZiVECQEm+M714AvVPTr20CxYNd8ATm1vc5FwoIvb4dKucPszG
Y3BncK0FCiz+OT71aryvAMrxAgBloo70+a27mkoSBlI=
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIICsDCCAhmgAwIBAgIJAKZVVjLHpH/xMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAlBMMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTUwNzI1MjI0NDU2WhcNMTYwNzI0MjI0NDU2WjBF
MQswCQYDVQQGEwJQTDETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
gQDGfYKCcOkzpH+Ps31sn82IHu03ykpt1/sydC2I2xDQ7WYNffGP0wOfVpPHYv+f
rfRM0J7QiHlJsVe/5/aCAt3lsdlBDBgbTyt0ZURWzPfDYZAZduaaSYvi3TYmX2X6
pnTdielpgeLUm4hMixQn4rSU92ZYjRM6BzEDvaRBtlCEDQIDAQABo4GnMIGkMB0G
A1UdDgQWBBQ5bbCUPMtXTOrJ1t6SQGupXBVLwzB1BgNVHSMEbjBsgBQ5bbCUPMtX
TOrJ1t6SQGupXBVLw6FJpEcwRTELMAkGA1UEBhMCUEwxEzARBgNVBAgTClNvbWUt
U3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJAKZVVjLH
pH/xMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAf4Cs6u+FUzGAPhSS
fWubVsTulMhY6NWavgY7D60ae1ygtQ9OPmZO4RJPY0aA9US0PkdZgbET5Iixtoqm
TH0CCpiCOq/NMOSimcW9RDqPJx2FXlASz3ziAs9jO+7x/sRZExFGX/w0mVfWXrJL
BT6z0uG3RbrD7NAbULjwnrOm8ng=
-----END CERTIFICATE-----
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
import sys
import base64
import ssl
key = ""
class AuthHandler(SimpleHTTPRequestHandler):
''' Main class to present webpages and authentication. '''
def do_HEAD(self):
print "send header"
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_AUTHHEAD(self):
print "send header"
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
global key
''' Present frontpage with user authentication. '''
if self.headers.getheader('Authorization') == None:
self.do_AUTHHEAD()
self.wfile.write('no auth header received')
pass
elif self.headers.getheader('Authorization') == 'Basic '+key:
SimpleHTTPRequestHandler.do_GET(self)
pass
else:
self.do_AUTHHEAD()
self.wfile.write(self.headers.getheader('Authorization'))
self.wfile.write('not authenticated')
pass
def test(HandlerClass = AuthHandler,
ServerClass = BaseHTTPServer.HTTPServer):
httpd = ServerClass(('', 4443), HandlerClass)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./server.pem', server_side=True)
httpd.serve_forever()
if __name__ == '__main__':
if len(sys.argv)<2:
print "usage SimpleAuthServer.py [username:password]"
sys.exit()
key = base64.b64encode(sys.argv[1])
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment