Files
txix 36699fdc72
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
2026-08-28 15:17:49: merge [main:657d4302881986193395aa831b36df6fd0798d4d];
2026-08-28 15:17:49 +03:00

63 lines
1.5 KiB
Go

package service
import (
"context"
"isp-gate-service/domain"
"isp-gate-service/entity"
"github.com/txix-open/isp-kit/errors"
)
type AdminAuth interface {
Authenticate(ctx context.Context, token string) (*entity.AdminAuthenticateResponse, error)
Authorize(ctx context.Context, adminId int, permission string) (bool, error)
}
type Admin struct {
cache AuthorizationCache
repo AdminAuth
}
func NewAdmin(cache AuthorizationCache, adminAuth AdminAuth) Admin {
return Admin{
cache: cache,
repo: adminAuth,
}
}
func (s Admin) AdminAuthenticate(ctx context.Context, token string) (*domain.AdminAuthenticateResponse, error) {
resp, err := s.repo.Authenticate(ctx, token)
if err != nil {
return nil, errors.WithMessage(err, "get admin token data from admin service")
}
return &domain.AdminAuthenticateResponse{
Authenticated: resp.Authenticated,
ErrorReason: resp.ErrorReason,
AdminId: resp.AdminId,
}, nil
}
func (s Admin) AdminAuthorize(ctx context.Context, adminId int, permission string) (bool, error) {
ok, err := s.cache.Get(ctx, adminId, permission)
if err != nil {
return false, errors.WithMessage(err, "authz cache get")
}
if ok {
return ok, nil
}
ok, err = s.repo.Authorize(ctx, adminId, permission)
if err != nil {
return false, errors.WithMessagef(err, "authz repo authorize")
}
if ok {
err = s.cache.SetAuthorized(ctx, adminId, permission)
if err != nil {
return false, errors.WithMessagef(err, "authz cache set")
}
}
return ok, nil
}