Skip to content

Commit 67c524e

Browse files
committed
include check for key activation in IsCorrect
1 parent 79d06d7 commit 67c524e

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

apikey.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,16 @@ func (k *ApiKey) Hash() error {
5252
return err
5353
}
5454

55-
// IsCorrect returns true if and only if the given string is a match for HashedSecret
55+
// IsCorrect returns true if and only if the key is active and the given string is a match for HashedSecret
5656
func (k *ApiKey) IsCorrect(given string) error {
57+
if k.ActivatedAt == 0 {
58+
return fmt.Errorf("key is not active: %s", k.Key)
59+
}
60+
5761
if given == "" {
5862
return errors.New("secret to compare cannot be empty")
5963
}
64+
6065
if k.HashedSecret == "" {
6166
return errors.New("cannot compare with empty hashed secret")
6267
}

apikey_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,37 @@ func TestApiKey_IsCorrect(t *testing.T) {
2020
tests := []struct {
2121
name string
2222
HashedSecret string
23+
ActivatedAt int
2324
Given string
2425
wantErr bool
2526
}{
2627
{
2728
name: "valid secret",
2829
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
30+
ActivatedAt: 1744896576000,
2931
Given: "abc123",
3032
wantErr: false,
3133
},
3234
{
3335
name: "invalid secret",
3436
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
37+
ActivatedAt: 1744896576000,
3538
Given: "123abc",
3639
wantErr: true,
3740
},
41+
{
42+
name: "inactive",
43+
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
44+
ActivatedAt: 0,
45+
Given: "abc123",
46+
wantErr: true,
47+
},
3848
}
3949
for _, tt := range tests {
4050
t.Run(tt.name, func(t *testing.T) {
4151
k := &ApiKey{
4252
HashedSecret: tt.HashedSecret,
53+
ActivatedAt: tt.ActivatedAt,
4354
}
4455
err := k.IsCorrect(tt.Given)
4556
if (err != nil) != tt.wantErr {
@@ -66,7 +77,8 @@ func TestApiKey_Hash(t *testing.T) {
6677
for _, tt := range tests {
6778
t.Run(tt.name, func(t *testing.T) {
6879
k := &ApiKey{
69-
Secret: tt.Secret,
80+
Secret: tt.Secret,
81+
ActivatedAt: 1744896576000,
7082
}
7183
err := k.Hash()
7284
if (err != nil) != tt.wantErr {

auth.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ func AuthenticateRequest(r *http.Request) (User, error) {
3838
return nil, fmt.Errorf("failed to load api key: %w", err)
3939
}
4040

41-
if apiKey.ActivatedAt == 0 {
42-
return nil, fmt.Errorf("api call attempted for not yet activated key: %s", apiKey.Key)
43-
}
44-
4541
err = apiKey.IsCorrect(secret)
4642
if err != nil {
4743
return nil, fmt.Errorf("failed to validate api key: %w", err)

0 commit comments

Comments
 (0)