2024-04-12 16:41:11 +02:00
|
|
|
package authn
|
|
|
|
|
|
|
|
import (
|
2024-09-24 15:46:42 +02:00
|
|
|
"context"
|
2024-04-12 16:41:11 +02:00
|
|
|
"net/http"
|
|
|
|
|
2024-06-26 13:52:49 +02:00
|
|
|
ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http"
|
2024-10-21 13:48:59 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
2024-04-12 16:41:11 +02:00
|
|
|
"github.com/expr-lang/expr"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
type Vars struct {
|
2024-06-26 13:52:49 +02:00
|
|
|
User *User `expr:"user"`
|
2024-04-12 16:41:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
func (l *Layer) applyRules(ctx context.Context, r *http.Request, layer *store.Layer, options *LayerOptions, user *User) error {
|
|
|
|
key := string(layer.Proxy) + "-" + string(layer.Name)
|
|
|
|
revisionedEngine := l.ruleEngineCache.Get(key)
|
2024-04-12 16:41:11 +02:00
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
engine, err := revisionedEngine.Get(ctx, layer.Revision, options)
|
2024-06-26 13:52:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
vars := &Vars{
|
2024-06-26 13:52:49 +02:00
|
|
|
User: user,
|
2024-04-12 16:41:11 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 15:46:42 +02:00
|
|
|
ctx = ruleHTTP.WithRequest(ctx, r)
|
|
|
|
|
|
|
|
if _, err := engine.Apply(ctx, vars); err != nil {
|
2024-06-26 13:52:49 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-21 12:10:52 +02:00
|
|
|
|
2024-06-26 13:52:49 +02:00
|
|
|
func getAuthnAPI() []expr.Option {
|
|
|
|
options := make([]expr.Option, 0)
|
|
|
|
|
|
|
|
// forbidden() allows the layer to hijack the current request and return a 403 Forbidden HTTP status
|
2024-05-21 12:10:52 +02:00
|
|
|
forbidden := expr.Function(
|
|
|
|
"forbidden",
|
|
|
|
func(params ...any) (any, error) {
|
2024-06-26 13:52:49 +02:00
|
|
|
return true, errors.WithStack(ErrForbidden)
|
2024-05-21 12:10:52 +02:00
|
|
|
},
|
|
|
|
new(func() bool),
|
|
|
|
)
|
|
|
|
|
2024-06-26 13:52:49 +02:00
|
|
|
options = append(options, forbidden)
|
2024-05-21 12:10:52 +02:00
|
|
|
|
2024-06-26 13:52:49 +02:00
|
|
|
return options
|
2024-04-12 16:41:11 +02:00
|
|
|
}
|