improve some account management commands

This commit is contained in:
wh1te909 2022-12-30 21:15:32 +00:00
parent 3c7c2dc1a5
commit 9192fa0fe2
4 changed files with 22 additions and 21 deletions

View File

@ -1,11 +1,10 @@
import os
import subprocess
from contextlib import suppress
import pyotp
from django.core.management.base import BaseCommand
from accounts.models import User
from tacticalrmm.helpers import get_webdomain
class Command(BaseCommand):
@ -22,26 +21,13 @@ class Command(BaseCommand):
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):
with suppress(Exception):
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())
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)
url = pyotp.totp.TOTP(code).provisioning_uri(
username, issuer_name=get_webdomain()
)
subprocess.run(f'qr "{url}"', shell=True)
self.stdout.write(
self.style.WARNING("Scan the barcode above with your authenticator app")

View File

@ -1,3 +1,5 @@
from getpass import getpass
from django.core.management.base import BaseCommand
from accounts.models import User
@ -17,7 +19,13 @@ class Command(BaseCommand):
self.stdout.write(self.style.ERROR(f"User {username} doesn't exist"))
return
passwd = input("Enter new password: ")
user.set_password(passwd)
pass1, pass2 = "foo", "bar"
while pass1 != pass2:
pass1 = getpass()
pass2 = getpass(prompt="Confirm Password:")
if pass1 != pass2:
self.stdout.write(self.style.ERROR("Passwords don't match"))
user.set_password(pass1)
user.save()
self.stdout.write(self.style.SUCCESS(f"Password for {username} was reset!"))

View File

@ -3,6 +3,8 @@ from urllib.parse import urlparse
from django.conf import settings
from django.core.management.base import BaseCommand
from tacticalrmm.helpers import get_webdomain
class Command(BaseCommand):
help = "Get config vars to be used in shell scripts"
@ -25,7 +27,7 @@ class Command(BaseCommand):
case "frontend":
self.stdout.write(settings.CORS_ORIGIN_WHITELIST[0])
case "webdomain":
self.stdout.write(urlparse(settings.CORS_ORIGIN_WHITELIST[0]).netloc)
self.stdout.write(get_webdomain())
case "djangoadmin":
url = f"https://{settings.ALLOWED_HOSTS[0]}/{settings.ADMIN_URL}"
self.stdout.write(url)

View File

@ -1,4 +1,5 @@
from typing import TYPE_CHECKING
from urllib.parse import urlparse
import pytz
from django.conf import settings
@ -46,3 +47,7 @@ def date_is_in_past(*, datetime_obj: "datetime", agent_tz: str) -> bool:
localized = agent_pytz.localize(datetime_obj)
utc_time = localized.astimezone(pytz.utc)
return now > utc_time
def get_webdomain() -> str:
return urlparse(settings.CORS_ORIGIN_WHITELIST[0]).netloc