webhookd/pkg/middleware/authn.go

23 lines
567 B
Go
Raw Normal View History

package middleware
import (
"net/http"
"github.com/ncarlier/webhookd/pkg/auth"
)
// AuthN is a middleware to checks HTTP request credentials
func AuthN(authenticator auth.Authenticator) Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if authenticator.Validate(r) {
next.ServeHTTP(w, r)
return
}
w.Header().Set("WWW-Authenticate", `Basic realm="Ah ah ah, you didn't say the magic word"`)
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
})
}
}