Switch to a proper logging library

Structured logs can be enabled with `-log.json`.
This commit is contained in:
Conrad Hoffmann 2022-12-01 12:00:42 +01:00
parent 03fce79e57
commit 40bae8dc31
9 changed files with 75 additions and 64 deletions

View file

@ -7,8 +7,7 @@ import (
"net/http"
"github.com/msteinert/pam"
"git.sr.ht/~sircmpwn/tokidoki/debug"
"github.com/rs/zerolog/log"
)
type pamProvider struct{}
@ -34,7 +33,7 @@ func pamAuth(next http.Handler, w http.ResponseWriter, r *http.Request) {
}
t, err := pam.StartFunc("login", user, func(s pam.Style, msg string) (string, error) {
debug.Printf("%v %v", s, msg)
log.Debug().Str("style", fmt.Sprintf("%v", s)).Msg(msg)
switch s {
case pam.PromptEchoOff:
return pass, nil
@ -45,26 +44,26 @@ func pamAuth(next http.Handler, w http.ResponseWriter, r *http.Request) {
}
})
if err != nil {
debug.Printf("Failed to start PAM conversation: %v", err)
log.Debug().Err(err).Msg("failed to start PAM conversation")
http.Error(w, "Temporary authentication error, try again later", http.StatusServiceUnavailable)
return
}
if err := t.Authenticate(0); err != nil {
debug.Printf("Auth error: %v", err)
log.Debug().Err(err).Msg("auth error")
http.Error(w, "Invalid username or password", http.StatusUnauthorized)
return
}
if err := t.AcctMgmt(0); err != nil {
debug.Printf("Account unavailable: %v", err)
log.Debug().Err(err).Msg("account unavailable")
http.Error(w, "Account unavailable", http.StatusUnauthorized)
return
}
user, err = t.GetItem(pam.User)
if err != nil {
debug.Printf("Failed to get PAM username: %v", err)
log.Debug().Err(err).Msg("failed to get PAM username")
http.Error(w, "Temporary authentication error, try again later", http.StatusServiceUnavailable)
return
}