zitadel/internal/notification/handlers/origin.go
Tim Möhlmann f680dd934d
refactor: rename package errors to zerrors (#7039)
* chore: rename package errors to zerrors

* rename package errors to gerrors

* fix error related linting issues

* fix zitadel error assertion

* fix gosimple linting issues

* fix deprecated linting issues

* resolve gci linting issues

* fix import structure

---------

Co-authored-by: Elio Bischof <elio@zitadel.com>
2023-12-08 15:30:55 +01:00

61 lines
1.5 KiB
Go

package handlers
import (
"context"
"net/url"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/api/authz"
http_utils "github.com/zitadel/zitadel/internal/api/http"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/query"
"github.com/zitadel/zitadel/internal/zerrors"
)
type OriginEvent interface {
eventstore.Event
TriggerOrigin() string
}
func (n *NotificationQueries) Origin(ctx context.Context, e eventstore.Event) (context.Context, error) {
var origin string
originEvent, ok := e.(OriginEvent)
if !ok {
logging.Errorf("event of type %T doesn't implement OriginEvent", e)
} else {
origin = originEvent.TriggerOrigin()
}
if origin != "" {
originURL, err := url.Parse(origin)
if err != nil {
return ctx, err
}
return enrichCtx(ctx, originURL.Hostname(), origin), nil
}
primary, err := query.NewInstanceDomainPrimarySearchQuery(true)
if err != nil {
return ctx, err
}
domains, err := n.SearchInstanceDomains(ctx, &query.InstanceDomainSearchQueries{
Queries: []query.SearchQuery{primary},
})
if err != nil {
return ctx, err
}
if len(domains.Domains) < 1 {
return ctx, zerrors.ThrowInternal(nil, "NOTIF-Ef3r1", "Errors.Notification.NoDomain")
}
return enrichCtx(
ctx,
domains.Domains[0].Domain,
http_utils.BuildHTTP(domains.Domains[0].Domain, n.externalPort, n.externalSecure),
), nil
}
func enrichCtx(ctx context.Context, host, origin string) context.Context {
ctx = authz.WithRequestedDomain(ctx, host)
ctx = http_utils.WithComposedOrigin(ctx, origin)
return ctx
}