Files
zitadel/internal/api/http/request_context.go
2a2d5392a3 fix: correctly send links to login v2 in email notifications (#10711)
# Which Problems Are Solved

There were still some emails (passkey registration and domain claimed)
sent with links pointing to login v1 even when the login v2 was enabled
for the instance.
Also while looking into the issue, it was discovered that some links
pointing to login V2 were not correctly generated.


# How the Problems Are Solved

- Added default paths for passkey registration and domain claimed
notifications
- Fixed the existing paths to properly handle concatenation (resp. use
`url.ResolveReference`)
  - Change their go types (from string) to `*url.URL` 
  - Added a mapstructure hook for string to url
- Removed unnecessary `InstanceSetupFeatures` and corresponding
conversions
- Refactored the methods on the `login.DefaultPaths` struct and added an
interface to the `Commands` to only need to pass a single config (and
not every method)
- Added an `OriginURL` method to the `DomainCtx` to prevent going from
url to string and back
- Added the use of the templates in case of enabled login v2 for passkey
registration and domain claimed)

# Additional Changes

None

# Additional Context

closes #10643

---------

Co-authored-by: Max Peintner <max@caos.ch>
Co-authored-by: Livio Spring <livio.a@gmail.com>
Co-authored-by: Livio Spring <livio@zitadel.com>
Co-authored-by: Max Peintner <peintnerm@gmail.com>
Co-authored-by: Gayathri Vijayan <66356931+grvijayan@users.noreply.github.com>
2026-02-13 13:06:56 +00:00

113 lines
2.8 KiB
Go

package http
import (
"context"
"fmt"
"log/slog"
"net"
"net/url"
)
type DomainCtx struct {
InstanceHost string
PublicHost string
Protocol string
}
func NewDomainCtx(instanceHostname, publicHostname, protocol string) *DomainCtx {
return &DomainCtx{
InstanceHost: instanceHostname,
PublicHost: publicHostname,
Protocol: protocol,
}
}
func NewDomainCtxFromOrigin(origin *url.URL) *DomainCtx {
return &DomainCtx{
InstanceHost: origin.Host,
PublicHost: origin.Host,
Protocol: origin.Scheme,
}
}
// InstanceDomain returns the hostname for which the request was handled.
func (r *DomainCtx) InstanceDomain() string {
return hostnameFromHost(r.InstanceHost)
}
func hostnameFromHost(host string) string {
hostname, _, err := net.SplitHostPort(host)
if err != nil {
return host
}
return hostname
}
// RequestedHost returns the host (hostname[:port]) for which the request was handled.
// The instance host is returned if not public host was set.
func (r *DomainCtx) RequestedHost() string {
if r.PublicHost != "" {
return r.PublicHost
}
return r.InstanceHost
}
// RequestedDomain returns the domain (hostname) for which the request was handled.
// The instance domain is returned if no public host / domain was set.
func (r *DomainCtx) RequestedDomain() string {
return hostnameFromHost(r.RequestedHost())
}
// Origin returns the origin (protocol://hostname[:port]) for which the request was handled.
// The instance host is used if no public host was set.
func (r *DomainCtx) Origin() string {
return fmt.Sprintf("%s://%s", r.Protocol, r.RequestedHost())
}
// OriginURL returns the origin (protocol://hostname[:port]) for which the request was handled as [*url.URL].
// The instance host is used if no public host was set.
func (r *DomainCtx) OriginURL() *url.URL {
return &url.URL{
Scheme: r.Protocol,
Host: r.RequestedHost(),
}
}
var _ slog.LogValuer = (*DomainCtx)(nil)
func (r *DomainCtx) LogValue() slog.Value {
values := make([]slog.Attr, 0, 3)
if r.InstanceHost != "" {
values = append(values, slog.String("instance_host", r.InstanceHost))
}
if r.PublicHost != "" {
values = append(values, slog.String("public_host", r.PublicHost))
}
if r.Protocol != "" {
values = append(values, slog.String("protocol", r.Protocol))
}
return slog.GroupValue(values...)
}
func DomainContext(ctx context.Context) *DomainCtx {
o, ok := ctx.Value(domainCtx).(*DomainCtx)
if !ok {
return &DomainCtx{}
}
return o
}
func WithDomainContext(ctx context.Context, domainContext *DomainCtx) context.Context {
return context.WithValue(ctx, domainCtx, domainContext)
}
func WithRequestedHost(ctx context.Context, host string) context.Context {
i, ok := ctx.Value(domainCtx).(*DomainCtx)
if !ok {
i = new(DomainCtx)
}
i.PublicHost = host
return context.WithValue(ctx, domainCtx, i)
}