Files
zeekayandHanzo Dev a5c5cc019e Go 1.27.1, and the code it can say more simply
Modules declare go 1.27.1 and the build images follow. go fix applied the
modernizers; omitzero is not applied, since it changes what the JSON says.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-16 22:42:15 -07:00

61 lines
1.4 KiB
Go

package cel
import (
"fmt"
"github.com/google/cel-go/cel"
"github.com/hanzoai/gateway/v2/internal/lura/config"
"github.com/hanzoai/gateway/v2/internal/lura/logging"
"github.com/hanzoai/gateway/v2/internal/plugin/cel/internal"
)
func NewRejecter(l logging.Logger, cfg *config.EndpointConfig) *Rejecter {
logPrefix := "[ENDPOINT: " + cfg.Endpoint + "][CEL]"
def, ok := internal.ConfigGetter(cfg.ExtraConfig)
if !ok {
return nil
}
p := internal.NewCheckExpressionParser(l)
evaluators, err := p.ParseJWT(def)
if err != nil {
l.Debug(logPrefix, "Error building the JWT rejecter:", err.Error())
return nil
}
return &Rejecter{
name: logPrefix,
evaluators: evaluators,
logger: l,
}
}
type Rejecter struct {
name string
evaluators []cel.Program
logger logging.Logger
}
func (r *Rejecter) Reject(data map[string]any) bool {
now := timeNow().Format("2006-01-02T15:04:05.999Z07:00")
reqActivation := map[string]any{
internal.JwtKey: data,
internal.NowKey: now,
}
for i, eval := range r.evaluators {
res, _, err := eval.Eval(reqActivation)
if err != nil {
r.logger.Info(fmt.Sprintf("%s Rejecter #%d failed: %v", r.name, i, res))
return true
}
resultMsg := fmt.Sprintf("%s Rejecter #%d result: %v", r.name, i, res)
if v, ok := res.Value().(bool); !ok || !v {
r.logger.Info(resultMsg)
return true
}
r.logger.Debug(resultMsg)
}
return false
}