File tree Expand file tree Collapse file tree 3 files changed +8
-22
lines changed Expand file tree Collapse file tree 3 files changed +8
-22
lines changed Original file line number Diff line number Diff line change @@ -54,20 +54,20 @@ func (k *ApiKey) Hash() error {
54
54
}
55
55
56
56
// IsCorrect returns true if and only if the given string is a match for HashedSecret
57
- func (k * ApiKey ) IsCorrect (given string ) ( bool , error ) {
57
+ func (k * ApiKey ) IsCorrect (given string ) error {
58
58
if given == "" {
59
- return false , errors .New ("secret to compare cannot be empty" )
59
+ return errors .New ("secret to compare cannot be empty" )
60
60
}
61
61
if k .HashedSecret == "" {
62
- return false , errors .New ("cannot compare with empty hashed secret" )
62
+ return errors .New ("cannot compare with empty hashed secret" )
63
63
}
64
64
65
65
err := bcrypt .CompareHashAndPassword ([]byte (k .HashedSecret ), []byte (given ))
66
66
if err != nil {
67
- return false , err
67
+ return err
68
68
}
69
69
70
- return true , nil
70
+ return nil
71
71
}
72
72
73
73
// EncryptData uses the Secret to AES encrypt an arbitrary data block. It does not encrypt the key itself.
Original file line number Diff line number Diff line change @@ -17,21 +17,18 @@ func TestApiKey_IsCorrect(t *testing.T) {
17
17
name string
18
18
HashedSecret string
19
19
Given string
20
- want bool
21
20
wantErr bool
22
21
}{
23
22
{
24
23
name : "valid secret" ,
25
24
HashedSecret : "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa" ,
26
25
Given : "abc123" ,
27
- want : true ,
28
26
wantErr : false ,
29
27
},
30
28
{
31
29
name : "invalid secret" ,
32
30
HashedSecret : "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa" ,
33
31
Given : "123abc" ,
34
- want : false ,
35
32
wantErr : true ,
36
33
},
37
34
}
@@ -40,14 +37,11 @@ func TestApiKey_IsCorrect(t *testing.T) {
40
37
k := & ApiKey {
41
38
HashedSecret : tt .HashedSecret ,
42
39
}
43
- got , err := k .IsCorrect (tt .Given )
40
+ err := k .IsCorrect (tt .Given )
44
41
if (err != nil ) != tt .wantErr {
45
42
t .Errorf ("IsCorrect() error = %v, wantErr %v" , err , tt .wantErr )
46
43
return
47
44
}
48
- if got != tt .want {
49
- t .Errorf ("IsCorrect() got = %v, want %v" , got , tt .want )
50
- }
51
45
})
52
46
}
53
47
}
@@ -79,15 +73,11 @@ func TestApiKey_Hash(t *testing.T) {
79
73
t .Error ("hashed secret is empty after call to hash" )
80
74
return
81
75
}
82
- valid , err : = k .IsCorrect (tt .Secret )
76
+ err = k .IsCorrect (tt .Secret )
83
77
if err != nil {
84
78
t .Errorf ("hashed password not valid after hashing??? error: %s" , err )
85
79
return
86
80
}
87
- if ! valid {
88
- t .Error ("hmm, password is not valid but no errors???" )
89
- return
90
- }
91
81
})
92
82
}
93
83
}
Original file line number Diff line number Diff line change @@ -42,15 +42,11 @@ func AuthenticateRequest(r *http.Request) (User, error) {
42
42
return nil , fmt .Errorf ("api call attempted for not yet activated key: %s" , apiKey .Key )
43
43
}
44
44
45
- valid , err : = apiKey .IsCorrect (secret )
45
+ err = apiKey .IsCorrect (secret )
46
46
if err != nil {
47
47
return nil , fmt .Errorf ("failed to validate api key: %w" , err )
48
48
}
49
49
50
- if ! valid {
51
- return nil , fmt .Errorf ("invalid api secret for key %s" , key )
52
- }
53
-
54
50
path := r .URL .Path
55
51
segments := strings .Split (strings .TrimPrefix (path , "/" ), "/" )
56
52
switch segments [0 ] {
You can’t perform that action at this time.
0 commit comments