Admin sends a messagae on account creation

This commit is contained in:
Chris Proctor
2024-05-20 10:02:16 -04:00
parent 0ff6bb784a
commit 7d988186a0
4 changed files with 27 additions and 6 deletions

View File

@@ -7,16 +7,32 @@ import sys
sys.path.insert(0, "..")
from encryption import PrivateKey, PublicKey
def get_or_create_admin():
if not User.objects.filter(name='subrosa_admin').exists():
private_key = PrivateKey.generate()
public_key = private_key.get_public_key()
admin = User(name='subrosa_admin', public_key=str(public_key))
admin.save()
return User.objects.get(name='subrosa_admin')
@route_post("users/new", args={'name': str, 'public_key': str})
def create_user(params):
"Creates a new user"
admin = get_or_create_admin()
try:
PublicKey.load(params['public_key'])
public_key = PublicKey.load(params['public_key'])
except (ValueError, FileNotFoundError):
raise NotAllowed("Invalid public key")
try:
new_user = User.from_dict(params)
new_user.save()
welcome = "Welcome to SubRosa! Please be a good community member."
message = Message(
sender=admin,
recipient=new_user,
ciphertext=public_key.encrypt(welcome)
)
message.save()
return new_user.to_dict()
except:
raise NotAllowed(f"Username {params['name']} is already in use.")