| 1 |
=== modified file 'klaus/__init__.py' |
| 2 |
Index: klaus.git/klaus/__init__.py |
| 3 |
=================================================================== |
| 4 |
--- klaus.git.orig/klaus/__init__.py |
| 5 |
+++ klaus.git/klaus/__init__.py |
| 6 |
@@ -5,10 +5,9 @@ try: |
| 7 |
except ImportError: |
| 8 |
jinja2_autoescape_builtin = True |
| 9 |
import flask |
| 10 |
-import httpauth |
| 11 |
import dulwich.web |
| 12 |
from dulwich.errors import NotGitRepository |
| 13 |
-from klaus import views, utils |
| 14 |
+from klaus import httpauth, views, utils |
| 15 |
from klaus.repo import FancyRepo, InvalidRepo |
| 16 |
|
| 17 |
|
| 18 |
Index: klaus.git/klaus/httpauth.py |
| 19 |
=================================================================== |
| 20 |
--- /dev/null |
| 21 |
+++ klaus.git/klaus/httpauth.py |
| 22 |
@@ -0,0 +1,198 @@ |
| 23 |
+""" |
| 24 |
+Copyright (c) 2012 Jonas Haag <[email protected]>. License: ISC |
| 25 |
+ |
| 26 |
+This implements Digest Auth as specified in RFC 2069, i.e. without the |
| 27 |
+`qop` quality-of-protection, `cnonce` nonce count, ... options. |
| 28 |
+ |
| 29 |
+References to the algorithm (HA1, HA2, nonce, ...) are taken from Wikipedia: |
| 30 |
+ |
| 31 |
+ http://en.wikipedia.org/wiki/Digest_access_authentication |
| 32 |
+ |
| 33 |
+""" |
| 34 |
+import os |
| 35 |
+import re |
| 36 |
+import time |
| 37 |
+import hashlib |
| 38 |
+try: # Python 3 |
| 39 |
+ from urllib.request import parse_http_list, parse_keqv_list |
| 40 |
+except ImportError: # Python 2 |
| 41 |
+ from urllib2 import parse_http_list, parse_keqv_list |
| 42 |
+ |
| 43 |
+ |
| 44 |
+def md5(x): |
| 45 |
+ return hashlib.md5(x).hexdigest() |
| 46 |
+ |
| 47 |
+def md5_str(x): |
| 48 |
+ return md5(x.encode('utf8')) |
| 49 |
+ |
| 50 |
+def sha256(x): |
| 51 |
+ return hashlib.sha256(x).hexdigest() |
| 52 |
+ |
| 53 |
+ |
| 54 |
+def reconstruct_uri(environ): |
| 55 |
+ """ |
| 56 |
+ Reconstruct the relative part of the request URI. I.e. if the requested URL |
| 57 |
+ is https://foo.bar/spam?eggs, ``reconstruct_uri`` returns ``'/spam?eggs'``. |
| 58 |
+ """ |
| 59 |
+ uri = environ.get('SCRIPT_NAME', '') + environ['PATH_INFO'] |
| 60 |
+ if environ.get('QUERY_STRING'): |
| 61 |
+ uri += '?' + environ['QUERY_STRING'] |
| 62 |
+ return uri |
| 63 |
+ |
| 64 |
+ |
| 65 |
+def make_www_authenticate_header(realm=None): |
| 66 |
+ return 'Digest realm="%s", nonce="%s"' % (realm, generate_nonce()) |
| 67 |
+ |
| 68 |
+def generate_nonce(): |
| 69 |
+ return sha256(os.urandom(1000) + str(time.time()).encode()) |
| 70 |
+ |
| 71 |
+ |
| 72 |
+def make_auth_response(nonce, HA1, HA2): |
| 73 |
+ """ response := md5(HA1 : nonce : HA2) """ |
| 74 |
+ return md5_str(HA1 + ':' + nonce + ':' + HA2) |
| 75 |
+ |
| 76 |
+def make_HA2(http_method, uri): |
| 77 |
+ """ HA2 := http_method : uri (as reconstructed by ``reconstruct_uri``) """ |
| 78 |
+ return md5_str(http_method + ':' + uri) |
| 79 |
+ |
| 80 |
+ |
| 81 |
+def parse_dict_header(value): |
| 82 |
+ """ |
| 83 |
+ Parses a HTTP dict header value -- i.e. ``"foo=bar, spam=eggs"`` is parsed |
| 84 |
+ into ``{'foo': 'bar', 'spam': 'eggs'}``. |
| 85 |
+ """ |
| 86 |
+ return parse_keqv_list(parse_http_list(value)) |
| 87 |
+ |
| 88 |
+ |
| 89 |
+class BaseHttpAuthMiddleware(object): |
| 90 |
+ """ |
| 91 |
+ Abstract HTTP Digest Auth middleware. Contains all the functionality |
| 92 |
+ except for credential validation -- this happens using the ``make_HA1`` |
| 93 |
+ method which needs to be overriden by subclasses. |
| 94 |
+ |
| 95 |
+ `wsgi_app` |
| 96 |
+ The WSGI app to be secured. |
| 97 |
+ `realm` |
| 98 |
+ The HTTP Auth realm to be displayed in the browser. |
| 99 |
+ `routes` |
| 100 |
+ (optional) A list of regular expressions that specify which URLs should |
| 101 |
+ be secured. If not given, all routes are secured by default. |
| 102 |
+ """ |
| 103 |
+ def __init__(self, wsgi_app, realm=None, routes=()): |
| 104 |
+ self.wsgi_app = wsgi_app |
| 105 |
+ self.realm = realm or '' |
| 106 |
+ self.routes = self.compile_routes(routes) |
| 107 |
+ |
| 108 |
+ def __call__(self, environ, start_response): |
| 109 |
+ environ['httpauth.uri'] = reconstruct_uri(environ) |
| 110 |
+ if (self.should_require_authentication(environ['httpauth.uri']) and |
| 111 |
+ not self.authenticate(environ)): |
| 112 |
+ # URL is secured and user hasn't sent authentication/wrong credentials. |
| 113 |
+ return self.challenge(environ, start_response) |
| 114 |
+ else: |
| 115 |
+ # Wave-through to real WSGI app. |
| 116 |
+ return self.wsgi_app(environ, start_response) |
| 117 |
+ |
| 118 |
+ def compile_routes(self, routes): |
| 119 |
+ return [re.compile(route) for route in routes] |
| 120 |
+ |
| 121 |
+ def should_require_authentication(self, url): |
| 122 |
+ """ Returns True if we should require authentication for the URL given """ |
| 123 |
+ return (not self.routes # require auth for all URLs |
| 124 |
+ or any(route.match(url) for route in self.routes)) |
| 125 |
+ |
| 126 |
+ def authenticate(self, environ): |
| 127 |
+ """ |
| 128 |
+ Returns True if the credentials passed in the Authorization header are |
| 129 |
+ valid, False otherwise. |
| 130 |
+ """ |
| 131 |
+ try: |
| 132 |
+ hd = parse_dict_header(environ['HTTP_AUTHORIZATION']) |
| 133 |
+ except (KeyError, ValueError): |
| 134 |
+ return False |
| 135 |
+ |
| 136 |
+ return self.credentials_valid( |
| 137 |
+ hd['response'], |
| 138 |
+ environ['REQUEST_METHOD'], |
| 139 |
+ environ['httpauth.uri'], |
| 140 |
+ hd['nonce'], |
| 141 |
+ hd['Digest username'], |
| 142 |
+ ) |
| 143 |
+ |
| 144 |
+ def credentials_valid(self, response, http_method, uri, nonce, user): |
| 145 |
+ try: |
| 146 |
+ HA1 = self.make_HA1(user) |
| 147 |
+ except KeyError: |
| 148 |
+ # Invalid user |
| 149 |
+ return False |
| 150 |
+ return response == make_auth_response(nonce, HA1, make_HA2(http_method, uri)) |
| 151 |
+ |
| 152 |
+ def challenge(self, environ, start_response): |
| 153 |
+ start_response( |
| 154 |
+ '401 Authentication Required', |
| 155 |
+ [('WWW-Authenticate', make_www_authenticate_header(self.realm))], |
| 156 |
+ ) |
| 157 |
+ html = '<h1>401 - Authentication Required</h1>' |
| 158 |
+ return [html if PY2 else html.encode()] |
| 159 |
+ |
| 160 |
+ |
| 161 |
+class DigestFileHttpAuthMiddleware(BaseHttpAuthMiddleware): |
| 162 |
+ """ |
| 163 |
+ Reads credentials from an Apache-style .htdigest file. |
| 164 |
+ |
| 165 |
+ `filelike` |
| 166 |
+ Any file-like object that has a ``.read()`` method. |
| 167 |
+ Note: Don't pass filenames, only open files/file-likes. |
| 168 |
+ """ |
| 169 |
+ def __init__(self, filelike, **kwargs): |
| 170 |
+ realm, self.user_HA1_map = self.parse_htdigest_file(filelike) |
| 171 |
+ BaseHttpAuthMiddleware.__init__(self, realm=realm, **kwargs) |
| 172 |
+ |
| 173 |
+ def make_HA1(self, username): |
| 174 |
+ return self.user_HA1_map[username] |
| 175 |
+ |
| 176 |
+ def parse_htdigest_file(self, filelike): |
| 177 |
+ """ |
| 178 |
+ .htdigest files consist of lines in the following format:: |
| 179 |
+ |
| 180 |
+ username:realm:passwordhash |
| 181 |
+ |
| 182 |
+ where both `username` and `realm` are plain-text without any colons |
| 183 |
+ and `passwordhash` is the result of ``md5(username : realm : password)`` |
| 184 |
+ and thus `passwordhash` == HA1. |
| 185 |
+ """ |
| 186 |
+ realm = None |
| 187 |
+ user_HA1_map = {} |
| 188 |
+ |
| 189 |
+ for lineno, line in enumerate(filter(None, filelike.read().splitlines()), 1): |
| 190 |
+ try: |
| 191 |
+ username, realm2, password_hash = line.split(':') |
| 192 |
+ except ValueError: |
| 193 |
+ raise ValueError("Line %d invalid: %r (username/password may not contain ':')" % (lineno, line)) |
| 194 |
+ if realm is not None and realm != realm2: |
| 195 |
+ raise ValueError("Line %d: realm may not vary (got %r and %r)" % (lineno, realm, realm2)) |
| 196 |
+ else: |
| 197 |
+ realm = realm2 |
| 198 |
+ user_HA1_map[username] = password_hash |
| 199 |
+ |
| 200 |
+ return realm, user_HA1_map |
| 201 |
+ |
| 202 |
+ |
| 203 |
+class DictHttpAuthMiddleware(BaseHttpAuthMiddleware): |
| 204 |
+ """ |
| 205 |
+ Reads credentials from ``user_password_map`` which is a |
| 206 |
+ `username -> plaintext password` map. |
| 207 |
+ """ |
| 208 |
+ def __init__(self, user_password_map, **kwargs): |
| 209 |
+ self.user_password_map = user_password_map |
| 210 |
+ BaseHttpAuthMiddleware.__init__(self, **kwargs) |
| 211 |
+ |
| 212 |
+ def make_HA1(self, username): |
| 213 |
+ password = self.user_password_map[username] |
| 214 |
+ return md5_str(username + ':' + self.realm + ':' + password) |
| 215 |
+ |
| 216 |
+ |
| 217 |
+class AlwaysFailingAuthMiddleware(BaseHttpAuthMiddleware): |
| 218 |
+ """ This thing just keeps asking for credentials all the time """ |
| 219 |
+ def authenticate(self, environ): |
| 220 |
+ return False |
| 221 |
Index: klaus.git/setup.py |
| 222 |
=================================================================== |
| 223 |
--- klaus.git.orig/setup.py |
| 224 |
+++ klaus.git/setup.py |
| 225 |
@@ -21,7 +21,6 @@ requires = [ |
| 226 |
"flask", |
| 227 |
"Werkzeug>=0.15.0", |
| 228 |
"pygments", |
| 229 |
- "httpauth", |
| 230 |
"humanize", |
| 231 |
'dulwich>=0.19.3;python_version>="3.5"', |
| 232 |
'dulwich>=0.19.3,<0.20;python_version<"3.5"', |