47 lines
981 B
Go
47 lines
981 B
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")
|
|
hand.logg.Debugf("Authorization header is %s", authHeader)
|
|
username, password, err := auxhttp.ParseBasicAuth(authHeader)
|
|
hand.logg.Debugf("Authorization username is %s:%s", username, password)
|
|
|
|
res = true
|
|
|
|
return res, err
|
|
}
|