From 83f9ee50dd9f64023e9b80fa8e12d650a2d5fdd1 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Thu, 25 Feb 2021 07:55:03 +0000 Subject: [PATCH] add management commands for resetting pw/2fa --- .../accounts/management/commands/reset_2fa.py | 55 +++++++++++++++++++ .../management/commands/reset_password.py | 22 ++++++++ 2 files changed, 77 insertions(+) create mode 100644 api/tacticalrmm/accounts/management/commands/reset_2fa.py create mode 100644 api/tacticalrmm/accounts/management/commands/reset_password.py diff --git a/api/tacticalrmm/accounts/management/commands/reset_2fa.py b/api/tacticalrmm/accounts/management/commands/reset_2fa.py new file mode 100644 index 00000000..2cd9271f --- /dev/null +++ b/api/tacticalrmm/accounts/management/commands/reset_2fa.py @@ -0,0 +1,55 @@ +import os +import pyotp +import subprocess +from django.core.management.base import BaseCommand +from accounts.models import User + + +class Command(BaseCommand): + help = "Reset 2fa" + + def add_arguments(self, parser): + parser.add_argument("username", type=str) + + def handle(self, *args, **kwargs): + username = kwargs["username"] + try: + user = User.objects.get(username=username) + except User.DoesNotExist: + self.stdout.write(self.style.ERROR(f"User {username} doesn't exist")) + return + + domain = "Tactical RMM" + nginx = "/etc/nginx/sites-available/frontend.conf" + found = None + if os.path.exists(nginx): + try: + with open(nginx, "r") as f: + for line in f: + if "server_name" in line: + found = line + break + + if found: + rep = found.replace("server_name", "").replace(";", "") + domain = "".join(rep.split()) + except: + pass + + code = pyotp.random_base32() + user.totp_key = code + user.save(update_fields=["totp_key"]) + + url = pyotp.totp.TOTP(code).provisioning_uri(username, issuer_name=domain) + subprocess.run(f'qr "{url}"', shell=True) + self.stdout.write( + self.style.WARNING("Scan the barcode above with your authenticator app") + ) + self.stdout.write( + self.style.WARNING( + f"If that doesn't work you may manually enter the setup key: {code}" + ) + ) + self.stdout.write( + self.style.SUCCESS(f"2fa was successfully reset for user {username}") + ) diff --git a/api/tacticalrmm/accounts/management/commands/reset_password.py b/api/tacticalrmm/accounts/management/commands/reset_password.py new file mode 100644 index 00000000..1cce94a1 --- /dev/null +++ b/api/tacticalrmm/accounts/management/commands/reset_password.py @@ -0,0 +1,22 @@ +from django.core.management.base import BaseCommand +from accounts.models import User + + +class Command(BaseCommand): + help = "Reset password for user" + + def add_arguments(self, parser): + parser.add_argument("username", type=str) + + def handle(self, *args, **kwargs): + username = kwargs["username"] + try: + user = User.objects.get(username=username) + except User.DoesNotExist: + self.stdout.write(self.style.ERROR(f"User {username} doesn't exist")) + return + + passwd = input("Enter new password: ") + user.set_password(passwd) + user.save() + self.stdout.write(self.style.SUCCESS(f"Password for {username} was reset!"))