Documentation
¶
Overview ¶
Package keychain is a pure-Go (CGO_ENABLED=0) store for macOS Keychain generic-password items. It reaches the OS through github.com/ebitengine/purego — dlsym'd CoreFoundation and Security C functions — so it links with no cgo and never shells out to /usr/bin/security (the secret never appears in any process's argv).
The public surface is three byte-oriented calls over a (service, account) pair, each mapping to a kSecClassGenericPassword item:
err := keychain.Set(service, account, secret) secret, err := keychain.Get(service, account) // keychain.ErrNotFound if absent err := keychain.Delete(service, account)
Errors are typed: ErrNotFound for a miss, ErrEmptySecret for an empty write, ErrUnsupported off darwin, and *Error (carrying the raw OSStatus) for any other Security-framework failure.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is returned by [Get] when no item exists for the // (service, account) pair. ErrNotFound = errors.New("keychain: item not found") // ErrEmptySecret is returned by [Set] when secret is empty; the Keychain // cannot hold a zero-length generic password and [Get] could not tell it // apart from a miss. ErrEmptySecret = errors.New("keychain: empty secret") // ErrUnsupported is returned by every entry point on non-darwin platforms // (the Keychain is macOS-only). Exported symbols exist everywhere so // consumers cross-compile. ErrUnsupported = errors.New("keychain: unsupported on this platform (darwin only)") )
Sentinel errors. They are stable and may be compared with errors.Is.
Functions ¶
func Delete ¶
Delete removes the item identified by (service, account). Deleting an absent item is not an error. It returns ErrUnsupported off darwin.
func Get ¶
Get returns the secret stored under (service, account). It returns ErrNotFound when no such item exists and ErrUnsupported off darwin. Reading an item created with WithAccessControl and UserPresence triggers the system biometric prompt.
func Set ¶
Set stores secret under the generic-password item identified by (service, account), replacing any existing value. An empty secret is rejected with ErrEmptySecret. On a non-darwin platform it returns ErrUnsupported.
Types ¶
type AccessControlFlags ¶
type AccessControlFlags uint
AccessControlFlags selects the SecAccessControlCreateFlags constraints an item created with WithAccessControl must satisfy on read. The zero value applies no interactive constraint (the item is readable whenever the accessibility class is satisfied, with no Touch ID / passcode prompt).
const ( // UserPresence (kSecAccessControlUserPresence) gates every read behind // Touch ID or the device passcode. Reading such an item triggers the // system biometric prompt. UserPresence AccessControlFlags = 1 << 0 // BiometryAny (kSecAccessControlBiometryAny) requires any currently // enrolled biometric. BiometryAny AccessControlFlags = 1 << 1 // BiometryCurrentSet (kSecAccessControlBiometryCurrentSet) invalidates the // item if the enrolled biometric set changes. BiometryCurrentSet AccessControlFlags = 1 << 3 // DevicePasscode (kSecAccessControlDevicePasscode) gates reads behind the // device passcode. DevicePasscode AccessControlFlags = 1 << 4 )
type Error ¶
type Error struct {
// Op is the failing operation: "set", "get" or "delete".
Op string
// Status is the raw OSStatus returned by the Security framework.
Status int32
}
Error wraps a non-success, non-not-found OSStatus from the Security framework, tagged with the operation that produced it.
type Option ¶
type Option func(*config)
Option customises a Set. Options are applied left to right.
func WithAccessControl ¶
func WithAccessControl(flags AccessControlFlags) Option
WithAccessControl stores the item behind a SecAccessControl protecting it with the given flags and the accessibility class kSecAttrAccessibleWhenUnlockedThisDeviceOnly (so it never leaves this device and is never synced to iCloud). Pass UserPresence to gate reads behind Touch ID or the passcode; pass 0 for an at-rest-protected item with no interactive prompt. Without this option an item is a plain generic password created (or updated in place) with the default accessibility.