Skip to content

Commit e566aaf

Browse files
committed
remove redundant bool return value from IsCorrect
1 parent 270165a commit e566aaf

File tree

3 files changed

+8
-22
lines changed

3 files changed

+8
-22
lines changed

apikey.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ func (k *ApiKey) Hash() error {
5454
}
5555

5656
// 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 {
5858
if given == "" {
59-
return false, errors.New("secret to compare cannot be empty")
59+
return errors.New("secret to compare cannot be empty")
6060
}
6161
if k.HashedSecret == "" {
62-
return false, errors.New("cannot compare with empty hashed secret")
62+
return errors.New("cannot compare with empty hashed secret")
6363
}
6464

6565
err := bcrypt.CompareHashAndPassword([]byte(k.HashedSecret), []byte(given))
6666
if err != nil {
67-
return false, err
67+
return err
6868
}
6969

70-
return true, nil
70+
return nil
7171
}
7272

7373
// EncryptData uses the Secret to AES encrypt an arbitrary data block. It does not encrypt the key itself.

apikey_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,18 @@ func TestApiKey_IsCorrect(t *testing.T) {
1717
name string
1818
HashedSecret string
1919
Given string
20-
want bool
2120
wantErr bool
2221
}{
2322
{
2423
name: "valid secret",
2524
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
2625
Given: "abc123",
27-
want: true,
2826
wantErr: false,
2927
},
3028
{
3129
name: "invalid secret",
3230
HashedSecret: "$2y$10$Y.FlUK8q//DfybgFzNG2lONaJwvEFxHnCRo/r60BZbITDT6rOUhGa",
3331
Given: "123abc",
34-
want: false,
3532
wantErr: true,
3633
},
3734
}
@@ -40,14 +37,11 @@ func TestApiKey_IsCorrect(t *testing.T) {
4037
k := &ApiKey{
4138
HashedSecret: tt.HashedSecret,
4239
}
43-
got, err := k.IsCorrect(tt.Given)
40+
err := k.IsCorrect(tt.Given)
4441
if (err != nil) != tt.wantErr {
4542
t.Errorf("IsCorrect() error = %v, wantErr %v", err, tt.wantErr)
4643
return
4744
}
48-
if got != tt.want {
49-
t.Errorf("IsCorrect() got = %v, want %v", got, tt.want)
50-
}
5145
})
5246
}
5347
}
@@ -79,15 +73,11 @@ func TestApiKey_Hash(t *testing.T) {
7973
t.Error("hashed secret is empty after call to hash")
8074
return
8175
}
82-
valid, err := k.IsCorrect(tt.Secret)
76+
err = k.IsCorrect(tt.Secret)
8377
if err != nil {
8478
t.Errorf("hashed password not valid after hashing??? error: %s", err)
8579
return
8680
}
87-
if !valid {
88-
t.Error("hmm, password is not valid but no errors???")
89-
return
90-
}
9181
})
9282
}
9383
}

auth.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@ func AuthenticateRequest(r *http.Request) (User, error) {
4242
return nil, fmt.Errorf("api call attempted for not yet activated key: %s", apiKey.Key)
4343
}
4444

45-
valid, err := apiKey.IsCorrect(secret)
45+
err = apiKey.IsCorrect(secret)
4646
if err != nil {
4747
return nil, fmt.Errorf("failed to validate api key: %w", err)
4848
}
4949

50-
if !valid {
51-
return nil, fmt.Errorf("invalid api secret for key %s", key)
52-
}
53-
5450
path := r.URL.Path
5551
segments := strings.Split(strings.TrimPrefix(path, "/"), "/")
5652
switch segments[0] {

0 commit comments

Comments
 (0)