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

+57
View File
@@ -0,0 +1,57 @@
package service
import (
"context"
"isp-gate-service/domain"
"github.com/pkg/errors"
)
type AdminAuth interface {
Authenticate(ctx context.Context, token string) (*domain.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 resp, 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
}
+55
View File
@@ -0,0 +1,55 @@
package service
import (
"context"
"github.com/pkg/errors"
"isp-gate-service/domain"
)
type AuthenticationCache interface {
Get(ctx context.Context, token string) (*domain.AuthData, error)
Set(ctx context.Context, token string, data domain.AuthData) error
}
type AuthenticationRepo interface {
Authenticate(ctx context.Context, token string) (*domain.AuthenticateResponse, error)
}
type Auth struct {
cache AuthenticationCache
repo AuthenticationRepo
}
func NewAuthentication(cache AuthenticationCache, repo AuthenticationRepo) Auth {
return Auth{
cache: cache,
repo: repo,
}
}
func (s Auth) Authenticate(ctx context.Context, token string) (*domain.AuthenticateResponse, error) {
authData, err := s.cache.Get(ctx, token)
if errors.Is(err, domain.ErrAuthenticationCacheMiss) {
resp, err := s.repo.Authenticate(ctx, token)
if err != nil {
return nil, errors.WithMessage(err, "auth repo authenticate")
}
if !resp.Authenticated {
return resp, nil
}
err = s.cache.Set(ctx, token, *resp.AuthData)
if err != nil {
return nil, errors.WithMessage(err, "auth cache set")
}
return resp, nil
}
if err != nil {
return nil, errors.WithMessage(err, "auth cache get")
}
return &domain.AuthenticateResponse{
Authenticated: true,
ErrorReason: "",
AuthData: authData,
}, nil
}
+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
}
+43
View File
@@ -0,0 +1,43 @@
package service
import (
"context"
"time"
"github.com/pkg/errors"
"isp-gate-service/conf"
)
type DailyLimitRepo interface {
Increment(ctx context.Context, applicationId int, today time.Time) (int64, error)
}
type DailyLimit struct {
repo DailyLimitRepo
limits map[int]int64
}
func NewDailyLimit(repo DailyLimitRepo, configs []conf.DailyLimit) DailyLimit {
limits := make(map[int]int64)
for _, config := range configs {
limits[config.ApplicationId] = config.RequestsPerDay
}
return DailyLimit{
repo: repo,
limits: limits,
}
}
func (s DailyLimit) IncrementAndCheck(ctx context.Context, applicationId int) (bool, error) {
max, ok := s.limits[applicationId]
if !ok {
return true, nil
}
newValue, err := s.repo.Increment(ctx, applicationId, time.Now())
if err != nil {
return false, errors.WithMessage(err, "increment")
}
return newValue <= max, nil
}
+47
View File
@@ -0,0 +1,47 @@
package service
import (
"context"
"github.com/pkg/errors"
"isp-gate-service/conf"
"isp-gate-service/domain"
)
type ThrottlingRepo interface {
IsAllowRequestPerSecond(ctx context.Context, applicationId int, rate int) (*domain.RateLimitResult, error)
}
type Throttling struct {
repo ThrottlingRepo
limits map[int]int
}
func NewThrottling(repo ThrottlingRepo, configs []conf.Throttling) Throttling {
limits := make(map[int]int)
for _, config := range configs {
limits[config.ApplicationId] = config.RequestsPerSeconds
}
return Throttling{
repo: repo,
limits: limits,
}
}
func (s Throttling) AllowRateLimit(ctx context.Context, applicationId int) (*domain.RateLimitResult, error) {
rate, ok := s.limits[applicationId]
if !ok {
return &domain.RateLimitResult{
Allow: true,
Remaining: -1,
RetryAfter: -1,
}, nil
}
result, err := s.repo.IsAllowRequestPerSecond(ctx, applicationId, rate)
if err != nil {
return nil, errors.WithMessage(err, "is allow request per second")
}
return result, nil
}