Show More
@@ -0,0 +1,58 b'' | |||||
|
1 | # Extended version of Mercurial WSGI wrapper, adding | |||
|
2 | # some minimal HTTP auth. | |||
|
3 | ||||
|
4 | from base64 import b64decode | |||
|
5 | from wsgiref.simple_server import make_server | |||
|
6 | from mercurial import demandimport; demandimport.enable() | |||
|
7 | from mercurial.hgweb import hgweb | |||
|
8 | ||||
|
9 | PORT = 8080 | |||
|
10 | CONFIG_PATH = 'auth_hg_serve.cfg' | |||
|
11 | ||||
|
12 | ||||
|
13 | class TrivialAuth(object): | |||
|
14 | """Authorizes if user==password + user starts from abc""" | |||
|
15 | ||||
|
16 | def __init__(self, application): | |||
|
17 | self.application = application | |||
|
18 | ||||
|
19 | def __call__(self, environ, start_response): | |||
|
20 | if self.is_authenticated(environ.get('HTTP_AUTHORIZATION')): | |||
|
21 | return self.application(environ, start_response) | |||
|
22 | return self.password_prompt(environ, start_response) | |||
|
23 | ||||
|
24 | def is_authenticated(self, header): | |||
|
25 | if not header: | |||
|
26 | return False | |||
|
27 | _, encoded = header.split(None, 1) | |||
|
28 | decoded = b64decode(encoded).decode('utf-8') | |||
|
29 | username, password = decoded.split(':', 1) | |||
|
30 | return username == password and username.startswith("abc") | |||
|
31 | ||||
|
32 | def password_prompt(self, environ, start_response): | |||
|
33 | start_response( | |||
|
34 | '401 Authentication Required', | |||
|
35 | [ | |||
|
36 | ('Content-Type', 'text/html'), | |||
|
37 | ('WWW-Authenticate', 'Basic realm="HGLogin"'), | |||
|
38 | ]) | |||
|
39 | return [b'Please login'] | |||
|
40 | ||||
|
41 | ||||
|
42 | def dummy_app(environ, start_response): | |||
|
43 | start_response('200 OK', [('Content-Type', 'text/html')]) | |||
|
44 | return [b'Hello, world!'] | |||
|
45 | ||||
|
46 | ||||
|
47 | if __name__ == '__main__': | |||
|
48 | # httpd = make_server('', PORT, TrivialAuth(dummy_app)) | |||
|
49 | ||||
|
50 | httpd = make_server('', PORT, TrivialAuth(hgweb(CONFIG_PATH))) | |||
|
51 | # application = hgweb(config_path) | |||
|
52 | ||||
|
53 | # httpd = make_server('', 8080, TrivialAuth(application)) | |||
|
54 | print('Serving on port 8080...') | |||
|
55 | try: | |||
|
56 | httpd.serve_forever() | |||
|
57 | except KeyboardInterrupt: | |||
|
58 | pass |
General Comments 0
You need to be logged in to leave comments.
Login now