1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
|
# SPDX-License-Identifier: GPL-3.0-only
# pylint: disable=too-many-lines
from __future__ import annotations
import binascii
import logging
import typing
from datetime import UTC, datetime
from enum import Enum
from gettext import gettext as _
from typing import NamedTuple
from gi.repository import Gio, GLib, GObject, Gtk
from pyotp import TOTP, parse_uri
from gsecrets.attributes_model import AttributesModel
if typing.TYPE_CHECKING:
from uuid import UUID
from pykeepass.attachment import Attachment
from pykeepass.entry import Entry
from pykeepass.group import Group
from gsecrets.database_manager import (
DatabaseManager, # pylint: disable=ungrouped-imports
)
class EntryColor(Enum):
NONE = "NoneColorButton"
BLUE = "BlueColorButton"
GREEN = "GreenColorButton"
YELLOW = "YellowColorButton"
ORANGE = "OrangeColorButton"
RED = "RedColorButton"
PURPLE = "PurpleColorButton"
BROWN = "BrownColorButton"
def to_translatable(self): # pylint: disable=too-many-return-statements
match self:
case EntryColor.NONE:
return _("White")
case EntryColor.BLUE:
return _("Blue")
case EntryColor.GREEN:
return _("Green")
case EntryColor.YELLOW:
return _("Yellow")
case EntryColor.ORANGE:
return _("Orange")
case EntryColor.RED:
return _("Red")
case EntryColor.PURPLE:
return _("Purple")
case EntryColor.BROWN:
return _("Brown")
def css_class(self):
match self:
case EntryColor.NONE:
return "white"
case EntryColor.BLUE:
return "blue"
case EntryColor.GREEN:
return "green"
case EntryColor.YELLOW:
return "yellow"
case EntryColor.ORANGE:
return "orange"
case EntryColor.RED:
return "red"
case EntryColor.PURPLE:
return "purple"
case EntryColor.BROWN:
return "brown"
class SafeElement(GObject.Object):
selected = GObject.Property(type=bool, default=False)
sensitive = GObject.Property(type=bool, default=True)
sorted_handler_id: int | None = None
def __init__(self, db_manager: DatabaseManager, element: Entry | Group):
"""GObject to handle a safe element.
The underlying pykeepass element can be obtainied via the `element`
property, when it is certain the element is a Group or Entry, the
properties `entry` and `group` should be used instead.
"""
super().__init__()
self._element = element
self._db_manager = db_manager
self.is_group = isinstance(self, SafeGroup)
self.is_entry = isinstance(self, SafeEntry)
self._notes: str = element.notes or ""
if self.is_group:
self._name = element.name or ""
else:
self._name = element.title or ""
def __eq__(self, other): # pylint: disable=arguments-differ
if isinstance(other, SafeElement):
return self.uuid == other.uuid
return False
def __hash__(self): # pylint: disable=arguments-differ
return hash(self.uuid)
@GObject.Signal(flags=GObject.SignalFlags.ACTION)
def updated(self):
"""Update the entry.
Used to tell whenever there have been any changed that should be
reflected on the main list box or edit page.
"""
self._db_manager.is_dirty = True
self.touch(modify=True)
self.emit(self.updated)
logging.debug("Safe element updated")
def touch(self, modify: bool = False) -> None:
"""Update the last accessed time.
If modify is true it also updates the last modified time.
"""
self._element.touch(modify)
def delete(self) -> None:
"""Delete an Element from the database."""
element = self._element
parentgroup = self.parentgroup
self.delete_inner()
if self.is_entry:
self._db_manager.db.delete_entry(element)
else:
self._db_manager.db.delete_group(element)
parentgroup.updated()
def delete_inner(self) -> None:
"""Recursively removes items from our wrapper of the database."""
if self.is_entry:
found, pos = self._db_manager.entries.find(self)
if found:
self._db_manager.entries.remove(pos)
else:
while entry := self.entries.get_item(0): # pylint: disable=no-member
entry.delete_inner()
while group := self.subgroups.get_item(0): # pylint: disable=no-member
group.delete_inner()
found, pos = self._db_manager.groups.find(self)
if found:
self._db_manager.groups.remove(pos)
def trash(self) -> bool:
"""Thrash an Element from the database.
Return if the element was deleted instead of being sent to the trash
bin.
"""
element = self._element
parentgroup = self.parentgroup
if (trash_bin := self._db_manager.trash_bin) and (
self._db_manager.parent_checker(self, trash_bin) and not self.is_trash_bin
):
self.delete()
return True
if self.is_trash_bin:
self.delete()
self._db_manager.trash_bin = None
return True
trash_bin_missing = self._db_manager.trash_bin is None
if self.is_entry:
self._db_manager.db.trash_entry(element)
parentgroup.filter_changed(True)
else:
self._db_manager.db.trash_group(element)
parentgroup.filter_changed(False)
parentgroup.emit_children_changed(1, 0)
# We add the trash bin if it was not present already
if trash_bin_missing and (
trash_bin_inner := self._db_manager.db.recyclebin_group
):
trash_bin = SafeGroup(self._db_manager, trash_bin_inner)
self._db_manager.trash_bin = trash_bin
self._db_manager.groups.append(trash_bin)
trash_bin.parentgroup.updated()
trash_bin.parentgroup.emit_children_changed(0, 1)
if trash_bin := self._db_manager.trash_bin:
trash_bin.filter_changed(self.is_entry)
trash_bin.emit_children_changed(0, 1)
parentgroup.updated()
return False
def move_to(self, dest: SafeGroup) -> None:
old_location = self.parentgroup
if old_location == dest:
return
# We signal items changed on the main list store so that the changes are
# propagated to the filter models used by each group.
if self.is_entry:
self._db_manager.db.move_entry(self._element, dest.group)
dest.filter_changed(True)
old_location.filter_changed(True)
else:
self._db_manager.db.move_group(self._element, dest.group)
dest.filter_changed(False)
old_location.filter_changed(False)
old_location.emit_children_changed(1, 0)
dest.emit_children_changed(0, 1)
old_location.updated()
dest.updated()
@property
def element(self) -> Entry | Group:
return self._element
@GObject.Property(type=str, default="")
def name(self) -> str:
"""Get element title or name.
:returns: name or an empty string if there is none
:rtype: str
"""
return self._name
@name.setter # type: ignore
def name(self, new_name: str) -> None:
"""Set entry title.
:param str new_name: new title
"""
if new_name != self._name:
self._name = new_name
if self.is_group:
self._element.name = new_name
self._db_manager.emit("sorting_changed", False)
else:
self._element.title = new_name
self._db_manager.emit("sorting_changed", True)
if self.is_entry:
found, pos = self._db_manager.entries.find(self)
if found:
self._db_manager.entries.items_changed(pos, 1, 1)
else:
found, pos = self._db_manager.groups.find(self)
if found:
self._db_manager.groups.items_changed(pos, 1, 1)
self.updated()
@GObject.Property(type=str, default="")
def notes(self) -> str:
"""Get entry notes.
:returns: notes or an empty string if there is none
:rtype: str
"""
return self._notes
@notes.setter # type: ignore
def notes(self, new_notes: str) -> None:
"""Set entry notes.
:param str new_notes: new notes
"""
if new_notes != self._notes:
self._notes = new_notes
self._element.notes = new_notes
self.updated()
@property
def parentgroup(self) -> SafeGroup:
"""Parent Group of the element.
:returns: parent group
:rtype: SafeGroup
"""
if self.is_root_group:
return self
for group in self._db_manager.groups:
if group.uuid == self._element.parentgroup.uuid:
return group
logging.error("This should be unreachable: parentgroup")
return SafeGroup(self._db_manager, self._element.parentgroup)
@property
def parentgroup_uuid(self) -> UUID:
"""UUID of the parent Group of the element.
This method should be preferred than parentgroup since it does not go
through the entire list of elements.
:returns: parent group
:rtype: SafeGroup
"""
if self.is_root_group:
return self.uuid
return self._element.parentgroup.uuid
@property
def is_root_group(self) -> bool:
if self.is_entry:
return False
return self._element.is_root_group
@property
def is_trash_bin(self) -> bool:
if trash_bin_inner := self._db_manager.db.recyclebin_group:
return self.uuid == trash_bin_inner.uuid
return False
@property
def uuid(self) -> UUID:
"""UUID of the element.
:returns: uuid of the element
:rtype: UUID
"""
return self._element.uuid
@GObject.Property(type=str, flags=GObject.ParamFlags.READABLE)
def parent_uuid_str(self) -> str:
"""UUID of the parent group of the element.
:returns: uuid of the parent
:rtype: UUID
"""
if self.is_root_group:
return ""
return str(self.parentgroup_uuid)
@property
def path(self) -> list[str]:
return self._element.path
@property
def atime(self) -> GLib.DateTime | None:
"""The UTC accessed time of the element."""
try:
time = self._element.atime
except ValueError:
logging.exception("Invalid accessed time")
return None
except OverflowError:
logging.exception("Accessed time for %s overflows", self.name)
return None
except Exception: # pylint: disable=broad-except
logging.exception("Could not read accessed time")
return None
if not time:
return None
return GLib.DateTime.new_utc(
time.year,
time.month,
time.day,
time.hour,
time.minute,
time.second,
)
@GObject.Property(type=int, flags=GObject.ParamFlags.READABLE)
def ctime_int(self) -> int:
try:
time = self._element.ctime
except ValueError:
logging.exception("Invalid creation time")
return -1
except OverflowError:
logging.exception("Creation time for %s overflows", self.name)
return -1
except Exception: # pylint: disable=broad-except
logging.exception("Could not read creation time")
return -1
if not time:
return -1
return time.timestamp()
@property
def ctime(self) -> GLib.DateTime | None:
"""The UTC creation time of the element."""
try:
time = self._element.ctime
except ValueError:
logging.exception("Invalid creation time")
return None
except OverflowError:
logging.exception("Creation time for %s overflows", self.name)
return None
except Exception: # pylint: disable=broad-except
logging.exception("Could not read creation time")
return None
if not time:
return None
return GLib.DateTime.new_utc(
time.year,
time.month,
time.day,
time.hour,
time.minute,
time.second,
)
@property
def mtime(self) -> GLib.DateTime | None:
"""The UTC modified time of the element."""
try:
time = self._element.mtime
except ValueError:
logging.exception("Invalid modified time")
return None
except OverflowError:
logging.exception("Modified time for %s overflows", self.name)
return None
except Exception: # pylint: disable=broad-except
logging.exception("Could not read modified time")
return None
if not time:
return None
return GLib.DateTime.new_utc(
time.year,
time.month,
time.day,
time.hour,
time.minute,
time.second,
)
class SafeGroup(SafeElement):
_entries = None
_entries_filter = None
_subgroups = None
_subgroups_filter = None
children_changed = GObject.Signal(arg_types=(int, int))
def __init__(self, db_manager: DatabaseManager, group: Group) -> None:
"""GObject to handle a safe group.
:param DatabaseManager db_manager: database of the group
:param Group group: group to handle
"""
super().__init__(db_manager, group)
self._group: Group = group
if self.is_root_group:
self._db_manager.root = self
if self.is_trash_bin:
self._db_manager.trash_bin = self
@staticmethod
def get_root(db_manager: DatabaseManager) -> SafeGroup:
"""Get the root group."""
# pylint: disable=no-member
if root := db_manager.root:
return root
for group in db_manager.groups:
if group.is_root_group:
return group
logging.error("This should be unreachable: get_root")
return SafeGroup(db_manager, db_manager.db.root_group)
def new_entry(
self,
title: str = "",
username: str = "",
password: str = "",
) -> SafeEntry:
"""Add a new entry to self."""
group = self.group
force: bool = self._db_manager.check_entry_in_group_exists("", group)
if username == "":
username = self._db_manager.default_username
new_entry = self._db_manager.db.add_entry(
group,
title,
username,
password,
url=None,
notes=None,
expiry_time=None,
tags=None,
icon="0",
force_creation=force,
)
safe_entry = SafeEntry(self._db_manager, new_entry)
self.updated()
self._db_manager.entries.append(safe_entry)
return safe_entry
def new_subgroup(
self,
name: str = "",
icon: str | None = None,
notes: str = "",
) -> SafeGroup:
"""Add a new subgroup to self."""
new_group = self._db_manager.db.add_group(
self.group,
name,
icon=icon,
notes=notes,
)
safe_group = SafeGroup(self._db_manager, new_group)
self.updated()
self._db_manager.groups.append(safe_group)
return safe_group
def filter_changed(self, is_entry: bool) -> None:
if is_entry:
if self._entries_filter is None:
self.init_entries()
self._entries_filter.changed(Gtk.FilterChange.DIFFERENT) # type: ignore
else:
if self._subgroups_filter is None:
self.init_subgroups()
self._subgroups_filter.changed(Gtk.FilterChange.DIFFERENT) # type: ignore
def init_subgroups(self):
expr = Gtk.PropertyExpression.new(
SafeElement.__gtype__, None, "parent_uuid_str"
)
self._subgroups_filter = Gtk.StringFilter.new(expr)
self._subgroups_filter.props.search = str(self.uuid)
self._subgroups = Gtk.FilterListModel.new(
self._db_manager.groups,
self._subgroups_filter,
)
def init_entries(self):
expr = Gtk.PropertyExpression.new(
SafeElement.__gtype__, None, "parent_uuid_str"
)
self._entries_filter = Gtk.StringFilter.new(expr)
self._entries_filter.props.search = str(self.uuid)
self._entries = Gtk.FilterListModel.new(
self._db_manager.entries,
self._entries_filter,
)
@property
def subgroups(self) -> Gio.ListModel:
if self._subgroups is None:
self.init_subgroups()
return self._subgroups
@property
def entries(self) -> Gio.ListModel:
if self._entries is None:
self.init_entries()
return self._entries
@property
def group(self) -> Group:
"""Returns the private pykeepass group."""
return self._group
def emit_children_changed(self, removed, added):
self.emit(self.children_changed, removed, added)
class SafeEntry(SafeElement):
# pylint: disable=too-many-instance-attributes, too-many-public-methods
_color_key = "color_prop_LcljUMJZ9X"
_expired_id: int | None = None
_note_key = "Notes"
_otp: TOTP | None = None
_otp_key = "otp"
history_saved = GObject.Signal()
def __init__(self, db_manager: DatabaseManager, entry: Entry) -> None:
"""GObject to handle a safe entry.
:param DatabaseManager db_manager: database of the entry
:param Entry entry: entry to handle
"""
super().__init__(db_manager, entry)
self._entry: Entry = entry
self._attachments: list[Attachment] = entry.attachments or []
# NOTE Can fail at libpykeepass, see
# https://github.com/libkeepass/pykeepass/issues/254
# TODO Read as many attributes as possible until we see an error.
try:
attributes = {
key: value
for key, value in entry.custom_properties.items()
if key not in (self._color_key, self._note_key, self._otp_key)
}
except Exception: # pylint: disable=broad-except
logging.exception("Could not read attributes")
attributes = {}
self._attributes = AttributesModel(attributes)
color_value: str = entry.get_custom_property(self._color_key)
self._color: str = color_value or EntryColor.NONE.value
self._icon_nr: str = entry.icon or ""
self._password: str = entry.password or ""
self._url: str = entry.url or ""
self._username: str = entry.username or ""
if otp_uri := entry.otp:
try:
if otp_uri.startswith("otpauth://"):
self._otp = parse_uri(otp_uri) # type: ignore
else:
self._otp = TOTP(otp_uri)
except ValueError:
logging.exception("Could not parse OTP")
self._check_expiration()
@property
def entry(self) -> Entry:
"""Get entry.
:returns: entry
:rtype: Entry
"""
return self._entry
def duplicate(self) -> None:
"""Duplicate an entry."""
title: str = self.name or ""
username: str = self.username or ""
password: str = self.password or ""
# NOTE: With clone is meant a duplicated object, not the process
# of cloning/duplication; "the" clone
entry = self.entry
clone_entry: Entry = self._db_manager.db.add_entry(
entry.parentgroup,
title + " - " + _("Clone"),
username,
password,
url=entry.url,
notes=entry.notes,
expiry_time=entry.expiry_time,
tags=entry.tags,
icon=entry.icon,
force_creation=True,
)
clone_entry.expires = entry.expires
if entry.otp:
clone_entry.otp = entry.otp
# Add custom properties
for key in entry.custom_properties:
value: str = entry.custom_properties[key] or ""
clone_entry.set_custom_property(key, value)
safe_entry = SafeEntry(self._db_manager, clone_entry)
self.parentgroup.updated()
self._db_manager.entries.append(safe_entry)
def _check_expiration(self) -> None:
"""Check expiration.
If the entry is expired, this ensures that a notification is sent.
If the entry is not expired yet, a timeout is set to regularly
check if the entry is expired.
"""
if self._expired_id:
GLib.source_remove(self._expired_id)
self._expired_id = None
if not self.props.expires:
return
# Update state
self.notify("expired")
if not self.props.expired:
self._expired_id = GLib.timeout_add_seconds(600, self._is_expired)
def _is_expired(self) -> bool:
if self.props.expired:
self._expired_id = None
self.notify("expired")
return GLib.SOURCE_REMOVE
return GLib.SOURCE_CONTINUE
def save_history(self) -> None:
"""Save current version of the entry in its history."""
# NOTE Attachments are references, so duplicating them is ok.
self._entry.save_history()
self.updated()
self.emit(self.history_saved)
def delete_history(self, entry: SafeEntry) -> None:
"""Delete entry from the history of self."""
self._entry.delete_history(entry.entry)
self.updated()
self.emit(self.history_saved)
@GObject.Property(type=object, flags=GObject.ParamFlags.READABLE)
def attachments(self) -> list[Attachment]:
return self._attachments
def add_attachment(self, byte_buffer: bytes, filename: str) -> Attachment:
"""Add an attachment to the entry.
:param bytes byte_buffer: attachment content
:param str filename: attachment name
:returns: attachment
:rtype: Attachment
"""
attachment_id = self._db_manager.db.add_binary(byte_buffer)
attachment = self._entry.add_attachment(attachment_id, filename)
self._attachments.append(attachment)
self.updated()
self.notify("attachments")
return attachment
def delete_attachment(self, attachment: Attachment) -> None:
"""Remove an attachment from the entry.
:param Attachmennt attachment: attachment to delete
"""
self._db_manager.db.delete_binary(attachment.id)
self._attachments.remove(attachment)
self.notify("attachments")
self.updated()
def get_attachment(self, id_: str) -> Attachment | None:
"""Get an attachment from its id.
:param str id_: attachment id
:returns: attachment
:rtype: Attachment
"""
for attachment in self._attachments:
if str(attachment.id) == id_:
return attachment
return None
def get_attachment_content(self, attachment: Attachment) -> bytes:
"""Get an attachment content.
:param Attachmennt attachment: attachment
"""
return self._db_manager.db.binaries[attachment.id]
@GObject.Property(type=object, flags=GObject.ParamFlags.READABLE)
def attributes(self) -> AttributesModel:
return self._attributes
def has_attribute(self, key: str) -> bool:
"""Check if an attribute exists.
:param str key: attribute key to check
"""
return self._attributes.has_attribute(key)
def set_attribute(self, key: str, value: str, protected: bool = False) -> None:
"""Add or replace an entry attribute.
:param str key: attribute key
:param str value: attribute value
"""
if self.props.attributes.get(key) == value:
return
if self._entry.get_custom_property(
key,
) == value and protected == self._entry.is_custom_property_protected(key):
return
self._entry.set_custom_property(key, value, protect=protected)
self._attributes.insert(key, value)
self.updated()
self.notify("attributes")
def delete_attribute(self, key: str) -> None:
"""Delete an attribute.
:param key: attribute key to delete
"""
if not self.has_attribute(key):
return
self._entry.delete_custom_property(key)
self._attributes.pop(key)
self.updated()
self.notify("attributes")
def is_attribute_protected(self, key: str) -> bool:
"""Return whether the attribute with a specific key is protected.
If there is no such key returns False.
"""
return self.entry.is_custom_property_protected(key)
@GObject.Property(type=str, default=EntryColor.NONE.value)
def color(self) -> str:
"""Get entry color.
:returns: color as string
:rtype: str
"""
return self._color
@color.setter # type: ignore
def color(self, new_color: str) -> None:
"""Set an entry color.
:param str new_color: new color as string
"""
if new_color != self._color:
self._color = new_color
self._entry.set_custom_property(self._color_key, new_color)
self.updated()
@property
def history(self) -> list[SafeEntry]:
history = self._entry.history
return [SafeEntry(self._db_manager, entry) for entry in history]
@GObject.Property(type=object)
def icon(self) -> Icon:
"""Get icon number.
:returns: icon number or "0" if no icon
:rtype: str
"""
try:
return ICONS[self._icon_nr]
except KeyError:
return ICONS["0"]
@icon.setter # type: ignore
def icon(self, new_icon_nr: str) -> None:
"""Set icon number.
:param str new_icon_nr: new icon number
"""
if new_icon_nr != self._icon_nr:
self._icon_nr = new_icon_nr
self._entry.icon = new_icon_nr
self.notify("icon-name")
self.updated()
@GObject.Property(type=str, default="", flags=GObject.ParamFlags.READABLE)
def icon_name(self) -> str:
"""Get the icon name.
:returns: icon name or the default icon if undefined
:rtype: str
"""
return self.props.icon.name
@property
def tags(self) -> dict:
return self.entry.tags
@tags.setter # type: ignore
def tags(self, new_tags: dict) -> None:
self.entry.tags = new_tags
self._db_manager.is_dirty = True
@GObject.Property(type=str, default="")
def otp(self) -> str:
if self._otp:
return self._otp.secret
return ""
@otp.setter # type: ignore
def otp(self, otp: str) -> None:
updated = False
# Some sites give the secret in chunks split by spaces for easy reading
# lets strip those as they'll produce an invalid secret.
otp = otp.replace(" ", "")
if not otp and self._otp:
# Delete existing
self._otp = None
# NOTE the opt property doesn't accept None.
self._element.otp = ""
self.updated()
elif self._otp and self._otp.secret != otp:
# Changing an existing OTP
self._otp.secret = otp
updated = True
elif otp:
# Creating brand new OTP.
self._otp = TOTP(otp, issuer=self.name)
updated = True
if updated:
self._element.otp = self._otp.provisioning_uri()
self.updated()
def otp_interval(self) -> int:
if isinstance(self._otp, TOTP):
return self._otp.interval
return 30
def otp_lifespan(self) -> float | None:
"""Return seconds until token expires."""
if isinstance(self._otp, TOTP):
gnow = GLib.DateTime.new_now_utc()
now_seconds = gnow.to_unix()
now_milis = gnow.get_seconds() % 1
now = now_seconds + now_milis
return self._otp.interval - now % self._otp.interval
return None
def otp_token(self) -> str | None:
if self._otp:
try:
return self._otp.now()
except binascii.Error:
logging.debug(
"Error caught in OTP token generation (likely invalid "
"base32 secret).",
)
return None
@GObject.Property(type=str, default="")
def password(self) -> str:
"""Get entry password.
:returns: password or an empty string if there is none
:rtype: str
"""
return self._password
@password.setter # type: ignore
def password(self, new_password: str) -> None:
"""Set entry password.
:param str new_password: new password
"""
if new_password != self._password:
self._password = new_password
self._entry.password = new_password
self.updated()
@GObject.Property(type=str, default="")
def url(self) -> str:
"""Get entry url.
:returns: url or an empty string if there is none
:rtype: str
"""
return self._url
@url.setter # type: ignore
def url(self, new_url: str) -> None:
"""Set entry url.
:param str new_url: new url
"""
if new_url != self._url:
self._url = new_url
self._entry.url = new_url
self.updated()
@GObject.Property(type=str, default="")
def username(self) -> str:
"""Get entry username.
:returns: username or an empty string if there is none
:rtype: str
"""
return self._username
@username.setter # type: ignore
def username(self, new_username: str) -> None:
"""Set entry username.
:param str new_username: new username
"""
if new_username != self._username:
self._username = new_username
self._entry.username = new_username
self.updated()
@GObject.Property(type=bool, default=False)
def expires(self) -> bool:
return self.entry.expires
@expires.setter # type: ignore
def expires(self, value: bool) -> None:
if value != self.entry.expires:
self.entry.expires = value
self._check_expiration()
self.updated()
self.notify("expired")
@GObject.Property(
type=bool,
default=False,
flags=GObject.ParamFlags.READABLE | GObject.ParamFlags.EXPLICIT_NOTIFY,
)
def expired(self):
try:
return self.entry.expired
except Exception: # pylint: disable=broad-except
logging.exception("Could not read expiry date from %s", self.name)
return False
@property
def expiry_time(self) -> GLib.DateTime | None:
"""Returns the expiration time in the UTC timezone.
Returns None when there isn't an expiration date.
"""
try:
time = self.entry.expiry_time
except Exception: # pylint: disable=broad-except
logging.exception("Could not read expiry date from %s", self.name)
return None
if not time:
return None
return GLib.DateTime.new_utc(
time.year,
time.month,
time.day,
time.hour,
time.minute,
time.second,
)
@expiry_time.setter # type: ignore
def expiry_time(self, value: GLib.DateTime) -> None:
"""Set the expiration time in the UTC timezone."""
if value != self.entry.expiry_time:
expired = datetime(
value.get_year(),
value.get_month(),
value.get_day_of_month(),
value.get_hour(),
value.get_minute(),
value.get_second(),
tzinfo=UTC,
)
self.entry.expiry_time = expired
self._check_expiration()
self.updated()
class Icon(NamedTuple):
# pylint: disable=inherit-non-class
# This is a false positive because pylint does not properly handle
# Python 3.9 at the moment. It can be safely removed once pylint
# handles it.
name: str
visible: bool = False
# https://github.com/dlech/KeePass2.x/blob/4facf2f1ebc76eeddbe11975eccb0dc2b49dfc37/KeePassLib/PwEnums.cs#L81
# https://hsto.org/files/b1e/d20/e38/b1ed20e385d642cc870355fdef153fb9.png
# FIXME: Based on the names from the links above, some of the current
# icons should be replaced.
ICONS = {
"0": Icon("dialog-password-symbolic", True),
"1": Icon("network-wired-symbolic", True),
"2": Icon("dialog-warning-symbolic"),
"3": Icon("network-server-symbolic"),
"4": Icon("document-edit-symbolic"),
"5": Icon("media-view-subtitles-symbolic"),
"6": Icon("application-x-addon-symbolic"),
"7": Icon("notepad-symbolic"),
"8": Icon("network-wired-symbolic"),
"9": Icon("send-symbolic", True),
"10": Icon("text-x-generic-symbolic"),
"11": Icon("camera-photo-symbolic"),
"12": Icon("network-wireless-signal-excellent-symbolic", True),
"13": Icon("dialog-password-symbolic"),
"14": Icon("colorimeter-colorhug-symbolic"),
"15": Icon("scanner-symbolic"),
"16": Icon("user-available-symbolic", True),
"17": Icon("media-optical-cd-audio-symbolic"),
"18": Icon("video-display-symbolic"),
"19": Icon("mail-unread-symbolic", True),
"20": Icon("settings-symbolic"),
"21": Icon("edit-paste-symbolic"),
"22": Icon("edit-paste-symbolic"),
"23": Icon("display-with-window-symbolic", True),
"24": Icon("uninterruptible-power-supply-symbolic"),
"25": Icon("mail-unread-symbolic"),
"26": Icon("media-floppy-symbolic"),
"27": Icon("drive-harddisk-symbolic", True),
"28": Icon("dialog-password-symbolic"),
"29": Icon("airplane-mode-symbolic", True),
"30": Icon("terminal-symbolic", True),
"31": Icon("printer-symbolic"),
"32": Icon("image-x-generic-symbolic"),
"33": Icon("edit-select-all-symbolic"),
"34": Icon("preferences-system-symbolic", True),
"35": Icon("network-workgroup-symbolic"),
"36": Icon("dialog-password-symbolic"),
"37": Icon("auth-fingerprint-symbolic", True),
"38": Icon("drive-harddisk-symbolic"),
"39": Icon("document-open-recent-symbolic"),
"40": Icon("system-search-symbolic"),
"41": Icon("applications-games-symbolic", True),
"42": Icon("media-flash-symbolic"),
"43": Icon("user-trash-symbolic"),
"44": Icon("notepad-symbolic"),
"45": Icon("edit-delete-symbolic"),
"46": Icon("dialog-question-symbolic"),
"47": Icon("package-x-generic-symbolic"),
"48": Icon("folder-symbolic", True),
"49": Icon("folder-open-symbolic"),
"50": Icon("document-open-symbolic"),
"51": Icon("system-lock-screen-symbolic", True),
"52": Icon("rotation-locked-symbolic"),
"53": Icon("object-select-symbolic"),
"54": Icon("document-edit-symbolic"),
"55": Icon("image-x-generic-symbolic"),
"56": Icon("open-book-symbolic", True),
"57": Icon("view-list-symbolic"),
"58": Icon("avatar-default-symbolic", True),
"59": Icon("applications-engineering-symbolic"),
"60": Icon("go-home-symbolic"),
"61": Icon("starred-symbolic", True),
"62": Icon("start-here-symbolic"),
"63": Icon("dialog-password-symbolic"),
"64": Icon("start-here-symbolic"),
"65": Icon("open-book-symbolic"),
"66": Icon("money-symbolic", True),
"67": Icon("application-certificate-symbolic"),
"68": Icon("phone-apple-iphone-symbolic", True),
}
|