aboutsummaryrefslogtreecommitdiff
path: root/password.py
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-11-28 19:34:35 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2022-11-28 19:34:35 +0100
commitd8a7a022df815d8254c4a9674ef1143f50caa277 (patch)
treedc0e6d2dea1b821bca9c1c15bd7a6413b80b8791 /password.py
parentReplace manual cookies with flask session. (diff)
downloadmu4web-d8a7a022df815d8254c4a9674ef1143f50caa277.tar.gz
mu4web-d8a7a022df815d8254c4a9674ef1143f50caa277.tar.xz
Documentation.
Diffstat (limited to '')
-rwxr-xr-xpassword.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/password.py b/password.py
index 16192be..ff0df21 100755
--- a/password.py
+++ b/password.py
@@ -1,4 +1,11 @@
#!/usr/bin/env python3
+
+"""
+Simple password store, backed by a JSON file.
+
+Also contains an entry point for managing the store.
+"""
+
import hashlib
import json
import os
@@ -28,6 +35,12 @@ class PasswordEntry(TypedDict):
class Passwords:
+ """
+ Simple password store.
+
+ [Parameters]
+ fname - Path of json file to load and store from.
+ """
def __init__(self, fname: os.PathLike):
self.fname = fname
self.db: dict[str, PasswordEntry]
@@ -38,6 +51,7 @@ class Passwords:
self.db = {}
def save(self) -> None:
+ """Dump current data to disk."""
try:
with open(os.fspath(self.fname) + '.tmp', 'w') as f:
json.dump(self.db, f)
@@ -47,6 +61,7 @@ class Passwords:
print(f'Saving password failed {e}')
def add(self, username: str, password: str) -> None:
+ """Add (or modify) entry in store."""
if cur := self.db.get(username):
salt = cur['salt']
hashed = hashlib.sha256((salt + password).encode('UTF-8'))
@@ -65,6 +80,7 @@ class Passwords:
}
def validate(self, username: str, password: str) -> bool:
+ """Check if user exists, and if it has a correct password."""
# These shall fail when key is missing
data = self.db[username]
proc = hash_methods[data['method']]
@@ -72,8 +88,7 @@ class Passwords:
return data['hash'] == digest
-if __name__ == '__main__':
-
+def main():
import argparse
parser = argparse.ArgumentParser()
@@ -99,3 +114,7 @@ if __name__ == '__main__':
print(passwords.validate(args.username, args.password))
else:
parser.print_help()
+
+
+if __name__ == '__main__':
+ main()