MSP-7042 Up authorize

This commit is contained in:
Синельник Тимур Романович committed 2026-01-23 13:42:41 +03:00
1 parent b2ffc15cfa
commit 29cdf42808
4 files changed
+28 -12

No files matched your search

+1
View File
@@ -12,6 +12,7 @@ type AuthenticateResponse struct {
type AuthorizeRequest struct {
ApplicationId int
HttpMethod string
Endpoint string
}
+10 -3
View File
@@ -11,7 +11,7 @@ import (
)
type Authorizer interface {
Authorize(ctx context.Context, applicationId int, endpoint string) (bool, error)
Authorize(ctx context.Context, applicationId int, httpMethod string, endpoint string) (bool, error)
}
func Authorize(authorizer Authorizer, logger log.Logger) Middleware {
@@ -22,7 +22,12 @@ func Authorize(authorizer Authorizer, logger log.Logger) Middleware {
return errors.WithMessage(err, "authorize: get auth data")
}
ok, err := authorizer.Authorize(ctx.Context(), authData.ApplicationId, ctx.Endpoint())
ok, err := authorizer.Authorize(
ctx.Context(),
authData.ApplicationId,
ctx.Request().Method,
ctx.Endpoint(),
)
if err != nil {
return errors.WithMessage(err, "authorize: authorize")
}
@@ -40,7 +45,9 @@ func Authorize(authorizer Authorizer, logger log.Logger) Middleware {
return httperrors.New(
http.StatusForbidden,
"endpoint is not allowed",
errors.Errorf("authorize: endpoint '%s' is not allowed for application '%d'", ctx.Endpoint(), authData.ApplicationId),
errors.Errorf("authorize: endpoint '%s' with http method '%s' is not allowed for application '%d'",
ctx.Endpoint(), ctx.Request().Method, authData.ApplicationId,
),
)
}
+4 -3
View File
@@ -4,9 +4,10 @@ package repository
import (
"context"
"isp-gate-service/domain"
"github.com/pkg/errors"
"github.com/txix-open/isp-kit/grpc/client"
"isp-gate-service/domain"
)
const (
@@ -36,10 +37,10 @@ func (r System) Authenticate(ctx context.Context, token string) (*domain.Authent
return &resp, nil
}
func (r System) Authorize(ctx context.Context, applicationId int, endpoint string) (bool, error) {
func (r System) Authorize(ctx context.Context, req domain.AuthorizeRequest) (bool, error) {
resp := domain.AuthorizeResponse{}
err := r.cli.Invoke(authorize).
JsonRequestBody(domain.AuthorizeRequest{ApplicationId: applicationId, Endpoint: endpoint}).
JsonRequestBody(req).
JsonResponseBody(&resp).
Do(ctx)
if err != nil {
+13 -6
View File
@@ -2,6 +2,8 @@ package service
import (
"context"
"fmt"
"isp-gate-service/domain"
"github.com/pkg/errors"
)
@@ -12,7 +14,7 @@ type AuthorizationCache interface {
}
type AuthorizationRepo interface {
Authorize(ctx context.Context, applicationId int, endpoint string) (bool, error)
Authorize(ctx context.Context, req domain.AuthorizeRequest) (bool, error)
}
type Authorization struct {
@@ -27,21 +29,26 @@ func NewAuthorization(cache AuthorizationCache, repo AuthorizationRepo) Authoriz
}
}
func (s Authorization) Authorize(ctx context.Context, applicationId int, endpoint string) (bool, error) {
ok, err := s.cache.Get(ctx, applicationId, endpoint)
func (s Authorization) Authorize(ctx context.Context, applicationId int, httpMethod string, endpoint string) (bool, error) {
cacheKey := fmt.Sprintf("%s %s", httpMethod, endpoint)
ok, err := s.cache.Get(ctx, applicationId, cacheKey)
if err != nil {
return false, errors.WithMessage(err, "authz cache get")
}
if ok {
return ok, nil
return true, nil
}
ok, err = s.repo.Authorize(ctx, applicationId, endpoint)
ok, err = s.repo.Authorize(ctx, domain.AuthorizeRequest{
ApplicationId: applicationId,
HttpMethod: httpMethod,
Endpoint: endpoint,
})
if err != nil {
return false, errors.WithMessagef(err, "authz repo authorize")
}
if ok {
err = s.cache.SetAuthorized(ctx, applicationId, endpoint)
err = s.cache.SetAuthorized(ctx, applicationId, cacheKey)
if err != nil {
return false, errors.WithMessagef(err, "authz cache set")
}