import sources
This commit is contained in:
32
internal/wrpc/handler/basauth.go
Normal file
32
internal/wrpc/handler/basauth.go
Normal file
@@ -0,0 +1,32 @@
|
||||
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()
|
||||
}
|
||||
24
internal/wrpc/handler/handler.go
Normal file
24
internal/wrpc/handler/handler.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"certmanager/internal/logic"
|
||||
"certmanager/pkg/logger"
|
||||
)
|
||||
|
||||
type HandlerConfig struct {
|
||||
Logic *logic.Logic
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
log *logger.Logger
|
||||
lg *logic.Logic
|
||||
}
|
||||
|
||||
func NewHandler(conf *HandlerConfig) (*Handler, error) {
|
||||
var err error
|
||||
hand := &Handler{
|
||||
log: logger.NewLogger("whandler"),
|
||||
lg: conf.Logic,
|
||||
}
|
||||
return hand, err
|
||||
}
|
||||
31
internal/wrpc/handler/status.go
Normal file
31
internal/wrpc/handler/status.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"certmanager/api/certmanagercontrol"
|
||||
"certmanager/pkg/auxhttp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (hand *Handler) GetStatus(gctx *gin.Context) {
|
||||
var err error
|
||||
nReq := &certmanagercontrol.GetStatusParams{}
|
||||
// Bind request
|
||||
err = gctx.ShouldBind(nReq)
|
||||
if err != nil {
|
||||
hand.log.Errorf("Cannot bind: %v", err)
|
||||
auxhttp.SendError(gctx, err)
|
||||
return
|
||||
}
|
||||
// Call logic
|
||||
ctx := gctx.Request.Context()
|
||||
lgRes, err := hand.lg.GetStatus(ctx, nReq)
|
||||
if err != nil {
|
||||
hand.log.Errorf("Got error: %v", err)
|
||||
auxhttp.SendError(gctx, err)
|
||||
return
|
||||
}
|
||||
// Send result
|
||||
nRes := lgRes
|
||||
auxhttp.SendResult(gctx, nRes)
|
||||
}
|
||||
Reference in New Issue
Block a user