2024-01-22 17:22:41: merge [master:56d3e06e49d7a75f142424293788a4f3b6245430];

This commit is contained in:
txix committed 2024-01-22 17:22:41 +03:00
1 parent 7014f56cbb
commit 2b91caece0
60 files changed
+3558

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
package middleware
import (
"context"
"net/http"
"github.com/pkg/errors"
"isp-gate-service/domain"
"isp-gate-service/httperrors"
"isp-gate-service/request"
)
const (
applicationTokenHeader = "x-application-token"
)
type Authenticator interface {
Authenticate(ctx context.Context, token string) (*domain.AuthenticateResponse, error)
}
func Authenticate(authenticator Authenticator) Middleware {
return func(next Handler) Handler {
return HandlerFunc(func(ctx *request.Context) error {
token := ctx.Param(applicationTokenHeader)
if token == "" {
return httperrors.New(
http.StatusUnauthorized,
"application token required",
errors.New("authenticate: application token required"),
)
}
resp, err := authenticator.Authenticate(ctx.Context(), token)
if err != nil {
return errors.WithMessagef(err, "authenticate: authenticate")
}
if !resp.Authenticated {
return httperrors.New(
http.StatusUnauthorized,
"invalid application token",
errors.WithMessage(errors.New(resp.ErrorReason), "authenticate: authenticate"),
)
}
ctx.Authenticate(request.AuthData(*resp.AuthData))
return next.Handle(ctx)
})
}
}