diff options
| author | LV-426 <lv-426@taproot.org.il> | 2014-12-02 02:08:56 +0200 |
|---|---|---|
| committer | LV-426 <lv-426@taproot.org.il> | 2014-12-02 02:33:05 +0200 |
| commit | 7a5e94b48165f76fd86a82fc90a466cb683ccc94 (patch) | |
| tree | 3dbbfa8bda0342c2f1ac996ccbb8852e261e030a | |
| parent | 23bb05b2f506743bf9c0008f8a7d9dd68bfa326e (diff) | |
crypt_util: add/validate logins
| -rw-r--r-- | src-py/crypt_util.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src-py/crypt_util.py b/src-py/crypt_util.py new file mode 100644 index 00000000..464fac94 --- /dev/null +++ b/src-py/crypt_util.py @@ -0,0 +1,46 @@ +import pickle +import hashlib, uuid +import os +import logging + +log = logging.getLogger('rhizi') + +def add_user_login(config, u, p): + htpasswd_path = config.htpasswd_path + + if False == os.path.exists(htpasswd_path): + with open(htpasswd_path, 'wb') as f: + pickle.dump({}, f) + + with open(htpasswd_path, 'rb') as f: + data = f.read() + pw_db = pickle.loads(data) + + with open(htpasswd_path, 'wb') as f: + salt = config.secret_key + pw_db[u] = hash_pw(str(p), salt) + pickle.dump(pw_db, f) + + log.info('htpasswd db: added entry: user: %s, pw: %s...' % (u, pw_db[u][:5])) + +def hash_pw(pw_str, salt_str): + salt = hashlib.sha512(salt_str).hexdigest() + ret = hashlib.sha512(pw_str + salt).hexdigest() + return ret + +def validate_login(config, u, p): + htpasswd_path = config.htpasswd_path + + salt = config.secret_key + + with open(htpasswd_path) as f: + pw_db = pickle.load(f) + + existing_pw_hash = pw_db.get(u) + if None == existing_pw_hash: + raise Exception('Not autorhized') + + if hash_pw(p, salt) != existing_pw_hash: + raise Exception('Not autorhized') + + |
