|
| 1 | +# pykeepass |
| 2 | + |
| 3 | +<a href="https://github.com/libkeepass/pykeepass/actions/workflows/ci.yaml"><img src="https://github.com/libkeepass/pykeepass/actions/workflows/ci.yaml/badge.svg"/></a> |
| 4 | +<a href="https://libkeepass.github.io/pykeepass"><img src="https://readthedocs.org/projects/pykeepass/badge/?version=latest"/></a> |
| 5 | +<a href="https://matrix.to/#/%23pykeepass:matrix.org"><img src="https://img.shields.io/badge/chat-%23pykeepass-green"/></a> |
| 6 | + |
| 7 | +This library allows you to write entries to a KeePass database. |
| 8 | + |
| 9 | +Come chat at [#pykeepass:matrix.org](https://matrix.to/#/%23pykeepass:matrix.org) on Matrix. |
| 10 | + |
| 11 | +# Installation |
| 12 | + |
| 13 | +``` bash |
| 14 | +sudo apt install python3-lxml |
| 15 | +pip install pykeepass |
| 16 | +``` |
| 17 | + |
| 18 | +# Quickstart |
| 19 | + |
| 20 | +General database manipulation |
| 21 | + |
| 22 | +``` python |
| 23 | +from pykeepass import PyKeePass |
| 24 | + |
| 25 | +# load database |
| 26 | +>>> kp = PyKeePass('db.kdbx', password='somePassw0rd') |
| 27 | + |
| 28 | +# get all entries |
| 29 | +>>> kp.entries |
| 30 | +[Entry: "foo_entry (myusername)", Entry: "foobar_entry (myusername)", ...] |
| 31 | + |
| 32 | +# find any group by its name |
| 33 | +>>> group = kp.find_groups(name='social', first=True) |
| 34 | + |
| 35 | +# get the entries in a group |
| 36 | +>>> group.entries |
| 37 | +[Entry: "social/facebook (myusername)", Entry: "social/twitter (myusername)"] |
| 38 | + |
| 39 | +# find any entry by its title |
| 40 | +>>> entry = kp.find_entries(title='facebook', first=True) |
| 41 | + |
| 42 | +# retrieve the associated password and OTP information |
| 43 | +>>> entry.password |
| 44 | +'s3cure_p455w0rd' |
| 45 | +>>> entry.otp |
| 46 | +otpauth://totp/test:lkj?secret=TEST%3D%3D%3D%3D&period=30&digits=6&issuer=test |
| 47 | + |
| 48 | +# update an entry |
| 49 | +>>> entry.notes = 'primary facebook account' |
| 50 | + |
| 51 | +# create a new group |
| 52 | +>>> group = kp.add_group(kp.root_group, 'email') |
| 53 | + |
| 54 | +# create a new entry |
| 55 | +>>> kp.add_entry(group, 'gmail', 'myusername', 'myPassw0rdXX') |
| 56 | +Entry: "email/gmail (myusername)" |
| 57 | + |
| 58 | +# save database |
| 59 | +>>> kp.save() |
| 60 | +``` |
| 61 | + |
| 62 | +Finding and manipulating entries |
| 63 | + |
| 64 | +``` python |
| 65 | +# add a new entry to the Root group |
| 66 | +>>> kp.add_entry(kp.root_group, 'testing', 'foo_user', 'passw0rd') |
| 67 | +Entry: "testing (foo_user)" |
| 68 | + |
| 69 | +# add a new entry to the social group |
| 70 | +>>> group = kp.find_groups(name='social', first=True) |
| 71 | +>>> entry = kp.add_entry(group, 'testing', 'foo_user', 'passw0rd') |
| 72 | +Entry: "testing (foo_user)" |
| 73 | + |
| 74 | +# save the database |
| 75 | +>>> kp.save() |
| 76 | + |
| 77 | +# delete an entry |
| 78 | +>>> kp.delete_entry(entry) |
| 79 | + |
| 80 | +# move an entry |
| 81 | +>>> kp.move_entry(entry, kp.root_group) |
| 82 | + |
| 83 | +# save the database |
| 84 | +>>> kp.save() |
| 85 | + |
| 86 | +# change creation time |
| 87 | +>>> from datetime import datetime, timezone |
| 88 | +>>> entry.ctime = datetime(2023, 1, 1, tzinfo=timezone.utc) |
| 89 | + |
| 90 | +# update modification or access time |
| 91 | +>>> entry.touch(modify=True) |
| 92 | + |
| 93 | +# save entry history |
| 94 | +>>> entry.save_history() |
| 95 | +``` |
| 96 | + |
| 97 | +Finding and manipulating groups |
| 98 | + |
| 99 | +``` python |
| 100 | +>>> kp.groups |
| 101 | +[Group: "foo", Group "foobar", Group: "social", Group: "social/foo_subgroup"] |
| 102 | + |
| 103 | +>>> kp.find_groups(name='foo', first=True) |
| 104 | +Group: "foo" |
| 105 | + |
| 106 | +>>> kp.find_groups(name='foo.*', regex=True) |
| 107 | +[Group: "foo", Group "foobar"] |
| 108 | + |
| 109 | +>>> kp.find_groups(path=['social'], regex=True) |
| 110 | +[Group: "social", Group: "social/foo_subgroup"] |
| 111 | + |
| 112 | +>>> kp.find_groups(name='social', first=True).subgroups |
| 113 | +[Group: "social/foo_subgroup"] |
| 114 | + |
| 115 | +>>> kp.root_group |
| 116 | +Group: "/" |
| 117 | + |
| 118 | +# add a new group to the Root group |
| 119 | +>>> group = kp.add_group(kp.root_group, 'social') |
| 120 | + |
| 121 | +# add a new group to the social group |
| 122 | +>>> group2 = kp.add_group(group, 'gmail') |
| 123 | +Group: "social/gmail" |
| 124 | + |
| 125 | +# save the database |
| 126 | +>>> kp.save() |
| 127 | + |
| 128 | +# delete a group |
| 129 | +>>> kp.delete_group(group) |
| 130 | + |
| 131 | +# move a group |
| 132 | +>>> kp.move_group(group2, kp.root_group) |
| 133 | + |
| 134 | +# save the database |
| 135 | +>>> kp.save() |
| 136 | + |
| 137 | +# change creation time |
| 138 | +>>> from datetime import datetime, timezone |
| 139 | +>>> group.ctime = datetime(2023, 1, 1, tzinfo=timezone.utc) |
| 140 | + |
| 141 | +# update modification or access time |
| 142 | +>>> group.touch(modify=True) |
| 143 | +``` |
| 144 | + |
| 145 | +Attachments |
| 146 | + |
| 147 | +``` python |
| 148 | +>>> e = kp.add_entry(kp.root_group, title='foo', username='', password='') |
| 149 | + |
| 150 | +# add attachment data to the db |
| 151 | +>>> binary_id = kp.add_binary(b'Hello world') |
| 152 | + |
| 153 | +>>> kp.binaries |
| 154 | +[b'Hello world'] |
| 155 | + |
| 156 | +# add attachment reference to entry |
| 157 | +>>> a = e.add_attachment(binary_id, 'hello.txt') |
| 158 | +>>> a |
| 159 | +Attachment: 'hello.txt' -> 0 |
| 160 | + |
| 161 | +# access attachments |
| 162 | +>>> a |
| 163 | +Attachment: 'hello.txt' -> 0 |
| 164 | +>>> a.id |
| 165 | +0 |
| 166 | +>>> a.filename |
| 167 | +'hello.txt' |
| 168 | +>>> a.data |
| 169 | +b'Hello world' |
| 170 | +>>> e.attachments |
| 171 | +[Attachment: 'hello.txt' -> 0] |
| 172 | + |
| 173 | +# list all attachments in the database |
| 174 | +>>> kp.attachments |
| 175 | +[Attachment: 'hello.txt' -> 0] |
| 176 | + |
| 177 | +# search attachments |
| 178 | +>>> kp.find_attachments(filename='hello.txt') |
| 179 | +[Attachment: 'hello.txt** -> 0] |
| 180 | + |
| 181 | +# delete attachment reference |
| 182 | +>>> e.delete_attachment(a) |
| 183 | + |
| 184 | +# or, delete both attachment reference and binary |
| 185 | +>>> kp.delete_binary(binary_id** |
| 186 | +``` |
| 187 | + |
| 188 | +OTP codes |
| 189 | + |
| 190 | +``` python |
| 191 | +# find an entry which has otp attribute |
| 192 | +>>> e = kp.find_entries(otp='.*', regex=True, first=True) |
| 193 | +>>> import pyotp |
| 194 | +>>> pyotp.parse_uri(e.otp).now() |
| 195 | +799270 |
| 196 | +``` |
| 197 | + |
| 198 | + |
| 199 | +# Tests and Debugging |
| 200 | + |
| 201 | +Run tests with `python tests/tests.py` or `python tests/tests.py SomeSpecificTest` |
| 202 | + |
| 203 | +Enable debugging when doing tests in console: |
| 204 | + |
| 205 | +``` python |
| 206 | +>>> from pykeepass.pykeepass import debug_setup |
| 207 | +>>> debug_setup() |
| 208 | +>>> kp.entries[0] |
| 209 | +DEBUG:pykeepass.pykeepass:xpath query: //Entry |
| 210 | +DEBUG:pykeepass.pykeepass:xpath query: (ancestor::Group)[last()] |
| 211 | +DEBUG:pykeepass.pykeepass:xpath query: (ancestor::Group)[last()] |
| 212 | +DEBUG:pykeepass.pykeepass:xpath query: String/Key[text()="Title"]/../Value |
| 213 | +DEBUG:pykeepass.pykeepass:xpath query: String/Key[text()="UserName"]/../Value |
| 214 | +Entry: "root_entry (foobar_user)" |
| 215 | +``` |
| 216 | + |
0 commit comments