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