2024-04-12 16:41:11 +02:00
|
|
|
package authn
|
|
|
|
|
|
|
|
import (
|
2024-09-27 10:09:25 +02:00
|
|
|
"bytes"
|
2024-05-17 17:29:26 +02:00
|
|
|
"html/template"
|
2024-09-27 10:09:25 +02:00
|
|
|
"io"
|
2024-04-12 16:41:11 +02:00
|
|
|
"net/http"
|
2024-05-17 17:29:26 +02:00
|
|
|
"path/filepath"
|
2024-04-12 16:41:11 +02:00
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/go-proxy"
|
|
|
|
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
2024-10-21 13:48:59 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/util"
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/rule"
|
|
|
|
ruleHTTP "forge.cadoles.com/cadoles/bouncer/internal/rule/http"
|
2024-04-12 16:41:11 +02:00
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
2024-05-17 17:29:26 +02:00
|
|
|
"github.com/Masterminds/sprig/v3"
|
2024-04-12 16:41:11 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Layer struct {
|
|
|
|
layerType store.LayerType
|
|
|
|
auth Authenticator
|
2024-06-05 15:46:59 +02:00
|
|
|
debug bool
|
2024-05-17 17:29:26 +02:00
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
ruleEngineCache *util.RuleEngineCache[*Vars, *LayerOptions]
|
|
|
|
|
2024-05-17 17:29:26 +02:00
|
|
|
templateDir string
|
2024-04-12 16:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Layer) Middleware(layer *store.Layer) proxy.Middleware {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
options, err := fromStoreOptions(layer.Options)
|
|
|
|
if err != nil {
|
2024-09-27 10:09:25 +02:00
|
|
|
director.HandleError(ctx, w, r, http.StatusInternalServerError, errors.Wrap(err, "could not parse layer options"))
|
2024-04-12 16:41:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if preAuth, ok := l.auth.(PreAuthentication); ok {
|
|
|
|
if err := preAuth.PreAuthentication(w, r, layer); err != nil {
|
|
|
|
if errors.Is(err, ErrSkipRequest) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-05 15:46:59 +02:00
|
|
|
err = errors.WithStack(err)
|
2024-09-27 10:15:08 +02:00
|
|
|
logger.Error(ctx, "could not execute pre-auth hook", logger.CapturedE(err))
|
2024-06-05 15:46:59 +02:00
|
|
|
l.renderErrorPage(w, r, layer, options, err)
|
2024-04-12 16:41:11 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := wildcard.MatchAny(r.URL.String(), options.MatchURLs...)
|
|
|
|
if !matches {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := l.auth.Authenticate(w, r, layer)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrSkipRequest) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:29:26 +02:00
|
|
|
if errors.Is(err, ErrForbidden) {
|
|
|
|
l.renderForbiddenPage(w, r, layer, options, user)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-05 15:46:59 +02:00
|
|
|
err = errors.WithStack(err)
|
2024-09-27 10:15:08 +02:00
|
|
|
logger.Error(ctx, "could not authenticate user", logger.CapturedE(err))
|
2024-06-05 15:46:59 +02:00
|
|
|
l.renderErrorPage(w, r, layer, options, err)
|
2024-04-12 16:41:11 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:48:59 +02:00
|
|
|
if err := l.applyRules(ctx, r, layer, options, user); err != nil {
|
2024-05-17 17:29:26 +02:00
|
|
|
if errors.Is(err, ErrForbidden) {
|
|
|
|
l.renderForbiddenPage(w, r, layer, options, user)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-05 15:46:59 +02:00
|
|
|
err = errors.WithStack(err)
|
2024-09-27 10:15:08 +02:00
|
|
|
logger.Error(ctx, "could not apply rules", logger.CapturedE(err))
|
2024-06-05 15:46:59 +02:00
|
|
|
l.renderErrorPage(w, r, layer, options, err)
|
2024-04-12 16:41:11 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if postAuth, ok := l.auth.(PostAuthentication); ok {
|
|
|
|
if err := postAuth.PostAuthentication(w, r, layer, user); err != nil {
|
|
|
|
if errors.Is(err, ErrSkipRequest) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:29:26 +02:00
|
|
|
if errors.Is(err, ErrForbidden) {
|
|
|
|
l.renderForbiddenPage(w, r, layer, options, user)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-05 15:46:59 +02:00
|
|
|
err = errors.WithStack(err)
|
2024-09-27 10:15:08 +02:00
|
|
|
logger.Error(ctx, "could not execute post-auth hook", logger.CapturedE(err))
|
2024-06-05 15:46:59 +02:00
|
|
|
l.renderErrorPage(w, r, layer, options, err)
|
2024-04-12 16:41:11 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 15:46:59 +02:00
|
|
|
type baseTemplateData struct {
|
|
|
|
Layer *store.Layer
|
|
|
|
Debug bool
|
|
|
|
Request *http.Request
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:29:26 +02:00
|
|
|
func (l *Layer) renderForbiddenPage(w http.ResponseWriter, r *http.Request, layer *store.Layer, options *LayerOptions, user *User) {
|
2024-06-05 15:46:59 +02:00
|
|
|
templateData := struct {
|
|
|
|
baseTemplateData
|
|
|
|
User *User
|
|
|
|
}{
|
|
|
|
baseTemplateData: baseTemplateData{
|
|
|
|
Layer: layer,
|
|
|
|
Debug: l.debug,
|
|
|
|
Request: r,
|
|
|
|
},
|
|
|
|
User: user,
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:29:26 +02:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2024-06-05 15:46:59 +02:00
|
|
|
l.renderPage(w, r, "forbidden", options.Templates.Forbidden.Block, templateData)
|
2024-05-17 17:29:26 +02:00
|
|
|
}
|
|
|
|
|
2024-06-05 15:46:59 +02:00
|
|
|
func (l *Layer) renderErrorPage(w http.ResponseWriter, r *http.Request, layer *store.Layer, options *LayerOptions, err error) {
|
|
|
|
templateData := struct {
|
|
|
|
baseTemplateData
|
|
|
|
Err error
|
|
|
|
}{
|
|
|
|
baseTemplateData: baseTemplateData{
|
|
|
|
Layer: layer,
|
|
|
|
Debug: l.debug,
|
|
|
|
Request: r,
|
|
|
|
},
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
l.renderPage(w, r, "error", options.Templates.Error.Block, templateData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Layer) renderPage(w http.ResponseWriter, r *http.Request, page string, block string, templateData any) {
|
2024-05-17 17:29:26 +02:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
pattern := filepath.Join(l.templateDir, page+".gohtml")
|
|
|
|
|
|
|
|
logger.Info(ctx, "loading authn templates", logger.F("pattern", pattern))
|
|
|
|
|
|
|
|
tmpl, err := template.New("").Funcs(sprig.FuncMap()).ParseGlob(pattern)
|
|
|
|
if err != nil {
|
2024-09-27 10:09:25 +02:00
|
|
|
director.HandleError(ctx, w, r, http.StatusInternalServerError, errors.Wrap(err, "could not load authn templates"))
|
2024-05-17 17:29:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Cache-Control", "no-cache")
|
|
|
|
|
2024-09-27 10:09:25 +02:00
|
|
|
var buf bytes.Buffer
|
2024-05-17 17:29:26 +02:00
|
|
|
|
2024-09-27 10:09:25 +02:00
|
|
|
if err := tmpl.ExecuteTemplate(w, block, templateData); err != nil {
|
|
|
|
director.HandleError(ctx, w, r, http.StatusInternalServerError, errors.Wrap(err, "could not render authn page"))
|
2024-05-17 17:29:26 +02:00
|
|
|
return
|
|
|
|
}
|
2024-09-27 10:09:25 +02:00
|
|
|
|
|
|
|
if _, err := io.Copy(w, &buf); err != nil {
|
|
|
|
logger.Error(ctx, "could not write authn page", logger.CapturedE(errors.WithStack(err)))
|
|
|
|
}
|
2024-05-17 17:29:26 +02:00
|
|
|
}
|
|
|
|
|
2024-04-12 16:41:11 +02:00
|
|
|
// LayerType implements director.MiddlewareLayer
|
|
|
|
func (l *Layer) LayerType() store.LayerType {
|
|
|
|
return l.layerType
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:29:26 +02:00
|
|
|
func NewLayer(layerType store.LayerType, auth Authenticator, funcs ...OptionFunc) *Layer {
|
|
|
|
opts := NewOptions(funcs...)
|
|
|
|
|
2024-04-12 16:41:11 +02:00
|
|
|
return &Layer{
|
2024-10-21 13:48:59 +02:00
|
|
|
ruleEngineCache: util.NewInMemoryRuleEngineCache[*Vars, *LayerOptions](func(options *LayerOptions) (*rule.Engine[*Vars], error) {
|
|
|
|
engine, err := rule.NewEngine[*Vars](
|
|
|
|
rule.WithRules(options.Rules...),
|
|
|
|
rule.WithExpr(getAuthnAPI()...),
|
|
|
|
ruleHTTP.WithRequestFuncs(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return engine, nil
|
|
|
|
}),
|
2024-05-17 17:29:26 +02:00
|
|
|
layerType: layerType,
|
|
|
|
auth: auth,
|
|
|
|
templateDir: opts.TemplateDir,
|
2024-06-05 15:46:59 +02:00
|
|
|
debug: opts.Debug,
|
2024-04-12 16:41:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ director.MiddlewareLayer = &Layer{}
|