Ejemplo n.º 1
0
def authenticate(username, password):
    """attempt to authenticate user using provided credentials"""

    api = PyiCloudService(username, password)

    if api.requires_2fa:
        print("Two-factor authentication required.")
        code = input(
            "Enter the code you received of one of your approved devices: ")
        result = api.validate_2fa_code(code)
        print("Code validation result: %s" % result)

        if not result:
            print("Failed to verify security code")
            sys.exit(1)

        if not api.is_trusted_session:
            print("Session is not trusted. Requesting trust...")
            result = api.trust_session()
            print("Session trust result %s" % result)

            if not result:
                print(
                    "Failed to request trust. You will likely be prompted for the code again in the coming weeks"
                )
    elif api.requires_2sa:
        import click
        print("Two-step authentication required. Your trusted devices are:")

        devices = api.trusted_devices
        for i, device in enumerate(devices):
            print("  %s: %s" %
                  (i,
                   device.get('deviceName',
                              "SMS to %s" % device.get('phoneNumber'))))

        device = click.prompt('Which device would you like to use?', default=0)
        device = devices[device]
        if not api.send_verification_code(device):
            print("Failed to send verification code")
            sys.exit(1)

        code = click.prompt('Please enter validation code')
        if not api.validate_verification_code(device, code):
            print("Failed to verify verification code")
            sys.exit(1)

    return api
Ejemplo n.º 2
0
    def login(self, username, password):
        api = PyiCloudService(username, password)
        if api.requires_2fa:
            logging.info(
                f"ICloud with mail {username} required Two-factor authentication!"
            )
            result = api.validate_2fa_code(self.__get_2fa_code())

            if not result:
                logging.critical(
                    f"Failed to verify security code of ICloud-Account with mail {username}"
                )
                self.connected = False

            if not api.is_trusted_session:
                logging.info(
                    f"Session for mail, {username} is not trusted. Requesting trust..."
                )
                result = api.trust_session()
                print(f"Session trust for mail {username} result '{result}'")

                if not result:
                    logging.critical(
                        "Failed to request trust. You will likely be prompted for the code again in the coming weeks!"
                    )
                    self.connected = False
        elif api.requires_2sa:
            import click
            devices = api.trusted_devices
            for i, device in enumerate(devices):
                print("  %s: %s" %
                      (i,
                       device.get('deviceName',
                                  "SMS to %s" % device.get('phoneNumber'))))

            device = click.prompt('Which device would you like to use?',
                                  default=0)
            device = devices[device]
            if not api.send_verification_code(device):
                logging.critical("Failed to send verification code")
            code = click.prompt('Please enter validation code')

            if not api.validate_verification_code(device, code):
                logging.critical("Failed to verify verification code!")
                self.connected = False

        return api
Ejemplo n.º 3
0
class iCloud:
    def __init__(self):
        creds = self.__load_creds(os.getcwd() + "/Secrets.txt")
        self.api = PyiCloudService(creds[0], creds[1])
        self.__two_factor_routine()
        self.phone = self.Phone()
        self.update_phone()

        self.lats, self.longs = self.__load_location()

    def __load_location(self):
        with open("location.txt", "r") as f:
            temp = f.readlines()
        out = []
        for i in temp:
            i = i.strip()
            i = i.split(",")
            out.append([float(j) for j in i])
        return out[0], out[1]

    def update_phone(self):
        _phone = self.api.devices[1]
        status = _phone.status()
        self.phone.update(_phone.content, status)

    def __load_creds(self, path):
        with open(path, "r") as f:
            creds = f.readlines()
        return [i.strip() for i in creds]

    # Not my code. This was ripped from pycloud docs.
    def __two_factor_routine(self):
        if self.api.requires_2fa:
            print("Two-factor authentication required.")
            code = input(
                "Enter the code you received of one of your approved devices: "
            )
            result = self.api.validate_2fa_code(code)
            print("Code validation result: %s" % result)

            if not result:
                print("Failed to verify security code")
                sys.exit(1)

            if not self.api.is_trusted_session:
                print("Session is not trusted. Requesting trust...")
                result = self.api.trust_session()
                print("Session trust result %s" % result)

                if not result:
                    print(
                        "Failed to request trust. You will likely be prompted for the code again in the coming weeks"
                    )
        elif self.api.requires_2sa:
            import click
            print(
                "Two-step authentication required. Your trusted devices are:")

            devices = self.api.trusted_devices
            for i, device in enumerate(devices):
                print("  %s: %s" %
                      (i,
                       device.get('deviceName',
                                  "SMS to %s" % device.get('phoneNumber'))))

            device = click.prompt('Which device would you like to use?',
                                  default=0)
            device = devices[device]
            if not self.api.send_verification_code(device):
                print("Failed to send verification code")
                sys.exit(1)

            code = click.prompt('Please enter validation code')
            if not self.api.validate_verification_code(device, code):
                print("Failed to verify verification code")
                sys.exit(1)

    def is_charging(self):
        first = self.api.devices[1].status()["batteryLevel"]
        count = 0

        while True:
            time.sleep(20)
            second = self.api.devices[1].status()["batteryLevel"]
            print("First {} --> Second {}".format(first, second))
            if second != first:
                break
            count += 1
        if second - first > 0.0:
            return True
        else:
            return False

    def is_home(self):
        lat_fence = self.lats
        long_fence = self.longs

        lat_check = self.phone.location[0] > lat_fence[
            0] and self.phone.location[0] < lat_fence[1]
        long_check = self.phone.location[1] < long_fence[
            0] and self.phone.location[1] > long_fence[1]
        return lat_check and long_check

    class Phone:
        def __init__(self):
            self.battery = 0.0
            self.location = []
            self.id = ""
            self.timestamp = None

        def update(self, content, status):
            self.timestamp = datetime.datetime.now()
            self.battery = status["batteryLevel"]
            self.id = content["id"]
            lat = content["location"]["latitude"]
            long = content["location"]["longitude"]
            self.location = [lat, long]
Ejemplo n.º 4
0
api = PyiCloudService(appleid_email, appleid_password)

if api.requires_2fa:
    print("Two-factor authentication required.")
    code = input(
        "Enter the code you received of one of your approved devices: ")
    result = api.validate_2fa_code(code)
    print("Code validation result: %s" % result)

    if not result:
        print("Failed to verify security code")
        sys.exit(1)

    if not api.is_trusted_session:
        print("Session is not trusted. Requesting trust...")
        result = api.trust_session()
        print("Session trust result %s" % result)

        if not result:
            print(
                "Failed to request trust. You will likely be prompted for the code again in the coming weeks"
            )
elif api.requires_2sa:
    import click
    print("Two-step authentication required. Your trusted devices are:")

    devices = api.trusted_devices
    for i, device in enumerate(devices):
        print("  %s: %s" % (i,
                            device.get('deviceName', "SMS to %s" %
                                       device.get('phoneNumber'))))