working commit

This commit is contained in:
2026-02-06 19:15:12 +02:00
parent fe91517e3f
commit f76881a765
14 changed files with 478 additions and 20 deletions
+46
View File
@@ -0,0 +1,46 @@
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
}
+9 -8
View File
@@ -15,21 +15,22 @@ import (
"mstore/app/operator"
"mstore/app/router"
"sigs.k8s.io/yaml"
)
func (hand *Handler) DumpHeaders(message string, rctx *router.Context) {
headers := rctx.GetHeaders()
yamlData, _ := yaml.Marshal(headers)
hand.logg.Debugf("%s:\n%s\n", message, string(yamlData))
}
// HEAD /v2/<name>/blobs/<digest> 200 404
func (hand *Handler) BlobExists(rctx *router.Context) {
name, _ := rctx.GetSubpath("name")
digest, _ := rctx.GetSubpath("digest")
auth := rctx.GetHeader("Authorization")
hand.DumpHeaders("BlobExists", rctx)
if auth == "" {
rctx.SetHeader("WWW-Authenticate", `Basic realm="mstore"`)
rctx.SetStatus(http.StatusUnauthorized)
return
}
params := &operator.BlobExistsParams{
Name: name,
Digest: digest,
+9
View File
@@ -12,6 +12,9 @@ package handler
import (
"mstore/app/logger"
"mstore/app/operator"
"mstore/app/router"
"sigs.k8s.io/yaml"
)
type HandlerParams struct {
@@ -31,3 +34,9 @@ func NewHandler(params *HandlerParams) (*Handler, error) {
hand.logg = logger.NewLoggerWithSubject("handler")
return hand, err
}
func (hand *Handler) DumpHeaders(label string, rctx *router.Context) {
headers := rctx.GetHeaders()
yamlData, _ := yaml.Marshal(headers)
hand.logg.Debugf("%s:\n%s\n", label, string(yamlData))
}
+3
View File
@@ -17,6 +17,9 @@ import (
// GET /v2/ 200 404/401
func (hand *Handler) GetVersion(rctx *router.Context) {
params := &operator.GetVersionParams{}
hand.DumpHeaders("GetVersion", rctx)
ctx := rctx.GetContext()
_, code, err := hand.oper.GetVersion(ctx, params)
if err != nil {