datatracker/ietf/ietfauth/htpasswd.py
Ole Laursen dedb00b0a5 Control the tests of adding user passwords to the htpasswd file better
fixing a bug in the tests and add separate tests of the Python-based
approach and the htpasswd binary
 - Legacy-Id: 11170
2016-05-06 12:11:20 +00:00

25 lines
860 B
Python

import subprocess, hashlib
from django.conf import settings
def update_htpasswd_file(username, password):
if getattr(settings, 'USE_PYTHON_HTDIGEST', None):
pass_file = settings.HTPASSWD_FILE
realm = settings.HTDIGEST_REALM
prefix = '%s:%s:' % (username, realm)
key = hashlib.md5(prefix + password).hexdigest()
f = open(pass_file, 'r+')
pos = f.tell()
line = f.readline()
while line:
if line.startswith(prefix):
break
pos=f.tell()
line = f.readline()
f.seek(pos)
f.write('%s%s\n' % (prefix, key))
f.close()
else:
p = subprocess.Popen([settings.HTPASSWD_COMMAND, "-b", settings.HTPASSWD_FILE, username, password], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()