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

+51
View File
@@ -0,0 +1,51 @@
package service
import (
"context"
"github.com/pkg/errors"
)
type AuthorizationCache interface {
Get(ctx context.Context, applicationId int, endpoint string) (bool, error)
SetAuthorized(ctx context.Context, applicationId int, endpoint string) error
}
type AuthorizationRepo interface {
Authorize(ctx context.Context, applicationId int, endpoint string) (bool, error)
}
type Authorization struct {
cache AuthorizationCache
repo AuthorizationRepo
}
func NewAuthorization(cache AuthorizationCache, repo AuthorizationRepo) Authorization {
return Authorization{
cache: cache,
repo: repo,
}
}
func (s Authorization) Authorize(ctx context.Context, applicationId int, endpoint string) (bool, error) {
ok, err := s.cache.Get(ctx, applicationId, endpoint)
if err != nil {
return false, errors.WithMessage(err, "authz cache get")
}
if ok {
return ok, nil
}
ok, err = s.repo.Authorize(ctx, applicationId, endpoint)
if err != nil {
return false, errors.WithMessagef(err, "authz repo authorize")
}
if ok {
err = s.cache.SetAuthorized(ctx, applicationId, endpoint)
if err != nil {
return false, errors.WithMessagef(err, "authz cache set")
}
}
return ok, nil
}