Beispiel #1
0
    def _on_import_key(self):
        key_file, _ = QFileDialog.getOpenFileName(
            parent=self,
            caption=translate("ACTION_IMPORT_KEY"),
            filter=translate("IMPORT_KEY_FILTERS"),
            initialFilter=translate("IMPORT_KEY_INITIAL_FILTER"),
        )
        if not key_file:
            return
        new_device = load_device_file(Path(key_file))
        if new_device is None:
            show_error(self, translate("TEXT_INVALID_DEVICE_KEY"))
            return
        rep = ask_question(
            parent=self,
            title=translate("ASK_IMPORT_KEY"),
            message=translate("TEXT_IMPORT_KEY_CONFIRM_organization-user-device").format(
                organization=new_device.organization_id,
                user=new_device.short_user_display,
                device=new_device.device_label,
            ),
            button_texts=(translate("ACTION_IMPORT_YES"), translate("ACTION_IMPORT_NO")),
        )
        if rep == translate("ACTION_IMPORT_YES"):
            key_name = new_device.slughash + ".keys"
            dest = get_devices_dir(self.config.config_dir).joinpath(key_name)
            if self._overwrite_key(dest):

                shutil.copyfile(
                    new_device.key_file_path,
                    os.path.join(get_devices_dir(self.config.config_dir), key_name),
                )
                self.reload_devices()
                self.key_imported.emit()
def test_list_devices_support_key_file(config_dir, type):
    if type == "password":
        data_extra = {"type": "password", "salt": b"12345"}
        available_device_extra = {"type": DeviceFileType.PASSWORD}

    elif type == "smartcard":
        data_extra = {
            "type": "smartcard",
            "encrypted_key": b"12345",
            "certificate_id": "42",
            "certificate_sha1": b"12345",
        }
        available_device_extra = {"type": DeviceFileType.SMARTCARD}

    # Device information
    user_id = uuid4().hex
    device_name = uuid4().hex
    organization_id = "Org"
    rvk_hash = (uuid4().hex)[:10]
    device_id = f"{user_id}@{device_name}"
    slug = f"{rvk_hash}#{organization_id}#{device_id}"
    human_label = "Billy Mc BillFace"
    human_email = "*****@*****.**"
    device_label = "My device"

    # Craft file data
    key_file_data = packb({
        "ciphertext":
        b"whatever",
        "human_handle": (human_email.encode(), human_label.encode()),
        "device_label":
        device_label,
        "device_id":
        device_id,
        "organization_id":
        organization_id,
        "slug":
        slug,
        **data_extra,
    })

    key_file_path = get_devices_dir(config_dir) / "device.keys"
    key_file_path.parent.mkdir(parents=True)
    key_file_path.write_bytes(key_file_data)

    devices = list_available_devices(config_dir)
    expected_device = AvailableDevice(
        key_file_path=key_file_path,
        organization_id=OrganizationID(organization_id),
        device_id=DeviceID(device_id),
        human_handle=HumanHandle(human_email, human_label),
        device_label=DeviceLabel(device_label),
        slug=slug,
        **available_device_extra,
    )
    assert devices == [expected_device]
    assert get_key_file(config_dir, expected_device) == key_file_path
def test_list_devices_support_legacy_file_with_meaningful_name(config_dir):
    # Legacy path might exceed the 256 characters limit in some cases (see issue #1356)
    # So we use the `\\?\` workaround: https://stackoverflow.com/a/57502760/2846140
    if sys.platform == "win32":
        config_dir = Path("\\\\?\\" + str(config_dir.resolve()))

    # Device information
    user_id = uuid4().hex
    device_name = uuid4().hex
    organization_id = "Org"
    rvk_hash = (uuid4().hex)[:10]
    device_id = f"{user_id}@{device_name}"
    slug = f"{rvk_hash}#{organization_id}#{device_id}"
    human_label = "Billy Mc BillFace"
    human_email = "*****@*****.**"
    device_label = "My device"

    # Craft file data without the user_id, organization_id and slug fields
    key_file_data = packb({
        "type":
        "password",
        "salt":
        b"12345",
        "ciphertext":
        b"whatever",
        "human_handle": (human_email.encode(), human_label.encode()),
        "device_label":
        device_label,
    })

    key_file_path = get_devices_dir(config_dir) / slug / f"{slug}.keys"
    key_file_path.parent.mkdir(parents=True)
    key_file_path.write_bytes(key_file_data)

    devices = list_available_devices(config_dir)
    expected_device = AvailableDevice(
        key_file_path=key_file_path,
        organization_id=OrganizationID(organization_id),
        device_id=DeviceID(device_id),
        human_handle=HumanHandle(human_email, human_label),
        device_label=DeviceLabel(device_label),
        slug=slug,
        type=DeviceFileType.PASSWORD,
    )
    assert devices == [expected_device]
    assert get_key_file(config_dir, expected_device) == key_file_path
def test_list_devices_support_legacy_file_without_labels(config_dir):
    # Craft file data without the labels fields
    key_file_data = packb({
        "type": "password",
        "salt": b"12345",
        "ciphertext": b"whatever"
    })
    slug = "9d84fbd57a#Org#Zack@PC1"
    key_file_path = get_devices_dir(config_dir) / slug / f"{slug}.keys"
    key_file_path.parent.mkdir(parents=True)
    key_file_path.write_bytes(key_file_data)

    devices = list_available_devices(config_dir)
    expected_device = AvailableDevice(
        key_file_path=key_file_path,
        organization_id=OrganizationID("Org"),
        device_id=DeviceID("Zack@PC1"),
        human_handle=None,
        device_label=None,
    )
    assert devices == [expected_device]