~sircmpwn/hiprompt-gtk-py

4cdf013fd29aa866cd7fbcd2b77a18dfb18b0ae0 — Martijn Braam 2 years ago bf8f5c4
Implement unlocking with key disclosure
1 files changed, 79 insertions(+), 6 deletions(-)

M hiprompt_gtk/window.py
M hiprompt_gtk/window.py => hiprompt_gtk/window.py +79 -6
@@ 39,6 39,16 @@ class Window:
        self.session.status = 1
        Gtk.main_quit()

    def on_unlock_grant(self, *args):
        password = self.entry.get_text()
        self.entry.set_sensitive(False)
        if self.session.password(password):
            self.session.status = 0
            Gtk.main_quit()
        else:
            self.entry.set_text("")
            self.entry.set_sensitive(True)

    def make_unlock_ui(self):
        for child in self.box:
            self.box.remove(child)


@@ 53,6 63,7 @@ class Window:

        grant = Gtk.Button("Grant access")
        grant.get_style_context().add_class("destructive-action")
        grant.connect("clicked", self.on_unlock_grant)
        self.header.pack_end(grant)

        scroller = Gtk.ScrolledWindow()


@@ 61,14 72,76 @@ class Window:
        scroller.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.NEVER)
        self.box.add(scroller)

        box = Gtk.Box(Gtk.Orientation.VERTICAL, 5)
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5)
        box.get_style_context().add_class("mainbox")
        scroller.add(box)

        label = Gtk.Label("An application has requested to unlock "
                          "your keyring. \n"
                          "If you consent, the application will have access to "
                          "the following information:")
        box.add(label)
        if len(self.session.keys) == 0:
            label = Gtk.Label("An application has requested to unlock "
                              "your keyring.")
            box.add(label)
        else:
            label = Gtk.Label("An application has requested to unlock "
                              "your keyring.\n"
                              "If you consent, the application will have access to "
                              "the following information:")
            box.add(label)
            box.add(self.make_key_box())

        listbox = Gtk.ListBox()
        listbox.get_style_context().add_class("content")
        listbox.set_selection_mode(Gtk.SelectionMode.NONE)
        box.add(listbox)

        row = Gtk.Box(spacing=8)
        listbox.insert(row, -1)

        rowlabel = Gtk.Label("Password")
        row.pack_start(rowlabel, False, False, 0)

        self.entry = Gtk.Entry()
        row.pack_end(self.entry, True, True, 0)
        self.entry.grab_focus()
        self.entry.set_input_purpose(Gtk.InputPurpose.PASSWORD)
        self.entry.set_visibility(False)
        self.entry.connect('activate', self.on_unlock_grant)

        self.window.show_all()

    def make_key_box(self):
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=8)

        for key in self.session.keys:
            listbox = Gtk.ListBox()
            listbox.get_style_context().add_class("content")
            listbox.set_selection_mode(Gtk.SelectionMode.NONE)
            box.add(listbox)

            for pair in key:
                row = Gtk.Box(spacing=6)
                listbox.insert(row, -1)

                name = Gtk.Label(pair.key, xalign=0.0)
                name.get_style_context().add_class("heading")
                row.pack_start(name, True, True, 0)

                if pair.value is not None and not pair.private:
                    value = Gtk.Label(pair.value, xalign=1.0)
                    value.get_style_context().add_class("value")
                    row.pack_start(value, True, True, 0)
                elif pair.private:
                    value = Gtk.Label("(not shown)", xalign=1.0)
                    context = value.get_style_context()
                    context.add_class("value")
                    context.add_class("secret")
                    context.add_class("dim-label")
                    row.pack_start(value, True, True, 0)
                else:
                    value = Gtk.Label("(empty)", xalign=1.0)
                    context = value.get_style_context()
                    context.add_class("value")
                    context.add_class("empty")
                    context.add_class("dim-label")
                    row.pack_start(value, True, True, 0)

        return box