File tree Expand file tree Collapse file tree 3 files changed +19
-6
lines changed Expand file tree Collapse file tree 3 files changed +19
-6
lines changed Original file line number Diff line number Diff line change @@ -52,11 +52,16 @@ func (k *ApiKey) Hash() error {
52
52
return err
53
53
}
54
54
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
56
56
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
+
57
61
if given == "" {
58
62
return errors .New ("secret to compare cannot be empty" )
59
63
}
64
+
60
65
if k .HashedSecret == "" {
61
66
return errors .New ("cannot compare with empty hashed secret" )
62
67
}
Original file line number Diff line number Diff line change @@ -20,26 +20,37 @@ func TestApiKey_IsCorrect(t *testing.T) {
20
20
tests := []struct {
21
21
name string
22
22
HashedSecret string
23
+ ActivatedAt int
23
24
Given string
24
25
wantErr bool
25
26
}{
26
27
{
27
28
name : "valid secret" ,
28
29
HashedSecret : "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa" ,
30
+ ActivatedAt : 1744896576000 ,
29
31
Given : "abc123" ,
30
32
wantErr : false ,
31
33
},
32
34
{
33
35
name : "invalid secret" ,
34
36
HashedSecret : "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa" ,
37
+ ActivatedAt : 1744896576000 ,
35
38
Given : "123abc" ,
36
39
wantErr : true ,
37
40
},
41
+ {
42
+ name : "inactive" ,
43
+ HashedSecret : "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa" ,
44
+ ActivatedAt : 0 ,
45
+ Given : "abc123" ,
46
+ wantErr : true ,
47
+ },
38
48
}
39
49
for _ , tt := range tests {
40
50
t .Run (tt .name , func (t * testing.T ) {
41
51
k := & ApiKey {
42
52
HashedSecret : tt .HashedSecret ,
53
+ ActivatedAt : tt .ActivatedAt ,
43
54
}
44
55
err := k .IsCorrect (tt .Given )
45
56
if (err != nil ) != tt .wantErr {
@@ -66,7 +77,8 @@ func TestApiKey_Hash(t *testing.T) {
66
77
for _ , tt := range tests {
67
78
t .Run (tt .name , func (t * testing.T ) {
68
79
k := & ApiKey {
69
- Secret : tt .Secret ,
80
+ Secret : tt .Secret ,
81
+ ActivatedAt : 1744896576000 ,
70
82
}
71
83
err := k .Hash ()
72
84
if (err != nil ) != tt .wantErr {
Original file line number Diff line number Diff line change @@ -38,10 +38,6 @@ func AuthenticateRequest(r *http.Request) (User, error) {
38
38
return nil , fmt .Errorf ("failed to load api key: %w" , err )
39
39
}
40
40
41
- if apiKey .ActivatedAt == 0 {
42
- return nil , fmt .Errorf ("api call attempted for not yet activated key: %s" , apiKey .Key )
43
- }
44
-
45
41
err = apiKey .IsCorrect (secret )
46
42
if err != nil {
47
43
return nil , fmt .Errorf ("failed to validate api key: %w" , err )
You can’t perform that action at this time.
0 commit comments