MSP-1069 update middleware admin auth

This commit is contained in:
john committed 2022-10-05 13:49:17 +03:00
1 parent b054257e06
commit 0910add54c
1 file changed
+14 -4
+14 -4
View File
@@ -1,12 +1,15 @@
package middleware
import (
"context"
"net/http"
"strings"
"github.com/pkg/errors"
"isp-gate-service/domain"
"isp-gate-service/httperrors"
"isp-gate-service/request"
"github.com/pkg/errors"
)
const (
@@ -14,7 +17,7 @@ const (
)
type AdminAuthenticator interface {
Authenticate(token string) (int, error)
AdminAuthenticate(ctx context.Context, token string) (*domain.AdminAuthenticateResponse, error)
}
func AdminAuthenticate(auth AdminAuthenticator) Middleware {
@@ -24,7 +27,7 @@ func AdminAuthenticate(auth AdminAuthenticator) Middleware {
if adminToken == "" {
return next.Handle(ctx)
}
adminId, err := auth.Authenticate(adminToken)
resp, err := auth.AdminAuthenticate(ctx.Context(), adminToken)
if err != nil {
return httperrors.New(
http.StatusUnauthorized,
@@ -32,7 +35,14 @@ func AdminAuthenticate(auth AdminAuthenticator) Middleware {
errors.WithMessage(err, "admin authenticate: authenticate"),
)
}
ctx.AuthenticateAdmin(adminId, adminToken)
if !resp.Authenticated {
return httperrors.New(
http.StatusUnauthorized,
"invalid admin token",
errors.WithMessage(errors.New(resp.ErrorReason), "admin authenticate: authenticate"),
)
}
ctx.AuthenticateAdmin(resp.AdminId, adminToken)
return next.Handle(ctx)
})
}