2022-03-29 11:53:19 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-17 08:07:41 +02:00
|
|
|
"errors"
|
2022-03-29 11:53:19 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-11-29 11:57:47 +01:00
|
|
|
"net/url"
|
2022-05-02 17:26:54 +02:00
|
|
|
"strings"
|
2022-03-29 11:53:19 +02:00
|
|
|
|
2022-08-17 08:07:41 +02:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-11-29 11:57:47 +01:00
|
|
|
zitadel_http "github.com/zitadel/zitadel/internal/api/http"
|
2022-08-17 08:07:41 +02:00
|
|
|
caos_errors "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/i18n"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2022-03-29 11:53:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type instanceInterceptor struct {
|
2022-05-02 17:26:54 +02:00
|
|
|
verifier authz.InstanceVerifier
|
|
|
|
headerName string
|
|
|
|
ignoredPrefixes []string
|
2022-08-17 08:07:41 +02:00
|
|
|
translator *i18n.Translator
|
2022-03-29 11:53:19 +02:00
|
|
|
}
|
|
|
|
|
2022-05-02 17:26:54 +02:00
|
|
|
func InstanceInterceptor(verifier authz.InstanceVerifier, headerName string, ignoredPrefixes ...string) *instanceInterceptor {
|
2022-03-29 11:53:19 +02:00
|
|
|
return &instanceInterceptor{
|
2022-05-02 17:26:54 +02:00
|
|
|
verifier: verifier,
|
|
|
|
headerName: headerName,
|
|
|
|
ignoredPrefixes: ignoredPrefixes,
|
2022-08-17 08:07:41 +02:00
|
|
|
translator: newZitadelTranslator(),
|
2022-03-29 11:53:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *instanceInterceptor) Handler(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 08:07:41 +02:00
|
|
|
a.handleInstance(w, r, next)
|
2022-03-29 11:53:19 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *instanceInterceptor) HandlerFunc(next http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 08:07:41 +02:00
|
|
|
a.handleInstance(w, r, next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *instanceInterceptor) handleInstance(w http.ResponseWriter, r *http.Request, next http.Handler) {
|
|
|
|
for _, prefix := range a.ignoredPrefixes {
|
|
|
|
if strings.HasPrefix(r.URL.Path, prefix) {
|
|
|
|
next.ServeHTTP(w, r)
|
2022-03-29 11:53:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 08:07:41 +02:00
|
|
|
ctx, err := setInstance(r, a.verifier, a.headerName)
|
|
|
|
if err != nil {
|
|
|
|
caosErr := new(caos_errors.NotFoundError)
|
|
|
|
if errors.As(err, &caosErr) {
|
|
|
|
caosErr.Message = a.translator.LocalizeFromRequest(r, caosErr.GetMessage(), nil)
|
|
|
|
}
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
next.ServeHTTP(w, r)
|
2022-03-29 11:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func setInstance(r *http.Request, verifier authz.InstanceVerifier, headerName string) (_ context.Context, err error) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
authCtx, span := tracing.NewServerInterceptorSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-06-03 14:44:04 +02:00
|
|
|
host, err := HostFromRequest(r, headerName)
|
2022-03-31 11:36:26 +02:00
|
|
|
if err != nil {
|
2023-11-29 11:57:47 +01:00
|
|
|
return nil, caos_errors.ThrowNotFound(err, "INST-zWq7X", "Errors.Instance.NotFound")
|
2022-03-29 11:53:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
instance, err := verifier.InstanceByHost(authCtx, host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
span.End()
|
|
|
|
return authz.WithInstance(ctx, instance), nil
|
|
|
|
}
|
2022-03-31 11:36:26 +02:00
|
|
|
|
2023-11-29 11:57:47 +01:00
|
|
|
func HostFromRequest(r *http.Request, headerName string) (host string, err error) {
|
2022-03-31 11:36:26 +02:00
|
|
|
if headerName != "host" {
|
2023-11-29 11:57:47 +01:00
|
|
|
return hostFromSpecialHeader(r, headerName)
|
2022-03-31 11:36:26 +02:00
|
|
|
}
|
2023-11-29 11:57:47 +01:00
|
|
|
return hostFromOrigin(r.Context())
|
|
|
|
}
|
|
|
|
|
|
|
|
func hostFromSpecialHeader(r *http.Request, headerName string) (host string, err error) {
|
|
|
|
host = r.Header.Get(headerName)
|
2022-03-31 11:36:26 +02:00
|
|
|
if host == "" {
|
|
|
|
return "", fmt.Errorf("host header `%s` not found", headerName)
|
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|
2022-08-17 08:07:41 +02:00
|
|
|
|
2023-11-29 11:57:47 +01:00
|
|
|
func hostFromOrigin(ctx context.Context) (host string, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("invalid origin: %w", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
origin := zitadel_http.ComposedOrigin(ctx)
|
|
|
|
u, err := url.Parse(origin)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-12-06 18:53:41 +02:00
|
|
|
host = u.Host
|
2023-11-29 11:57:47 +01:00
|
|
|
if host == "" {
|
|
|
|
err = errors.New("empty host")
|
|
|
|
}
|
|
|
|
return host, err
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:07:41 +02:00
|
|
|
func newZitadelTranslator() *i18n.Translator {
|
2023-12-05 12:12:01 +01:00
|
|
|
translator, err := i18n.NewZitadelTranslator(language.English)
|
2022-08-17 08:07:41 +02:00
|
|
|
logging.OnError(err).Panic("unable to get translator")
|
|
|
|
return translator
|
|
|
|
}
|