dgn | 28050da | 2015-10-19 10:16:43 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2015 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | """ Test server for testing Negotiate Authorization |
| 7 | |
| 8 | This is a minimal dummy server for testing HTTP Negotiate Authorization. |
| 9 | It is currently only used for manual testing. |
| 10 | |
| 11 | To use: |
| 12 | - Start on host. |
| 13 | - Use chrome://inspect to forward port 8080 from the target device to |
| 14 | the host. |
| 15 | - From Chrome on the target connect to localhost:8080. |
| 16 | - If HTTP Negotiate is working correctly the page should load as |
| 17 | "Talked to SPNEGO Authenticator" |
| 18 | |
| 19 | Please see //tools/android/kerberos/README.md for detailed instructions. |
| 20 | |
| 21 | """ |
| 22 | |
| 23 | # TODO(dgn) Replace with an EmbeddedTestServer based server in the test apk once |
| 24 | # the java version is ready. See http://crbug.com/488192 |
| 25 | |
| 26 | |
| 27 | import time |
| 28 | import BaseHTTPServer |
| 29 | |
| 30 | |
| 31 | HOST_NAME = 'localhost' |
| 32 | PORT_NUMBER = 8080 |
| 33 | |
| 34 | SUCCESS_HTML = ('<html><head>' |
| 35 | '<title>SPNEGO Negotiation completed!</title>' |
| 36 | '</head><body>' |
| 37 | '<p><b><big>Talked to SPNEGO Authenticator</big></b></p>' |
| 38 | '</body></html>') |
| 39 | |
| 40 | FAILURE_HTML = ('<html><head>' |
| 41 | '<title>Not Authorized</title>' |
| 42 | '</head><body>' |
| 43 | '<p><big>Did not talk to SPNEGO Authenticator</big></p>' |
| 44 | '</body></html>') |
| 45 | |
| 46 | WRONG_HEADER_HTML = ('<html><head>' |
| 47 | '<title>Not Authorized</title>' |
| 48 | '</head><body>' |
| 49 | '<p><big>Bad header value. Found "%s"</big></p>' |
| 50 | '</body></html>') |
| 51 | |
| 52 | |
| 53 | class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 54 | def do_HEAD(self): |
| 55 | self.send_response(401, 'negotiate') |
| 56 | self.send_header('WWW-Authenticate', 'negotiate') |
| 57 | self.end_headers() |
| 58 | |
| 59 | def do_GET(self): |
| 60 | """Respond to a GET request.""" |
| 61 | print('Path: ' + self.path) |
| 62 | print(self.headers) |
| 63 | auth_header = self.headers.getheader('Authorization') |
| 64 | |
| 65 | if not auth_header: |
| 66 | self.send_response(401) |
| 67 | self.send_header('WWW-Authenticate', 'negotiate') |
| 68 | self.send_header('Content-type', 'text/html') |
| 69 | self.end_headers() |
| 70 | self.wfile.write(FAILURE_HTML) |
| 71 | elif not auth_header.startswith('Negotiate '): |
| 72 | self.send_response(403) |
| 73 | self.send_header('Content-type', 'text/html') |
| 74 | self.end_headers() |
| 75 | self.wfile.write(WRONG_HEADER_HTML % auth_header) |
| 76 | else: |
| 77 | self.send_response(200) |
| 78 | self.send_header('Content-type', 'text/html') |
| 79 | self.end_headers() |
| 80 | self.wfile.write(SUCCESS_HTML) |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | server_class = BaseHTTPServer.HTTPServer |
| 85 | httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler) |
| 86 | print('%s Server Starts - %s:%s' % (time.asctime(), HOST_NAME, PORT_NUMBER)) |
| 87 | try: |
| 88 | httpd.serve_forever() |
| 89 | except KeyboardInterrupt: |
| 90 | pass |
| 91 | httpd.server_close() |
| 92 | print('%s Server Stops - %s:%s' % (time.asctime(), HOST_NAME, PORT_NUMBER)) |