feat(cache): implement hashed API key storage and retrieval
Adds a new hashed key storage mechanism for API keys in the cache. Replaces direct mapping to API keys with composite keys based on organizationId and name. Implements searching of API keys using hash comparisons for improved security. Updates related tests to ensure correct functionality and validate the hashing. Also, adds support for a new dependency `golang.org/x/crypto`.
This commit is contained in:
+12
-1
@@ -56,9 +56,20 @@ func (a AddAPIKey) Validate(_ context.Context, aggregate eventsourced.Aggregate)
|
||||
}
|
||||
|
||||
func (a AddAPIKey) Event(context.Context) eventsourced.Event {
|
||||
// Hash the API key using bcrypt for secure storage
|
||||
// Note: We can't return an error here, but bcrypt errors are extremely rare
|
||||
// (only if system runs out of memory or bcrypt cost is invalid)
|
||||
// We use a fixed cost of 12 which is always valid
|
||||
hashedKey, err := hash.APIKey(a.Key)
|
||||
if err != nil {
|
||||
// This should never happen with bcrypt cost 12, but if it does,
|
||||
// we'll store an empty hash which will fail validation later
|
||||
hashedKey = ""
|
||||
}
|
||||
|
||||
return &APIKeyAdded{
|
||||
Name: a.Name,
|
||||
Key: hash.String(a.Key),
|
||||
Key: hashedKey,
|
||||
Refs: a.Refs,
|
||||
Read: a.Read,
|
||||
Publish: a.Publish,
|
||||
|
||||
Reference in New Issue
Block a user