mirror of
https://github.com/hanzoai/gateway.git
synced 2026-10-30 15:04:44 +00:00
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]>
61 lines
1.4 KiB
Go
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
|
|
}
|