mirror of
https://git.sr.ht/~sircmpwn/tokidoki
synced 2025-12-12 14:17:21 +01:00
Switch to a proper logging library
Structured logs can be enabled with `-log.json`.
This commit is contained in:
parent
03fce79e57
commit
40bae8dc31
9 changed files with 75 additions and 64 deletions
11
auth/imap.go
11
auth/imap.go
|
|
@ -1,13 +1,12 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/emersion/go-imap/client"
|
||||
"github.com/emersion/go-sasl"
|
||||
|
||||
"git.sr.ht/~sircmpwn/tokidoki/debug"
|
||||
)
|
||||
|
||||
type IMAPProvider struct {
|
||||
|
|
@ -20,7 +19,7 @@ func NewIMAP(addr string, tls bool) AuthProvider {
|
|||
prov := &IMAPProvider{addr, tls}
|
||||
conn, err := prov.dial()
|
||||
if err != nil {
|
||||
log.Fatalf("Error dialing configured IMAP auth server: %s", err.Error())
|
||||
log.Fatal().Err(err).Msg("error dialing configured IMAP auth server")
|
||||
}
|
||||
conn.Close()
|
||||
return prov
|
||||
|
|
@ -45,7 +44,7 @@ func (prov *IMAPProvider) doAuth(next http.Handler,
|
|||
|
||||
conn, err := prov.dial()
|
||||
if err != nil {
|
||||
debug.Printf("Auth dial error: %v", err)
|
||||
log.Debug().Err(err).Msg("auth dial error")
|
||||
http.Error(w, "Temporary authentication error, try again later", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
|
@ -53,7 +52,7 @@ func (prov *IMAPProvider) doAuth(next http.Handler,
|
|||
|
||||
auth := sasl.NewPlainClient("", user, pass)
|
||||
if err := conn.Authenticate(auth); 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
|
||||
}
|
||||
|
|
|
|||
13
auth/pam.go
13
auth/pam.go
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue