Files
mstore/app/handler/authmw.go
T
2026-02-07 14:10:54 +02:00

52 lines
1.0 KiB
Go

package handler
import (
//"encoding/base64"
//"fmt"
//"strings"
"mstore/app/router"
"mstore/pkg/auxhttp"
)
const (
authTag = "authpass"
userTag = "username"
)
func (hand *Handler) AuthMiddleware(next router.Handler) router.Handler {
var handlerFunc router.HandlerFunc
handlerFunc = func(rctx *router.Context) {
authSuccessful, authError := hand.CheckAccess(rctx)
if authSuccessful && authError == nil {
rctx.SetBool(authTag, true)
}
if authError != nil {
hand.logg.Errorf("Authorization middleware error: %v", authError)
}
next.ServeHTTP(rctx)
}
return handlerFunc
}
func (hand *Handler) CheckAccess(rctx *router.Context) (bool, error) {
var err error
var res bool
authHeader := rctx.GetHeader("Authorization")
if authHeader != "" {
hand.logg.Debugf("Authorization header is %s", authHeader)
username, password, err := auxhttp.ParseBasicAuth(authHeader)
if err != nil {
return res, err
}
hand.logg.Debugf("Authorization username is %s:%s", username, password)
}
res = true
return res, err
}