new version

This commit is contained in:
Ivan Rezchikov
2026-07-11 19:04:41 +03:00
parent 3c64666299
commit b7334696e4
16 changed files with 313 additions and 269 deletions
@@ -88,10 +88,7 @@ func redirectURIMatches(redirectURIsJSON json.RawMessage, redirectURI string) bo
}
func verifyClientSecret(stored, provided string) bool {
if stored == "" {
return true
}
if provided == "" {
if stored == "" || provided == "" {
return false
}
if strings.HasPrefix(stored, "$2") {
@@ -268,7 +268,9 @@ func (s *Service) RevokeToken(ctx context.Context, auth ClientAuth, rawToken str
return nil
}
_ = s.oauth.DeleteRefreshTokenByID(ctx, row.ID)
if err := s.oauth.DeleteRefreshTokenByID(ctx, row.ID); err != nil {
return models.ErrInternal
}
return nil
}
@@ -25,6 +25,23 @@ var (
Max: 5,
Window: time.Hour,
}
TokenRateLimit = RateLimitConfig{
Max: 20,
Window: time.Minute,
BlockFor: 5 * time.Minute,
}
RevokeRateLimit = RateLimitConfig{
Max: 30,
Window: time.Minute,
}
RefreshRateLimit = RateLimitConfig{
Max: 30,
Window: time.Minute,
BlockFor: 5 * time.Minute,
}
)
type RateLimiter struct {
@@ -42,7 +59,7 @@ func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig
if cfg.BlockFor > 0 {
blocked, err := r.redis.Exists(ctx, blockKey).Result()
if err != nil {
return true, 0, nil
return false, 0, fmt.Errorf("rate limiter: %w", err)
}
if blocked > 0 {
return false, 0, nil
@@ -53,7 +70,7 @@ func (r *RateLimiter) Allow(ctx context.Context, key string, cfg RateLimitConfig
incrCmd := pipe.Incr(ctx, counterKey)
pipe.Expire(ctx, counterKey, cfg.Window)
if _, err := pipe.Exec(ctx); err != nil {
return true, 0, nil
return false, 0, fmt.Errorf("rate limiter: %w", err)
}
count := int(incrCmd.Val())