Skip to content

Commit

Permalink
set primary uid method
Browse files Browse the repository at this point in the history
  • Loading branch information
Dax Harris committed Mar 21, 2024
1 parent fd22930 commit 1ab33cc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
26 changes: 25 additions & 1 deletion gpyg/operators/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,31 @@ def revoke_signature(
cmd,
input=passphrase + "\n" if passphrase else None,
)
print(proc.output)
if proc.code == 0:
return self.reload()
else:
raise ExecutionError(proc.output)

def set_primary_uid(self, uid: str, passphrase: str | None = None) -> "Key":
"""Set the primary UID of the current Key
Args:
uid (str): User ID to set as primary
passphrase (str | None, optional): Key passphrase, if required. Defaults to None.
Raises:
ExecutionError: If command execution fails
Returns:
Key: An updated reference to the Key
"""
cmd = "gpg --batch --pinentry-mode loopback --passphrase-fd 0 --quick-set-primary-uid '{fingerprint}' '{uid}'".format(
fingerprint=self.fingerprint, uid=uid
).strip()
proc = self.session.run(
cmd,
input=passphrase + "\n" if passphrase else None,
)
if proc.code == 0:
return self.reload()
else:
Expand Down
9 changes: 3 additions & 6 deletions scratchpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

with TemporaryDirectory(dir="tmp", delete=False) as tmpdir:
gpg = GPG(homedir=tmpdir, kill_existing_agent=True)
key = gpg.keys.generate_key("Bongus", passphrase="test-signed")
key_signer = gpg.keys.generate_key("Signer", passphrase="test-signer")
key_signer.sign_key(key, password="test-signer")
key.revoke_signature(key_signer, passphrase="test-signed")
time.sleep(2)
key_signer.sign_key(key, password="test-signer")
key = gpg.keys.generate_key("Bongus", passphrase="test")
key.add_user_id(uid="New Prime", passphrase="test")
key.set_primary_uid("New Prime", passphrase="test")
print(key.model_dump_json(indent=4))
9 changes: 9 additions & 0 deletions tests/test_op_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,12 @@ def test_revoke_sig(environment):
assert len(keys[0].valid_signatures) == 2
keys[0].revoke_signature(keys[1], passphrase="test-psk-1")
assert len(keys[0].valid_signatures) == 1


def test_set_primary_uid(environment):
key = environment.keys.list_keys()[3]

key.add_user_id(uid="New Primary")
assert len(key.user_ids) == 2
key.set_primary_uid("New Primary")
assert key.user_ids[0].uid == "New Primary"

0 comments on commit 1ab33cc

Please sign in to comment.