Documentation
¶
Overview ¶
Package encryption provides utilities for encrypting and decrypting sensitive data. It uses AES-256-GCM for symmetric encryption of data at rest.
Package encryption provides utilities for hashing sensitive data. It uses bcrypt for secure password-like hashing of tokens.
Package encryption provides a secure database wrapper that encrypts/decrypts sensitive fields.
Package encryption provides a secure token store wrapper that hashes tokens.
Index ¶
- Constants
- Variables
- func GenerateKey() ([]byte, error)
- func GenerateKeyBase64() (string, error)
- func IsEncrypted(value string) bool
- func IsHashed(value string) bool
- func IsHexString(s string) bool
- type Encryptor
- type FieldEncryptor
- type NullEncryptor
- type NullTokenHasher
- type SecureProjectStore
- func (s *SecureProjectStore) CreateProject(ctx context.Context, project proxy.Project) error
- func (s *SecureProjectStore) DeleteProject(ctx context.Context, projectID string) error
- func (s *SecureProjectStore) GetAPIKeyForProject(ctx context.Context, projectID string) (string, error)
- func (s *SecureProjectStore) GetProjectActive(ctx context.Context, projectID string) (bool, error)
- func (s *SecureProjectStore) GetProjectByID(ctx context.Context, projectID string) (proxy.Project, error)
- func (s *SecureProjectStore) ListProjects(ctx context.Context) ([]proxy.Project, error)
- func (s *SecureProjectStore) UpdateProject(ctx context.Context, project proxy.Project) error
- type SecureRateLimitStore
- func (s *SecureRateLimitStore) GetTokenByID(ctx context.Context, tokenID string) (token.TokenData, error)
- func (s *SecureRateLimitStore) IncrementTokenUsage(ctx context.Context, tokenID string) error
- func (s *SecureRateLimitStore) ResetTokenUsage(ctx context.Context, tokenID string) error
- func (s *SecureRateLimitStore) UpdateTokenLimit(ctx context.Context, tokenID string, maxRequests *int) error
- type SecureRevocationStore
- func (s *SecureRevocationStore) DeleteToken(ctx context.Context, tokenID string) error
- func (s *SecureRevocationStore) RevokeBatchTokens(ctx context.Context, tokenIDs []string) (int, error)
- func (s *SecureRevocationStore) RevokeExpiredTokens(ctx context.Context) (int, error)
- func (s *SecureRevocationStore) RevokeProjectTokens(ctx context.Context, projectID string) (int, error)
- func (s *SecureRevocationStore) RevokeToken(ctx context.Context, tokenID string) error
- type SecureTokenStore
- func (s *SecureTokenStore) CreateToken(ctx context.Context, td token.TokenData) error
- func (s *SecureTokenStore) GetTokenByID(ctx context.Context, id string) (token.TokenData, error)
- func (s *SecureTokenStore) GetTokenByToken(ctx context.Context, tokenString string) (token.TokenData, error)
- func (s *SecureTokenStore) GetTokensByProjectID(ctx context.Context, projectID string) ([]token.TokenData, error)
- func (s *SecureTokenStore) IncrementTokenUsage(ctx context.Context, tokenString string) error
- func (s *SecureTokenStore) ListTokens(ctx context.Context) ([]token.TokenData, error)
- func (s *SecureTokenStore) UpdateToken(ctx context.Context, td token.TokenData) error
- type TokenHasher
- type TokenHasherInterface
Constants ¶
const ( // KeySize is the required size for AES-256 encryption keys (32 bytes). KeySize = 32 // NonceSize is the size of the GCM nonce (12 bytes). NonceSize = 12 // EncryptedPrefix is added to encrypted values to identify them. EncryptedPrefix = "enc:v1:" )
const ( // HashPrefix is added to hashed values to identify them. HashPrefix = "hash:v1:" // DefaultBcryptCost is the default cost parameter for bcrypt. // A cost of 10 is a good balance between security and performance. DefaultBcryptCost = 10 )
Variables ¶
var ( // ErrInvalidKeySize is returned when the encryption key has an invalid size. ErrInvalidKeySize = errors.New("encryption key must be exactly 32 bytes") // ErrDecryptionFailed is returned when decryption fails. ErrDecryptionFailed = errors.New("decryption failed") // ErrNoEncryptionKey is returned when no encryption key is configured. ErrNoEncryptionKey = errors.New("no encryption key configured") // ErrInvalidCiphertext is returned when the ciphertext is invalid. ErrInvalidCiphertext = errors.New("invalid ciphertext format") )
var ( // ErrHashMismatch is returned when a hash comparison fails. ErrHashMismatch = errors.New("hash does not match") // ErrInvalidHash is returned when the hash format is invalid. ErrInvalidHash = errors.New("invalid hash format") )
Functions ¶
func GenerateKey ¶
GenerateKey generates a new random 32-byte encryption key.
func GenerateKeyBase64 ¶
GenerateKeyBase64 generates a new random encryption key and returns it as base64.
func IsEncrypted ¶
IsEncrypted checks if a value has the encryption prefix.
func IsHexString ¶
IsHexString checks if a string contains only hexadecimal characters. Exported for use by migration tools and other packages.
Types ¶
type Encryptor ¶
type Encryptor struct {
// contains filtered or unexported fields
}
Encryptor provides encryption and decryption operations. It is safe for concurrent use - cipher.AEAD implementations are thread-safe.
func NewEncryptor ¶
NewEncryptor creates a new Encryptor with the given 32-byte key. The key must be exactly 32 bytes for AES-256 encryption.
func NewEncryptorFromBase64Key ¶
NewEncryptorFromBase64Key creates a new Encryptor from a base64-encoded key.
type FieldEncryptor ¶
type FieldEncryptor interface {
Encrypt(plaintext string) (string, error)
Decrypt(ciphertext string) (string, error)
}
FieldEncryptor is an interface for encrypting and decrypting field values.
type NullEncryptor ¶
type NullEncryptor struct{}
NullEncryptor is a no-op encryptor for when encryption is disabled.
func NewNullEncryptor ¶
func NewNullEncryptor() *NullEncryptor
NewNullEncryptor creates a new NullEncryptor.
type NullTokenHasher ¶
type NullTokenHasher struct{}
NullTokenHasher is a no-op hasher for when hashing is disabled.
func NewNullTokenHasher ¶
func NewNullTokenHasher() *NullTokenHasher
NewNullTokenHasher creates a new NullTokenHasher.
func (*NullTokenHasher) CreateLookupKey ¶
func (h *NullTokenHasher) CreateLookupKey(token string) string
CreateLookupKey returns the token as-is (it's already the lookup key).
func (*NullTokenHasher) HashToken ¶
func (h *NullTokenHasher) HashToken(token string) (string, error)
HashToken returns the token as-is (no hashing).
func (*NullTokenHasher) VerifyToken ¶
func (h *NullTokenHasher) VerifyToken(token, storedToken string) error
VerifyToken performs a constant-time comparison of the tokens.
type SecureProjectStore ¶
type SecureProjectStore struct {
// contains filtered or unexported fields
}
SecureProjectStore wraps a ProjectStore and encrypts/decrypts API keys.
func NewSecureProjectStore ¶
func NewSecureProjectStore(store proxy.ProjectStore, encryptor FieldEncryptor) *SecureProjectStore
NewSecureProjectStore creates a new SecureProjectStore. The encryptor is used to encrypt API keys before storing and decrypt after retrieval. If encryptor is nil, a NullEncryptor is used (no encryption).
func (*SecureProjectStore) CreateProject ¶
CreateProject encrypts the API key and creates the project.
func (*SecureProjectStore) DeleteProject ¶
func (s *SecureProjectStore) DeleteProject(ctx context.Context, projectID string) error
DeleteProject deletes a project.
func (*SecureProjectStore) GetAPIKeyForProject ¶
func (s *SecureProjectStore) GetAPIKeyForProject(ctx context.Context, projectID string) (string, error)
GetAPIKeyForProject retrieves and decrypts the API key for a project.
func (*SecureProjectStore) GetProjectActive ¶
GetProjectActive returns whether a project is active.
func (*SecureProjectStore) GetProjectByID ¶
func (s *SecureProjectStore) GetProjectByID(ctx context.Context, projectID string) (proxy.Project, error)
GetProjectByID retrieves a project and decrypts its API key.
func (*SecureProjectStore) ListProjects ¶
ListProjects retrieves all projects and decrypts their API keys.
func (*SecureProjectStore) UpdateProject ¶
UpdateProject encrypts the API key and updates the project.
type SecureRateLimitStore ¶
type SecureRateLimitStore struct {
// contains filtered or unexported fields
}
SecureRateLimitStore wraps a RateLimitStore and hashes tokens before operations.
func NewSecureRateLimitStore ¶
func NewSecureRateLimitStore(store token.RateLimitStore, hasher TokenHasherInterface) *SecureRateLimitStore
NewSecureRateLimitStore creates a new SecureRateLimitStore.
func (*SecureRateLimitStore) GetTokenByID ¶
func (s *SecureRateLimitStore) GetTokenByID(ctx context.Context, tokenID string) (token.TokenData, error)
GetTokenByID retrieves a token by its ID.
func (*SecureRateLimitStore) IncrementTokenUsage ¶
func (s *SecureRateLimitStore) IncrementTokenUsage(ctx context.Context, tokenID string) error
IncrementTokenUsage increments the usage count for a token.
func (*SecureRateLimitStore) ResetTokenUsage ¶
func (s *SecureRateLimitStore) ResetTokenUsage(ctx context.Context, tokenID string) error
ResetTokenUsage resets the usage count for a token to zero.
func (*SecureRateLimitStore) UpdateTokenLimit ¶
func (s *SecureRateLimitStore) UpdateTokenLimit(ctx context.Context, tokenID string, maxRequests *int) error
UpdateTokenLimit updates the maximum allowed requests for a token.
type SecureRevocationStore ¶
type SecureRevocationStore struct {
// contains filtered or unexported fields
}
SecureRevocationStore wraps a RevocationStore and hashes tokens before operations.
func NewSecureRevocationStore ¶
func NewSecureRevocationStore(store token.RevocationStore, hasher TokenHasherInterface) *SecureRevocationStore
NewSecureRevocationStore creates a new SecureRevocationStore.
func (*SecureRevocationStore) DeleteToken ¶
func (s *SecureRevocationStore) DeleteToken(ctx context.Context, tokenID string) error
DeleteToken deletes a token by its ID.
func (*SecureRevocationStore) RevokeBatchTokens ¶
func (s *SecureRevocationStore) RevokeBatchTokens(ctx context.Context, tokenIDs []string) (int, error)
RevokeBatchTokens revokes multiple tokens at once.
func (*SecureRevocationStore) RevokeExpiredTokens ¶
func (s *SecureRevocationStore) RevokeExpiredTokens(ctx context.Context) (int, error)
RevokeExpiredTokens revokes all expired tokens.
func (*SecureRevocationStore) RevokeProjectTokens ¶
func (s *SecureRevocationStore) RevokeProjectTokens(ctx context.Context, projectID string) (int, error)
RevokeProjectTokens revokes all tokens for a project.
func (*SecureRevocationStore) RevokeToken ¶
func (s *SecureRevocationStore) RevokeToken(ctx context.Context, tokenID string) error
RevokeToken revokes a token by its ID.
type SecureTokenStore ¶
type SecureTokenStore struct {
// contains filtered or unexported fields
}
SecureTokenStore wraps a TokenStore and hashes tokens before storage. This prevents tokens from being exposed if the database is compromised.
func NewSecureTokenStore ¶
func NewSecureTokenStore(store token.TokenStore, hasher TokenHasherInterface) *SecureTokenStore
NewSecureTokenStore creates a new SecureTokenStore. If hasher is nil, a NullTokenHasher is used (no hashing).
func (*SecureTokenStore) CreateToken ¶
CreateToken creates a new token in the store. The token value is hashed before storage.
func (*SecureTokenStore) GetTokenByID ¶
GetTokenByID retrieves a token by its UUID.
func (*SecureTokenStore) GetTokenByToken ¶
func (s *SecureTokenStore) GetTokenByToken(ctx context.Context, tokenString string) (token.TokenData, error)
GetTokenByToken retrieves a token by its token string (for authentication). The token is hashed before lookup, and the returned TokenData will have the hashed token value (not the original).
func (*SecureTokenStore) GetTokensByProjectID ¶
func (s *SecureTokenStore) GetTokensByProjectID(ctx context.Context, projectID string) ([]token.TokenData, error)
GetTokensByProjectID retrieves all tokens for a project. Note: The returned tokens will have hashed token values.
func (*SecureTokenStore) IncrementTokenUsage ¶
func (s *SecureTokenStore) IncrementTokenUsage(ctx context.Context, tokenString string) error
IncrementTokenUsage increments the usage count for a token by token string. The token is hashed before the operation.
func (*SecureTokenStore) ListTokens ¶
ListTokens retrieves all tokens from the store. Note: The returned tokens will have hashed token values.
func (*SecureTokenStore) UpdateToken ¶
UpdateToken updates an existing token. The token value is hashed before the operation.
type TokenHasher ¶
type TokenHasher struct {
// contains filtered or unexported fields
}
TokenHasher provides secure hashing for authentication tokens. It uses SHA-256 for creating lookup keys and bcrypt for secure storage.
func NewTokenHasher ¶
func NewTokenHasher() *TokenHasher
NewTokenHasher creates a new TokenHasher with the default bcrypt cost.
func NewTokenHasherWithCost ¶
func NewTokenHasherWithCost(cost int) (*TokenHasher, error)
NewTokenHasherWithCost creates a new TokenHasher with a custom bcrypt cost.
func (*TokenHasher) CreateLookupKey ¶
func (h *TokenHasher) CreateLookupKey(token string) string
CreateLookupKey creates a deterministic hash for token lookup. This is used as an index key in the database for finding tokens. Uses SHA-256 which is fast and collision-resistant.
func (*TokenHasher) HashToken ¶
func (h *TokenHasher) HashToken(token string) (string, error)
HashToken creates a bcrypt hash of a token for secure storage. Returns a hash prefixed with HashPrefix for identification. For tokens longer than 72 bytes, a SHA-256 pre-hash is used since bcrypt has a 72-byte input limit.
func (*TokenHasher) VerifyToken ¶
func (h *TokenHasher) VerifyToken(token, hashedToken string) error
VerifyToken compares a plaintext token against a stored hash. It returns nil if the token matches, or ErrHashMismatch if it doesn't.