authentik.admin.management.commands.changepassword

Change a user's password

 1"""Change a user's password"""
 2
 3from getpass import getpass
 4
 5from django.core.management.base import BaseCommand, CommandError
 6from rest_framework.exceptions import ValidationError
 7
 8from authentik.core.models import User
 9from authentik.stages.password.models import PasswordDevice
10
11
12class Command(BaseCommand):
13    """Change a user's password, replacing Django's command that writes to the user"""
14
15    help = "Change a user's password."
16
17    def add_arguments(self, parser):
18        parser.add_argument("username")
19
20    def handle(self, *args, username: str, **options):
21        user = User.objects.filter(username=username).first()
22        if not user:
23            raise CommandError(f"User '{username}' does not exist")
24        password = getpass("Password: ")
25        if password != getpass("Password (again): "):
26            raise CommandError("Passwords do not match")
27        if not password:
28            raise CommandError("Password cannot be empty")
29        try:
30            PasswordDevice.set_password(user, password)
31        except ValidationError as exc:
32            # An LDAP or Kerberos source refused the password
33            raise CommandError(str(exc)) from exc
34        self.stdout.write(f"Password changed for user '{username}'")
class Command(django.core.management.base.BaseCommand):
13class Command(BaseCommand):
14    """Change a user's password, replacing Django's command that writes to the user"""
15
16    help = "Change a user's password."
17
18    def add_arguments(self, parser):
19        parser.add_argument("username")
20
21    def handle(self, *args, username: str, **options):
22        user = User.objects.filter(username=username).first()
23        if not user:
24            raise CommandError(f"User '{username}' does not exist")
25        password = getpass("Password: ")
26        if password != getpass("Password (again): "):
27            raise CommandError("Passwords do not match")
28        if not password:
29            raise CommandError("Password cannot be empty")
30        try:
31            PasswordDevice.set_password(user, password)
32        except ValidationError as exc:
33            # An LDAP or Kerberos source refused the password
34            raise CommandError(str(exc)) from exc
35        self.stdout.write(f"Password changed for user '{username}'")

Change a user's password, replacing Django's command that writes to the user

help = "Change a user's password."
def add_arguments(self, parser):
18    def add_arguments(self, parser):
19        parser.add_argument("username")

Entry point for subclassed commands to add custom arguments.

def handle(self, *args, username: str, **options):
21    def handle(self, *args, username: str, **options):
22        user = User.objects.filter(username=username).first()
23        if not user:
24            raise CommandError(f"User '{username}' does not exist")
25        password = getpass("Password: ")
26        if password != getpass("Password (again): "):
27            raise CommandError("Passwords do not match")
28        if not password:
29            raise CommandError("Password cannot be empty")
30        try:
31            PasswordDevice.set_password(user, password)
32        except ValidationError as exc:
33            # An LDAP or Kerberos source refused the password
34            raise CommandError(str(exc)) from exc
35        self.stdout.write(f"Password changed for user '{username}'")

The actual logic of the command. Subclasses must implement this method.