2021-01-04 13:52:13 +00:00
|
|
|
package user
|
2020-12-10 15:18:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2023-10-13 14:45:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/http"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2023-03-14 19:20:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2024-09-26 07:14:33 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/senders"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-12-10 15:18:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-04 13:52:13 +00:00
|
|
|
phoneEventPrefix = humanEventPrefix + "phone."
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPhoneChangedType = phoneEventPrefix + "changed"
|
|
|
|
HumanPhoneRemovedType = phoneEventPrefix + "removed"
|
|
|
|
HumanPhoneVerifiedType = phoneEventPrefix + "verified"
|
|
|
|
HumanPhoneVerificationFailedType = phoneEventPrefix + "verification.failed"
|
|
|
|
HumanPhoneCodeAddedType = phoneEventPrefix + "code.added"
|
|
|
|
HumanPhoneCodeSentType = phoneEventPrefix + "code.sent"
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneChangedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
PhoneNumber domain.PhoneNumber `json:"phone,omitempty"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneChangedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
func NewHumanPhoneChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, phone domain.PhoneNumber) *HumanPhoneChangedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPhoneChangedType,
|
|
|
|
),
|
2021-03-19 10:12:56 +00:00
|
|
|
PhoneNumber: phone,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPhoneChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
phoneChangedEvent := &HumanPhoneChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(phoneChangedEvent)
|
2020-12-10 15:18:52 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-5M0pd", "unable to unmarshal human phone changed")
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return phoneChangedEvent, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneRemovedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneRemovedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPhoneRemovedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneRemovedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneRemovedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPhoneRemovedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPhoneRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-11-30 07:57:51 +00:00
|
|
|
return &HumanPhoneRemovedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneVerifiedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
IsPhoneVerified bool `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneVerifiedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneVerifiedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPhoneVerifiedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneVerifiedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneVerifiedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPhoneVerifiedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPhoneVerifiedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneVerifiedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
IsPhoneVerified: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneVerificationFailedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneVerificationFailedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneVerificationFailedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewHumanPhoneVerificationFailedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *HumanPhoneVerificationFailedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneVerificationFailedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPhoneVerificationFailedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPhoneVerificationFailedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneVerificationFailedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneCodeAddedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-10-13 14:45:38 +00:00
|
|
|
Code *crypto.CryptoValue `json:"code,omitempty"`
|
|
|
|
Expiry time.Duration `json:"expiry,omitempty"`
|
|
|
|
CodeReturned bool `json:"code_returned,omitempty"`
|
2024-09-26 07:14:33 +00:00
|
|
|
GeneratorID string `json:"generatorId,omitempty"`
|
2023-10-13 14:45:38 +00:00
|
|
|
TriggeredAtOrigin string `json:"triggerOrigin,omitempty"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneCodeAddedEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneCodeAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-13 14:45:38 +00:00
|
|
|
func (e *HumanPhoneCodeAddedEvent) TriggerOrigin() string {
|
|
|
|
return e.TriggeredAtOrigin
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewHumanPhoneCodeAddedEvent(
|
2020-12-10 15:18:52 +00:00
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
code *crypto.CryptoValue,
|
|
|
|
expiry time.Duration,
|
2024-09-26 07:14:33 +00:00
|
|
|
generatorID string,
|
2023-08-03 04:42:59 +00:00
|
|
|
) *HumanPhoneCodeAddedEvent {
|
2024-09-26 07:14:33 +00:00
|
|
|
return NewHumanPhoneCodeAddedEventV2(ctx, aggregate, code, expiry, false, generatorID)
|
2023-08-03 04:42:59 +00:00
|
|
|
}
|
2024-09-26 07:14:33 +00:00
|
|
|
|
2023-08-03 04:42:59 +00:00
|
|
|
func NewHumanPhoneCodeAddedEventV2(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
code *crypto.CryptoValue,
|
|
|
|
expiry time.Duration,
|
|
|
|
codeReturned bool,
|
2024-09-26 07:14:33 +00:00
|
|
|
generatorID string,
|
2021-01-04 13:52:13 +00:00
|
|
|
) *HumanPhoneCodeAddedEvent {
|
|
|
|
return &HumanPhoneCodeAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPhoneCodeAddedType,
|
|
|
|
),
|
2023-10-13 14:45:38 +00:00
|
|
|
Code: code,
|
|
|
|
Expiry: expiry,
|
|
|
|
CodeReturned: codeReturned,
|
2024-09-26 07:14:33 +00:00
|
|
|
GeneratorID: generatorID,
|
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved
ZITADEL currently selects the instance context based on a HTTP header
(see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and
checks it against the list of instance domains. Let's call it instance
or API domain.
For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in
emails, ...) the requested domain (instance domain) will be used. Let's
call it the public domain.
In cases of proxied setups, all exposed domains (public domains) require
the domain to be managed as instance domain.
This can either be done using the "ExternalDomain" in the runtime config
or via system API, which requires a validation through CustomerPortal on
zitadel.cloud.
# How the Problems Are Solved
- Two new headers / header list are added:
- `InstanceHostHeaders`: an ordered list (first sent wins), which will
be used to match the instance.
(For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader`
and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked
afterwards as well)
- `PublicHostHeaders`: an ordered list (first sent wins), which will be
used as public host / domain. This will be checked against a list of
trusted domains on the instance.
- The middleware intercepts all requests to the API and passes a
`DomainCtx` object with the hosts and protocol into the context
(previously only a computed `origin` was passed)
- HTTP / GRPC server do not longer try to match the headers to instances
themself, but use the passed `http.DomainContext` in their interceptors.
- The `RequestedHost` and `RequestedDomain` from authz.Instance are
removed in favor of the `http.DomainContext`
- When authenticating to or signing out from Console UI, the current
`http.DomainContext(ctx).Origin` (already checked by instance
interceptor for validity) is used to compute and dynamically add a
`redirect_uri` and `post_logout_redirect_uri`.
- Gateway passes all configured host headers (previously only did
`x-zitadel-*`)
- Admin API allows to manage trusted domain
# Additional Changes
None
# Additional Context
- part of #8279
- open topics:
- "single-instance" mode
- Console UI
2024-07-31 15:00:38 +00:00
|
|
|
TriggeredAtOrigin: http.DomainContext(ctx).Origin(),
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func HumanPhoneCodeAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
codeAdded := &HumanPhoneCodeAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(codeAdded)
|
2020-12-10 15:18:52 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-6Ms9d", "unable to unmarshal human phone code added")
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return codeAdded, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type HumanPhoneCodeSentEvent struct {
|
2024-09-26 07:14:33 +00:00
|
|
|
*eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
GeneratorInfo *senders.CodeGeneratorInfo `json:"generatorInfo,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanPhoneCodeSentEvent) SetBaseEvent(event *eventstore.BaseEvent) {
|
|
|
|
e.BaseEvent = event
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneCodeSentEvent) Payload() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *HumanPhoneCodeSentEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-26 07:14:33 +00:00
|
|
|
func NewHumanPhoneCodeSentEvent(ctx context.Context, aggregate *eventstore.Aggregate, generatorInfo *senders.CodeGeneratorInfo) *HumanPhoneCodeSentEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanPhoneCodeSentEvent{
|
2024-09-26 07:14:33 +00:00
|
|
|
BaseEvent: eventstore.NewBaseEventForPush(
|
2020-12-10 15:18:52 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
HumanPhoneCodeSentType,
|
|
|
|
),
|
2024-09-26 07:14:33 +00:00
|
|
|
GeneratorInfo: generatorInfo,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|