Skip to content

Commit

Permalink
refactor: DRY for authentication failure errors (#47)
Browse files Browse the repository at this point in the history
Signed-off-by: jannfis <[email protected]>
  • Loading branch information
jannfis authored Mar 21, 2024
1 parent e135aa8 commit 8096b87
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions internal/auth/userpass/userpass.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package userpass

import (
"bufio"
"errors"
"fmt"
"os"
"regexp"
Expand All @@ -24,6 +25,8 @@ const ClientIDField = "clientid"
// ClientSecretField is the name of the field in the Credentials containing the client secret
const ClientSecretField = "clientsecret"

var errAuthFailed = errors.New("authentication failed")

// UserPassAuthentication implements a simple username/password authentication
// method.
//
Expand Down Expand Up @@ -68,14 +71,14 @@ func (a *UserPassAuthentication) Authenticate(creds auth.Credentials) (clientID
// To make timing attacks a little more complex, we compare the given
// password with our dummy hash.
_ = bcrypt.CompareHashAndPassword(a.dummy, []byte(incomingPassword))
return "", fmt.Errorf("authentication failed")
return "", errAuthFailed
}

if err := bcrypt.CompareHashAndPassword([]byte(realPassword), []byte(incomingPassword)); err == nil {
return incomingUsername, nil
}

return "", fmt.Errorf("authentication failed")
return "", errAuthFailed
}

// UpsertUser adds or updates a user with username and password
Expand Down

0 comments on commit 8096b87

Please sign in to comment.