updated hwsauth: uses new hws version

This commit is contained in:
2026-01-23 12:55:44 +11:00
parent 378bd8006d
commit f25bc437c4
4 changed files with 471 additions and 36 deletions

View File

@@ -5,6 +5,7 @@ import (
"time"
"git.haelnorr.com/h/golib/hws"
"github.com/pkg/errors"
)
// LoginReq returns a middleware that requires the user to be authenticated.
@@ -18,23 +19,14 @@ func (auth *Authenticator[T, TX]) LoginReq(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, ok := getAuthorizedModel[T](r.Context())
if !ok {
page, err := auth.errorPage(http.StatusUnauthorized)
err := auth.server.ThrowError(w, r, hws.HWSError{
Error: errors.New("Login required"),
Message: "Please login to view this page",
StatusCode: http.StatusUnauthorized,
RenderErrorPage: true,
})
if err != nil {
auth.server.ThrowError(w, r, hws.HWSError{
Error: err,
Message: "Failed to get valid error page",
StatusCode: http.StatusInternalServerError,
RenderErrorPage: true,
})
}
err = page.Render(r.Context(), w)
if err != nil {
auth.server.ThrowError(w, r, hws.HWSError{
Error: err,
Message: "Failed to render error page",
StatusCode: http.StatusInternalServerError,
RenderErrorPage: true,
})
auth.server.ThrowFatal(w, err)
}
return
}
@@ -74,23 +66,14 @@ func (auth *Authenticator[T, TX]) FreshReq(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
model, ok := getAuthorizedModel[T](r.Context())
if !ok {
page, err := auth.errorPage(http.StatusUnauthorized)
err := auth.server.ThrowError(w, r, hws.HWSError{
Error: errors.New("Login required"),
Message: "Please login to view this page",
StatusCode: http.StatusUnauthorized,
RenderErrorPage: true,
})
if err != nil {
auth.server.ThrowError(w, r, hws.HWSError{
Error: err,
Message: "Failed to get valid error page",
StatusCode: http.StatusInternalServerError,
RenderErrorPage: true,
})
}
err = page.Render(r.Context(), w)
if err != nil {
auth.server.ThrowError(w, r, hws.HWSError{
Error: err,
Message: "Failed to render error page",
StatusCode: http.StatusInternalServerError,
RenderErrorPage: true,
})
auth.server.ThrowFatal(w, err)
}
return
}