Skip to main content

macos_keychain

Reads the desktop app's key from the macOS keychain without showing UI.

The desktop app owns its keychain item (service Bitfount, account ENCRYPTION::KEY) and puts the orchestrator on the item's access list. The orchestrator only reads, and must never show a dialog: it works in the background, often with nobody at the screen, so a dialog would either stall the read or put a keychain prompt in front of someone who didn't start anything.

This module reads through PyObjC's bindings to Security.framework rather than through the keyring package:

  • keyring's macOS read is a plain SecItemCopyMatching with no way to suppress UI, so an item that doesn't trust the orchestrator yet would show a dialog during background work.
  • keyring.get_password turns every failure into a generic PasswordGetError without the OSStatus, so "not trusted yet", "locked" and "denied" look the same to the key lookup and to telemetry. Getting the status back means calling keyring.backends.macOS.api, which is internal.
  • It would add keyring and its jaraco.* packages (and, on Linux, the Secret Service ones) to the frozen build, for code this module would mostly bypass.

PyObjC rather than hand-written ctypes, because it binds all of Security.framework, the legacy SecKeychain* calls included, and manages CoreFoundation memory itself, so there is no manual CFRelease. Shelling out to security find-generic-password is no alternative either: the reading process would then be /usr/bin/security, which isn't on the item's access list, so macOS would prompt.

Only macOS needs PyObjC. Elsewhere every read fails with keychain_error.

Module​

Functions​

read_generic_password​

def read_generic_password(service: str, account: str) ‑> bytes:

Reads a generic password from the keychain, never showing UI.

Two mechanisms keep the read silent: the query asks for kSecUseAuthenticationUIFail, and keychain UI is turned off for the whole process around the call (SecKeychainSetUserInteractionAllowed), because on the file-based login keychain the query option alone may not stop an access-list prompt. Wherever macOS would have shown a dialog, the read fails instead.

Arguments

  • service: The item's service.
  • account: The item's account.

Returns The item's secret, as stored.

Raises

  • KeychainReadError: If the item can't be read without UI, with the reason and the OSStatus. Off macOS, always, with reason keychain_error and status 0.

reason_for_status​

def reason_for_status(    status: int, keychain_unlocked: bool | None,) ‑> KeychainUnavailableReason:

Maps a failed read's OSStatus to the reason the key lookup reports.

  • errSecItemNotFound (-25300) is no_item.
  • errSecInteractionNotAllowed (-25308) means macOS would have shown

Arguments

  • status: The OSStatus the read returned.
  • keychain_unlocked: Whether the default keychain is unlocked, or None if that isn't known.

Returns The reason for the failure.