2022-03-29 09:53:19 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-17 06:07:41 +00:00
|
|
|
"errors"
|
2022-03-29 09:53:19 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-11-29 10:57:47 +00:00
|
|
|
"net/url"
|
2022-05-02 15:26:54 +00:00
|
|
|
"strings"
|
2022-03-29 09:53:19 +00:00
|
|
|
|
2022-08-17 06:07:41 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-11-29 10:57:47 +00:00
|
|
|
zitadel_http "github.com/zitadel/zitadel/internal/api/http"
|
2022-08-17 06:07:41 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/i18n"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-29 09:53:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type instanceInterceptor struct {
|
2024-02-28 10:49:57 +00:00
|
|
|
verifier authz.InstanceVerifier
|
|
|
|
headerName, externalDomain string
|
|
|
|
ignoredPrefixes []string
|
|
|
|
translator *i18n.Translator
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
|
|
|
|
2024-02-28 10:49:57 +00:00
|
|
|
func InstanceInterceptor(verifier authz.InstanceVerifier, headerName, externalDomain string, ignoredPrefixes ...string) *instanceInterceptor {
|
2022-03-29 09:53:19 +00:00
|
|
|
return &instanceInterceptor{
|
2022-05-02 15:26:54 +00:00
|
|
|
verifier: verifier,
|
|
|
|
headerName: headerName,
|
2024-02-28 10:49:57 +00:00
|
|
|
externalDomain: externalDomain,
|
2022-05-02 15:26:54 +00:00
|
|
|
ignoredPrefixes: ignoredPrefixes,
|
2022-08-17 06:07:41 +00:00
|
|
|
translator: newZitadelTranslator(),
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *instanceInterceptor) Handler(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 06:07:41 +00:00
|
|
|
a.handleInstance(w, r, next)
|
2022-03-29 09:53:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *instanceInterceptor) HandlerFunc(next http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-08-17 06:07:41 +00: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 09:53:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 06:07:41 +00:00
|
|
|
ctx, err := setInstance(r, a.verifier, a.headerName)
|
|
|
|
if err != nil {
|
2024-02-28 10:49:57 +00:00
|
|
|
err = fmt.Errorf("unable to set instance using origin %s (ExternalDomain is %s): %w", zitadel_http.ComposedOrigin(r.Context()), a.externalDomain, err)
|
|
|
|
zErr := new(zerrors.ZitadelError)
|
|
|
|
if errors.As(err, &zErr) {
|
|
|
|
zErr.SetMessage(a.translator.LocalizeFromRequest(r, zErr.GetMessage(), nil))
|
|
|
|
zErr.Parent = err
|
|
|
|
err = zErr
|
2022-08-17 06:07:41 +00:00
|
|
|
}
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
next.ServeHTTP(w, r)
|
2022-03-29 09:53:19 +00: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 12:44:04 +00:00
|
|
|
host, err := HostFromRequest(r, headerName)
|
2024-02-28 10:49:57 +00:00
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
if err != nil {
|
2024-02-28 10:49:57 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(err, "INST-zWq7X", "Errors.IAM.NotFound")
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
instance, err := verifier.InstanceByHost(authCtx, host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
span.End()
|
|
|
|
return authz.WithInstance(ctx, instance), nil
|
|
|
|
}
|
2022-03-31 09:36:26 +00:00
|
|
|
|
2023-11-29 10:57:47 +00:00
|
|
|
func HostFromRequest(r *http.Request, headerName string) (host string, err error) {
|
2022-03-31 09:36:26 +00:00
|
|
|
if headerName != "host" {
|
2023-11-29 10:57:47 +00:00
|
|
|
return hostFromSpecialHeader(r, headerName)
|
2022-03-31 09:36:26 +00:00
|
|
|
}
|
2023-11-29 10:57:47 +00:00
|
|
|
return hostFromOrigin(r.Context())
|
|
|
|
}
|
|
|
|
|
|
|
|
func hostFromSpecialHeader(r *http.Request, headerName string) (host string, err error) {
|
|
|
|
host = r.Header.Get(headerName)
|
2022-03-31 09:36:26 +00:00
|
|
|
if host == "" {
|
|
|
|
return "", fmt.Errorf("host header `%s` not found", headerName)
|
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|
2022-08-17 06:07:41 +00:00
|
|
|
|
2023-11-29 10:57:47 +00: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 16:53:41 +00:00
|
|
|
host = u.Host
|
2023-11-29 10:57:47 +00:00
|
|
|
if host == "" {
|
|
|
|
err = errors.New("empty host")
|
|
|
|
}
|
|
|
|
return host, err
|
|
|
|
}
|
|
|
|
|
2022-08-17 06:07:41 +00:00
|
|
|
func newZitadelTranslator() *i18n.Translator {
|
2023-12-05 11:12:01 +00:00
|
|
|
translator, err := i18n.NewZitadelTranslator(language.English)
|
2022-08-17 06:07:41 +00:00
|
|
|
logging.OnError(err).Panic("unable to get translator")
|
|
|
|
return translator
|
|
|
|
}
|