Keysmith

Entities

Core data types used across Keysmith.

Keysmith defines five core entity types, each backed by a dedicated store interface.

Key

The key.Key struct represents an API key. Raw keys are never stored -- only their SHA-256 hash.

FieldTypeDescription
IDid.KeyIDUnique TypeID identifier
NamestringHuman-readable key name
HashstringSHA-256 hash of the raw key
PrefixstringKey prefix (e.g., "sk")
EnvironmentEnvironmentlive, test, or dev
StateStateactive, rotated, expired, revoked, or suspended
Scopes[]stringPermission scopes assigned to the key
AppIDstringApplication identifier
TenantIDstringTenant identifier
PolicyID*id.PolicyIDOptional attached policy
ExpiresAt*time.TimeOptional expiration time
LastUsedAt*time.TimeLast time the key was used

Key states

StateDescriptionTransitions to
activeValid and usablerotated, expired, revoked, suspended
rotatedReplaced, may be in grace periodrevoked
expiredPast expiration time(terminal)
revokedPermanently disabled(terminal)
suspendedTemporarily disabledactive (reactivate)

Environments

ValueConstantDescription
livekey.EnvLiveProduction environment
testkey.EnvTestTesting environment
devkey.EnvDevDevelopment environment

Policy

The policy.Policy struct defines constraints for API keys.

FieldTypeDescription
IDid.PolicyIDUnique identifier
NamestringPolicy name
RateLimitintMax requests per window
RateWindowtime.DurationRate limit window
AllowedIPs[]stringCIDR-notation IP allowlist
AllowedOrigins[]stringHTTP origin allowlist
AllowedScopes[]stringPermitted scopes
MaxKeyAgetime.DurationMaximum key lifetime

Scope

The scope.Scope struct represents a permission scope that can be assigned to keys.

FieldTypeDescription
IDid.ScopeIDUnique identifier
NamestringScope name (e.g., "read:users")
DescriptionstringHuman-readable description

Usage Record

The usage.Record struct tracks per-request API key usage.

FieldTypeDescription
IDid.UsageIDUnique identifier
KeyIDid.KeyIDAssociated key
EndpointstringRequest endpoint
MethodstringHTTP method
StatusCodeintResponse status code
IPstringClient IP address
UserAgentstringClient user agent
Timestamptime.TimeRequest timestamp

Rotation Record

The rotation.Record struct tracks key rotation history.

FieldTypeDescription
IDid.RotationIDUnique identifier
KeyIDid.KeyIDRotated key
OldHashstringPrevious key hash
NewHashstringNew key hash
ReasonReasonRotation reason
GraceTTLtime.DurationGrace period duration
GraceExpirytime.TimeWhen grace period ends

Rotation reasons

ReasonConstantDescription
scheduledrotation.ReasonScheduledRegular scheduled rotation
compromisedrotation.ReasonCompromisedKey may have been compromised
expiringrotation.ReasonExpiringKey approaching expiration
manualrotation.ReasonManualManual rotation

On this page