Files
certmanager/internal/wrpc/handler/basauth.go
Олег Бородин e9d4d1ef07 import sources
2024-07-30 09:49:53 +02:00

33 lines
687 B
Go

package handler
import (
"errors"
"fmt"
"certmanager/pkg/auxhttp"
"github.com/gin-gonic/gin"
)
func (hand *Handler) BasicAuth(gctx *gin.Context) {
var err error
authHeader := gctx.Request.Header.Get("Authorization")
if authHeader == "" {
err = errors.New("Cannot found autentification data")
auxhttp.SendError(gctx, err)
return
}
username, password, err := auxhttp.ParseAuthBasicHeader(authHeader)
if err != nil {
err = fmt.Errorf("Authorization error: %s", err)
auxhttp.SendError(gctx, err)
return
}
if !hand.lg.ValidateUser(username, password) {
err = errors.New("Incorrect autentification data")
auxhttp.SendError(gctx, err)
return
}
gctx.Next()
}