4468903535
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`.
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package domain
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gitlab.com/unboundsoftware/schemas/hash"
|
|
)
|
|
|
|
func TestAddAPIKey_Event(t *testing.T) {
|
|
type fields struct {
|
|
Name string
|
|
Key string
|
|
Refs []string
|
|
Read bool
|
|
Publish bool
|
|
Initiator string
|
|
}
|
|
type args struct {
|
|
in0 context.Context
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
}{
|
|
{
|
|
name: "event",
|
|
fields: fields{
|
|
Name: "test",
|
|
Key: "us_ak_1234567890123456",
|
|
Refs: []string{"Example@dev"},
|
|
Read: true,
|
|
Publish: true,
|
|
Initiator: "jim@example.org",
|
|
},
|
|
args: args{},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
a := AddAPIKey{
|
|
Name: tt.fields.Name,
|
|
Key: tt.fields.Key,
|
|
Refs: tt.fields.Refs,
|
|
Read: tt.fields.Read,
|
|
Publish: tt.fields.Publish,
|
|
Initiator: tt.fields.Initiator,
|
|
}
|
|
event := a.Event(tt.args.in0)
|
|
require.NotNil(t, event)
|
|
|
|
// Cast to APIKeyAdded to verify fields
|
|
apiKeyEvent, ok := event.(*APIKeyAdded)
|
|
require.True(t, ok, "Event should be *APIKeyAdded")
|
|
|
|
// Verify non-key fields match exactly
|
|
assert.Equal(t, tt.fields.Name, apiKeyEvent.Name)
|
|
assert.Equal(t, tt.fields.Refs, apiKeyEvent.Refs)
|
|
assert.Equal(t, tt.fields.Read, apiKeyEvent.Read)
|
|
assert.Equal(t, tt.fields.Publish, apiKeyEvent.Publish)
|
|
assert.Equal(t, tt.fields.Initiator, apiKeyEvent.Initiator)
|
|
|
|
// Verify the key is hashed correctly (bcrypt format)
|
|
assert.True(t, strings.HasPrefix(apiKeyEvent.Key, "$2"), "Key should be bcrypt hashed")
|
|
assert.NotEqual(t, tt.fields.Key, apiKeyEvent.Key, "Key should be hashed, not plaintext")
|
|
|
|
// Verify the hash matches the original key
|
|
assert.True(t, hash.CompareAPIKey(apiKeyEvent.Key, tt.fields.Key), "Hashed key should match original")
|
|
})
|
|
}
|
|
}
|