Add role ratelimit (#1)

* Add ratelimit configuration.
* Add rate limiting :party:
This commit is contained in:
2023-10-09 17:46:50 +01:00
parent 743eed7f71
commit ac44056a00
12 changed files with 185 additions and 17 deletions
+14 -3
View File
@@ -6,6 +6,7 @@ import (
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
jsoniter "github.com/json-iterator/go"
libpack_monitoring "github.com/telegram-bot-app/libpack/monitoring"
)
@@ -44,12 +45,22 @@ func processGraphQLRequest(c *fiber.Ctx) error {
t := time.Now()
var extracted_user_id string = "-"
var query_cache_hash string = ""
var extracted_role_name string = "-"
var query_cache_hash string
authorization := c.Request().Header.Peek("Authorization")
if authorization != nil && len(cfg.Client.JWTUserClaimPath) > 0 {
extracted_user_id = extractClaimsFromJWTHeader(string(authorization))
if authorization != nil && (len(cfg.Client.JWTUserClaimPath) > 0 || len(cfg.Client.JWTRoleClaimPath) > 0) {
extracted_user_id, extracted_role_name = extractClaimsFromJWTHeader(string(authorization))
}
if cfg.Client.JWTRoleRateLimit {
cfg.Logger.Debug("Rate limiting enabled", map[string]interface{}{"user_id": extracted_user_id, "role_name": extracted_role_name})
if !rateLimitedRequest(extracted_user_id, extracted_role_name) {
c.Status(429).SendString("Rate limit exceeded, try again later")
return nil
}
}
opType, opName, cache_from_query, should_block := parseGraphQLQuery(c)
if should_block {