Log errors returned from graphql (#3562)

* Add func methods to logger
* Log errors returned from the graphql interface
* Log authentication
* Log when credentials changed
This commit is contained in:
WithoutPants
2023-04-17 15:27:25 +10:00
committed by GitHub
parent 75f22042b7
commit 32cefea524
8 changed files with 177 additions and 4 deletions

View File

@@ -34,7 +34,15 @@ const (
passwordFormKey = "password"
)
var ErrInvalidCredentials = errors.New("invalid username or password")
type InvalidCredentialsError struct {
Username string
}
func (e InvalidCredentialsError) Error() string {
// don't leak the username
return "invalid credentials"
}
var ErrUnauthorized = errors.New("unauthorized")
type Store struct {
@@ -63,9 +71,12 @@ func (s *Store) Login(w http.ResponseWriter, r *http.Request) error {
// authenticate the user
if !s.config.ValidateCredentials(username, password) {
return ErrInvalidCredentials
return &InvalidCredentialsError{Username: username}
}
// since we only have one user, don't leak the name
logger.Info("User logged in")
newSession.Values[userIDKey] = username
err := newSession.Save(r, w)
@@ -90,6 +101,9 @@ func (s *Store) Logout(w http.ResponseWriter, r *http.Request) error {
return err
}
// since we only have one user, don't leak the name
logger.Infof("User logged out")
return nil
}