32 lines
No EOL
1 KiB
Python
32 lines
No EOL
1 KiB
Python
from django.core.management.base import BaseCommand, CommandError
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from totp.models import TOTPUser
|
|
from totp.otp import TOTP
|
|
|
|
from dbsettings.functions import getValue
|
|
|
|
import pyqrcode
|
|
import pyotp
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Enables TOTP for the specified user (identified by username)'
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument('user', type=str)
|
|
|
|
def handle(self, *args, **options):
|
|
try:
|
|
user = get_user_model().objects.get(username=options["user"])
|
|
except get_user_model().DoesNotExist:
|
|
raise ValueError(f"User {options['user']} does not exist")
|
|
|
|
if TOTP().active_for_user(user):
|
|
raise ValueError(f"TOTP already enabled for user {options['user']}")
|
|
|
|
totu = TOTPUser.objects.create(user=user)
|
|
|
|
uri = pyotp.totp.TOTP(totu.secret).provisioning_uri(options["user"], issuer_name=getValue("core.title", "Expephalon"))
|
|
|
|
print(pyqrcode.create(uri).terminal())
|
|
print(uri) |