authentik.recovery.management.commands.create_recovery_key
authentik recovery createkey command
1"""authentik recovery createkey command""" 2 3from datetime import timedelta 4from getpass import getuser 5 6from django.utils.timesince import timesince 7from django.utils.timezone import now 8from django.utils.translation import gettext as _ 9 10from authentik.core.models import User 11from authentik.recovery.lib import create_recovery_token 12from authentik.tenants.management import TenantCommand 13 14 15class Command(TenantCommand): 16 """Create Token used to recover access""" 17 18 help = _("Create a Key which can be used to restore access to authentik.") 19 20 def format_duration_message(self, duration: int) -> str: 21 """Format duration in minutes to a human-readable message""" 22 current_time = now() 23 future_time = current_time + timedelta(minutes=duration) 24 25 # fyi a non-breaking space is returned by timesince 26 return timesince(current_time, future_time) 27 28 def add_arguments(self, parser): 29 parser.add_argument( 30 "duration", 31 nargs="?", 32 default=60, 33 type=int, 34 help="How long the token is valid for (in minutes). Default: 60 minutes (1 hour).", 35 ) 36 parser.add_argument("user", action="store", help="Which user the Token gives access to.") 37 38 def handle_per_tenant(self, *args, **options): 39 """Create Token used to recover access""" 40 duration = int(options.get("duration", 60)) 41 expiry = now() + timedelta(minutes=duration) 42 user = User.objects.filter(username=options.get("user")).first() 43 if not user: 44 self.stderr.write(f"User '{options.get('user')}' not found.") 45 return 46 _, url = create_recovery_token(user, expiry, getuser()) 47 48 duration_msg = self.format_duration_message(duration) 49 50 self.stdout.write( 51 f"Store this link safely, as it will allow anyone to access authentik as {user}." 52 ) 53 self.stdout.write(f"This recovery token is valid for {duration_msg}.") 54 self.stdout.write(url)
16class Command(TenantCommand): 17 """Create Token used to recover access""" 18 19 help = _("Create a Key which can be used to restore access to authentik.") 20 21 def format_duration_message(self, duration: int) -> str: 22 """Format duration in minutes to a human-readable message""" 23 current_time = now() 24 future_time = current_time + timedelta(minutes=duration) 25 26 # fyi a non-breaking space is returned by timesince 27 return timesince(current_time, future_time) 28 29 def add_arguments(self, parser): 30 parser.add_argument( 31 "duration", 32 nargs="?", 33 default=60, 34 type=int, 35 help="How long the token is valid for (in minutes). Default: 60 minutes (1 hour).", 36 ) 37 parser.add_argument("user", action="store", help="Which user the Token gives access to.") 38 39 def handle_per_tenant(self, *args, **options): 40 """Create Token used to recover access""" 41 duration = int(options.get("duration", 60)) 42 expiry = now() + timedelta(minutes=duration) 43 user = User.objects.filter(username=options.get("user")).first() 44 if not user: 45 self.stderr.write(f"User '{options.get('user')}' not found.") 46 return 47 _, url = create_recovery_token(user, expiry, getuser()) 48 49 duration_msg = self.format_duration_message(duration) 50 51 self.stdout.write( 52 f"Store this link safely, as it will allow anyone to access authentik as {user}." 53 ) 54 self.stdout.write(f"This recovery token is valid for {duration_msg}.") 55 self.stdout.write(url)
Create Token used to recover access
def
format_duration_message(self, duration: int) -> str:
21 def format_duration_message(self, duration: int) -> str: 22 """Format duration in minutes to a human-readable message""" 23 current_time = now() 24 future_time = current_time + timedelta(minutes=duration) 25 26 # fyi a non-breaking space is returned by timesince 27 return timesince(current_time, future_time)
Format duration in minutes to a human-readable message
def
add_arguments(self, parser):
29 def add_arguments(self, parser): 30 parser.add_argument( 31 "duration", 32 nargs="?", 33 default=60, 34 type=int, 35 help="How long the token is valid for (in minutes). Default: 60 minutes (1 hour).", 36 ) 37 parser.add_argument("user", action="store", help="Which user the Token gives access to.")
Entry point for subclassed commands to add custom arguments.
def
handle_per_tenant(self, *args, **options):
39 def handle_per_tenant(self, *args, **options): 40 """Create Token used to recover access""" 41 duration = int(options.get("duration", 60)) 42 expiry = now() + timedelta(minutes=duration) 43 user = User.objects.filter(username=options.get("user")).first() 44 if not user: 45 self.stderr.write(f"User '{options.get('user')}' not found.") 46 return 47 _, url = create_recovery_token(user, expiry, getuser()) 48 49 duration_msg = self.format_duration_message(duration) 50 51 self.stdout.write( 52 f"Store this link safely, as it will allow anyone to access authentik as {user}." 53 ) 54 self.stdout.write(f"This recovery token is valid for {duration_msg}.") 55 self.stdout.write(url)
Create Token used to recover access