2024-01-22 17:22:41: merge [master:56d3e06e49d7a75f142424293788a4f3b6245430];
This commit is contained in:
1 parent
7014f56cbb
commit
2b91caece0
60 files changed
+3558
No files matched your search
@@ -0,0 +1,47 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"isp-gate-service/domain"
|
||||
|
||||
"github.com/integration-system/isp-kit/grpc/client"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
adminAuthenticate = "admin/secure/authenticate"
|
||||
adminAuthorize = "admin/secure/authorize"
|
||||
)
|
||||
|
||||
type Admin struct {
|
||||
cli *client.Client
|
||||
}
|
||||
|
||||
func NewAdmin(cli *client.Client) Admin {
|
||||
return Admin{cli: cli}
|
||||
}
|
||||
|
||||
func (r Admin) Authenticate(ctx context.Context, token string) (*domain.AdminAuthenticateResponse, error) {
|
||||
resp := domain.AdminAuthenticateResponse{}
|
||||
err := r.cli.Invoke(adminAuthenticate).
|
||||
JsonRequestBody(domain.AuthenticateRequest{Token: token}).
|
||||
JsonResponseBody(&resp).
|
||||
Do(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "grpc client invoke %s", adminAuthenticate)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (r Admin) Authorize(ctx context.Context, adminId int, permission string) (bool, error) {
|
||||
resp := domain.AdminAuthorizeResponse{}
|
||||
err := r.cli.Invoke(adminAuthorize).
|
||||
JsonRequestBody(domain.AdminAuthorizeRequest{AdminId: adminId, Permission: permission}).
|
||||
JsonResponseBody(&resp).
|
||||
Do(ctx)
|
||||
if err != nil {
|
||||
return false, errors.WithMessagef(err, "grpc client invoke: %s", adminAuthorize)
|
||||
}
|
||||
return resp.Authorized, nil
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/integration-system/isp-kit/json"
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/cache"
|
||||
"isp-gate-service/domain"
|
||||
)
|
||||
|
||||
type AuthenticationCache struct {
|
||||
cache *cache.Cache
|
||||
duration time.Duration
|
||||
}
|
||||
|
||||
func NewAuthenticationCache(duration time.Duration) AuthenticationCache {
|
||||
return AuthenticationCache{
|
||||
duration: duration,
|
||||
cache: cache.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (r AuthenticationCache) Get(ctx context.Context, token string) (*domain.AuthData, error) {
|
||||
data, ok := r.cache.Get(token)
|
||||
if !ok {
|
||||
return nil, domain.ErrAuthenticationCacheMiss
|
||||
}
|
||||
|
||||
result := domain.AuthData{}
|
||||
err := json.Unmarshal(data, &result)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "json unmarshal auth data")
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (r AuthenticationCache) Set(ctx context.Context, token string, data domain.AuthData) error {
|
||||
value, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "json marshal auth data")
|
||||
}
|
||||
|
||||
r.cache.Set(token, value, r.duration)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"isp-gate-service/cache"
|
||||
)
|
||||
|
||||
type AuthorizationCache struct {
|
||||
duration time.Duration
|
||||
cache *cache.Cache
|
||||
}
|
||||
|
||||
func NewAuthorizationCache(duration time.Duration) *AuthorizationCache {
|
||||
return &AuthorizationCache{
|
||||
duration: duration,
|
||||
cache: cache.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (r AuthorizationCache) Get(ctx context.Context, id int, endpoint string) (bool, error) {
|
||||
key := r.key(id, endpoint)
|
||||
_, ok := r.cache.Get(key)
|
||||
return ok, nil
|
||||
}
|
||||
|
||||
func (r AuthorizationCache) SetAuthorized(ctx context.Context, id int, endpoint string) error {
|
||||
key := r.key(id, endpoint)
|
||||
r.cache.Set(key, nil, r.duration)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r AuthorizationCache) key(id int, endpoint string) string {
|
||||
return fmt.Sprintf("%d:%s", id, endpoint)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type DailyLimit struct {
|
||||
cli redis.UniversalClient
|
||||
}
|
||||
|
||||
func NewDailyLimit(cli redis.UniversalClient) DailyLimit {
|
||||
return DailyLimit{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
func (r DailyLimit) Increment(ctx context.Context, applicationId int, today time.Time) (int64, error) {
|
||||
key := r.key(applicationId, today)
|
||||
value, err := r.cli.Incr(ctx, key).Result()
|
||||
if err != nil {
|
||||
return 0, errors.WithMessage(err, "incr")
|
||||
}
|
||||
|
||||
if value == 1 {
|
||||
err := r.cli.ExpireNX(ctx, key, 24*time.Hour).Err() //nolint:gomnd
|
||||
if err != nil {
|
||||
return 0, errors.WithMessage(err, "expire nx")
|
||||
}
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (r DailyLimit) key(applicationId int, today time.Time) string {
|
||||
y, m, d := today.Date()
|
||||
return fmt.Sprintf("daily_limit:%d:%d-%d-%d", applicationId, y, m, d)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/integration-system/isp-kit/grpc/client"
|
||||
"github.com/pkg/errors"
|
||||
"isp-gate-service/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
authenticate = "system/secure/authenticate"
|
||||
authorize = "system/secure/authorize"
|
||||
)
|
||||
|
||||
type System struct {
|
||||
cli *client.Client
|
||||
}
|
||||
|
||||
func NewSystem(cli *client.Client) System {
|
||||
return System{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
func (r System) Authenticate(ctx context.Context, token string) (*domain.AuthenticateResponse, error) {
|
||||
resp := domain.AuthenticateResponse{}
|
||||
err := r.cli.Invoke(authenticate).
|
||||
JsonRequestBody(domain.AuthenticateRequest{Token: token}).
|
||||
JsonResponseBody(&resp).
|
||||
Do(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessagef(err, "grpc client invoke: %s", authenticate)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (r System) Authorize(ctx context.Context, applicationId int, endpoint string) (bool, error) {
|
||||
resp := domain.AuthorizeResponse{}
|
||||
err := r.cli.Invoke(authorize).
|
||||
JsonRequestBody(domain.AuthorizeRequest{ApplicationId: applicationId, Endpoint: endpoint}).
|
||||
JsonResponseBody(&resp).
|
||||
Do(ctx)
|
||||
if err != nil {
|
||||
return false, errors.WithMessagef(err, "grpc client invoke: %s", authorize)
|
||||
}
|
||||
return resp.Authorized, nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-redis/redis_rate/v10"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"isp-gate-service/domain"
|
||||
)
|
||||
|
||||
type Throttling struct {
|
||||
cli *redis_rate.Limiter
|
||||
}
|
||||
|
||||
func NewThrottling(cli redis.UniversalClient) Throttling {
|
||||
return Throttling{
|
||||
cli: redis_rate.NewLimiter(cli),
|
||||
}
|
||||
}
|
||||
|
||||
func (r Throttling) IsAllowRequestPerSecond(ctx context.Context, applicationId int, rate int) (*domain.RateLimitResult, error) {
|
||||
result, err := r.cli.Allow(ctx, r.key(applicationId), redis_rate.PerSecond(rate))
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "redis_rate/Allow")
|
||||
}
|
||||
return &domain.RateLimitResult{
|
||||
Allow: result.Allowed > 0,
|
||||
Remaining: result.Remaining,
|
||||
RetryAfter: result.RetryAfter,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r Throttling) key(applicationId int) string {
|
||||
return fmt.Sprintf("throttling:%d", applicationId)
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package unused
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/integration-system/isp-kit/json"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"isp-gate-service/domain"
|
||||
)
|
||||
|
||||
type RedisAuthCache struct {
|
||||
cli redis.UniversalClient
|
||||
duration time.Duration
|
||||
db int
|
||||
}
|
||||
|
||||
func NewRedisAuthCache(cli redis.UniversalClient, duration time.Duration, db int) RedisAuthCache {
|
||||
return RedisAuthCache{
|
||||
cli: cli,
|
||||
duration: duration,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r RedisAuthCache) Get(ctx context.Context, token string) (*domain.AuthData, error) {
|
||||
results, err := r.cli.Pipelined(ctx, func(p redis.Pipeliner) error {
|
||||
p.Select(ctx, r.db)
|
||||
p.Get(ctx, token)
|
||||
return nil
|
||||
})
|
||||
if err != nil && err != redis.Nil {
|
||||
return nil, errors.WithMessage(err, "pipelined")
|
||||
}
|
||||
|
||||
data, err := results[1].(*redis.StringCmd).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, domain.ErrAuthenticationCacheMiss
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "get")
|
||||
}
|
||||
|
||||
result := domain.AuthData{}
|
||||
err = json.Unmarshal([]byte(data), &result)
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "json unmarshal auth data")
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (r RedisAuthCache) Set(ctx context.Context, token string, data domain.AuthData) error {
|
||||
value, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "json marshal auth data")
|
||||
}
|
||||
|
||||
results, err := r.cli.Pipelined(ctx, func(p redis.Pipeliner) error {
|
||||
p.Select(ctx, r.db)
|
||||
p.SetEx(ctx, token, string(value), r.duration)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "pipelined")
|
||||
}
|
||||
|
||||
err = results[1].Err()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "set ex")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package unused
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type RedisAuthzCache struct {
|
||||
cli redis.UniversalClient
|
||||
duration time.Duration
|
||||
db int
|
||||
}
|
||||
|
||||
func NewRedisAuthzCache(cli redis.UniversalClient, duration time.Duration, db int) *RedisAuthzCache {
|
||||
return &RedisAuthzCache{
|
||||
cli: cli,
|
||||
duration: duration,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r RedisAuthzCache) Get(ctx context.Context, applicationId int, endpoint string) (bool, error) {
|
||||
result, err := r.cli.Pipelined(ctx, func(p redis.Pipeliner) error {
|
||||
p.Select(ctx, r.db)
|
||||
p.Get(ctx, r.key(applicationId, endpoint))
|
||||
return nil
|
||||
})
|
||||
if err != nil && err != redis.Nil {
|
||||
return false, errors.WithMessage(err, "pipelined")
|
||||
}
|
||||
|
||||
_, err = result[1].(*redis.StringCmd).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "get")
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (r RedisAuthzCache) SetAuthorized(ctx context.Context, applicationId int, endpoint string) error {
|
||||
result, err := r.cli.Pipelined(ctx, func(p redis.Pipeliner) error {
|
||||
p.Select(ctx, r.db)
|
||||
p.SetEx(ctx, r.key(applicationId, endpoint), "authorized", r.duration)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "pipelined")
|
||||
}
|
||||
|
||||
err = result[1].Err()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "set ex")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r RedisAuthzCache) key(applicationId int, endpoint string) string {
|
||||
return fmt.Sprintf("%d:%s", applicationId, endpoint)
|
||||
}
|
||||
Reference in new issue
Block a user