def pty_based_auth(auth_backend, pty): """ Show a username/password login prompt. Return username when authentication succeeded. """ tries = 0 while True: # Password authentication required for this session? sys.stdout.write('\033[2J\033[0;0H') # Clear screen sys.stdout.write(colored('Please authenticate\r\n\r\n', 'cyan')) if tries: sys.stdout.write(colored(' Authentication failed, try again\r\n', 'red')) try: console = Console(pty) username = console.input('Username', False) password = console.input('Password', True) except NoInput: raise NotAuthenticated if auth_backend.authenticate(username, password): sys.stdout.write(colored(' ' * 40 + 'Authentication successful\r\n\r\n', 'green')) return username else: tries += 1 if tries == 3: raise NotAuthenticated
def pty_based_auth(auth_backend, pty): """ Show a username/password login prompt. Return username when authentication succeeded. """ tries = 0 while True: # Password authentication required for this session? sys.stdout.write('\033[2J\033[0;0H') # Clear screen sys.stdout.write(colored('Please authenticate\r\n\r\n', 'cyan')) if tries: sys.stdout.write( colored(' Authentication failed, try again\r\n', 'red')) try: console = Console(pty) username = console.input('Username', False) password = console.input('Password', True) except NoInput: raise NotAuthenticated if auth_backend.authenticate(username, password): sys.stdout.write( colored(' ' * 40 + 'Authentication successful\r\n\r\n', 'green')) return username else: tries += 1 if tries == 3: raise NotAuthenticated