def input_credentials_if_unauthorized(func):
    """Decorator. Handles AuthenticationException and request user
    to input a user and a password"""
    LOGIN_RETRIES = 3

    def wrapper(self, *args, **kwargs):
        try:
            # Set custom headers of mac_digest and username
            self.set_custom_headers(self.user)
            return func(self, *args, **kwargs)
        except ForbiddenException as e:
            # User valid but not enough permissions
            logger.debug("Forbidden: %s" % str(e))
            if self.user is None or self.rest_client.token is None:
                # token is None when you change user with user command
                # Anonymous is not enough, ask for a user
                self.user_io.out.info('Please log in to perform this action. If you don\'t have'
                                      ' an account sign up here: http://www.biicode.com')
                if self.user is None:
                    logger.debug("User None, ask for it, anonymous not enough!")
                return retry_with_new_token(self, *args, **kwargs)
            else:
                # If our user receives a ForbiddenException propagate it, not log with other user
                raise e
        except AuthenticationException:
            # Token expired or not valid, so clean the token and repeat the call
            # (will be anonymous call but registering who is calling)
            self._store_login((self.user, None))
            self.rest_client.token = None
            # Set custom headers of mac_digest and username
            self.set_custom_headers(self.user)
            return wrapper(self, *args, **kwargs)

    def retry_with_new_token(self, *args, **kwargs):
        """Try LOGIN_RETRIES to obtain a password from user input for which
        we can get a valid token from api_client. If a token is returned,
        credentials are stored in localdb and rest method is called"""
        for _ in range(LOGIN_RETRIES):
            user, password = self.user_io.request_login(self.user)
            token = None
            try:
                token = self.authenticate(user, password)
            except AuthenticationException:
                if self.user is None:
                    self.user_io.out.error('Wrong user or password')
                else:
                    self.user_io.out.error(self.user % 'Wrong password for user "%s"')
                    self.user_io.out.info('You can change username with "bii user <username>"')
            if token:
                self.rest_client.token = token
                self.user = user
                self._store_login((user, token))
                # Set custom headers of mac_digest and username
                self.set_custom_headers(user)
                return func(self, *args, **kwargs)

        raise AuthenticationException("Too many failed login attempts, bye!")
    return wrapper
dataset/ETHPy150Open biicode/client/api/biiapi_auth_manager.py/input_credentials_if_unauthorized/swapped operands of `%`