added glob matching to auth middleware
This commit is contained in:
@@ -3,10 +3,10 @@ package hwsauth
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"github.com/gobwas/glob"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -23,7 +23,7 @@ func (auth *Authenticator[T, TX]) Authenticate() hws.Middleware {
|
||||
|
||||
func (auth *Authenticator[T, TX]) authenticate() hws.MiddlewareFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) (*http.Request, *hws.HWSError) {
|
||||
if slices.Contains(auth.ignoredPaths, r.URL.Path) {
|
||||
if globTest(r.URL.Path, auth.ignoredPaths) {
|
||||
return r, nil
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
||||
@@ -38,6 +38,7 @@ func (auth *Authenticator[T, TX]) authenticate() hws.MiddlewareFunc {
|
||||
Error: errors.Wrap(err, "auth.beginTx"),
|
||||
}
|
||||
}
|
||||
defer tx.Rollback()
|
||||
// Type assert to TX - safe because user's beginTx should return their TX type
|
||||
txTyped, ok := tx.(TX)
|
||||
if !ok {
|
||||
@@ -49,7 +50,14 @@ func (auth *Authenticator[T, TX]) authenticate() hws.MiddlewareFunc {
|
||||
}
|
||||
model, err := auth.getAuthenticatedUser(txTyped, w, r)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
rberr := tx.Rollback()
|
||||
if rberr != nil {
|
||||
return nil, &hws.HWSError{
|
||||
Message: "Failed rolling back after error",
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Error: errors.Wrap(err, "tx.Rollback"),
|
||||
}
|
||||
}
|
||||
auth.logger.Debug().
|
||||
Str("remote_addr", r.RemoteAddr).
|
||||
Err(err).
|
||||
@@ -62,3 +70,12 @@ func (auth *Authenticator[T, TX]) authenticate() hws.MiddlewareFunc {
|
||||
return newReq, nil
|
||||
}
|
||||
}
|
||||
|
||||
func globTest(testPath string, globs []glob.Glob) bool {
|
||||
for _, g := range globs {
|
||||
if g.Match(testPath) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user