2021-01-04 14:52:13 +01:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 14:48:27 +01:00
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
otpEventPrefix = mfaEventPrefix + "otp."
|
|
|
|
HumanMFAOTPAddedType = otpEventPrefix + "added"
|
|
|
|
HumanMFAOTPVerifiedType = otpEventPrefix + "verified"
|
|
|
|
HumanMFAOTPRemovedType = otpEventPrefix + "removed"
|
|
|
|
HumanMFAOTPCheckSucceededType = otpEventPrefix + "check.succeeded"
|
|
|
|
HumanMFAOTPCheckFailedType = otpEventPrefix + "check.failed"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HumanOTPAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Secret *crypto.CryptoValue `json:"otpSecret,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanOTPAddedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *HumanOTPAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *HumanOTPAddedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewHumanOTPAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
secret *crypto.CryptoValue,
|
|
|
|
) *HumanOTPAddedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &HumanOTPAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-04 14:52:13 +01:00
|
|
|
HumanMFAOTPAddedType,
|
|
|
|
),
|
|
|
|
Secret: secret,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HumanOTPAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
otpAdded := &HumanOTPAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, otpAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-Ns9df", "unable to unmarshal human otp added")
|
|
|
|
}
|
|
|
|
return otpAdded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HumanOTPVerifiedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-01-15 09:32:59 +01:00
|
|
|
UserAgentID string `json:"userAgentID,omitempty"`
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanOTPVerifiedEvent) Data() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *HumanOTPVerifiedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *HumanOTPVerifiedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewHumanOTPVerifiedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
userAgentID string,
|
|
|
|
) *HumanOTPVerifiedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &HumanOTPVerifiedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-04 14:52:13 +01:00
|
|
|
HumanMFAOTPVerifiedType,
|
|
|
|
),
|
2021-01-15 09:32:59 +01:00
|
|
|
UserAgentID: userAgentID,
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HumanOTPVerifiedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &HumanOTPVerifiedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HumanOTPRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanOTPRemovedEvent) Data() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *HumanOTPRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *HumanOTPRemovedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewHumanOTPRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
) *HumanOTPRemovedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &HumanOTPRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-04 14:52:13 +01:00
|
|
|
HumanMFAOTPRemovedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HumanOTPRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &HumanOTPRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HumanOTPCheckSucceededEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-02-08 11:30:30 +01:00
|
|
|
*AuthRequestInfo
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanOTPCheckSucceededEvent) Data() interface{} {
|
2021-02-08 11:30:30 +01:00
|
|
|
return e
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *HumanOTPCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *HumanOTPCheckSucceededEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewHumanOTPCheckSucceededEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
info *AuthRequestInfo,
|
|
|
|
) *HumanOTPCheckSucceededEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &HumanOTPCheckSucceededEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-04 14:52:13 +01:00
|
|
|
HumanMFAOTPCheckSucceededType,
|
|
|
|
),
|
2021-02-08 11:30:30 +01:00
|
|
|
AuthRequestInfo: info,
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HumanOTPCheckSucceededEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
2021-02-08 11:30:30 +01:00
|
|
|
otpAdded := &HumanOTPCheckSucceededEvent{
|
2021-01-04 14:52:13 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2021-02-08 11:30:30 +01:00
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, otpAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-Ns9df", "unable to unmarshal human otp check succeeded")
|
|
|
|
}
|
|
|
|
return otpAdded, nil
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type HumanOTPCheckFailedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-02-08 11:30:30 +01:00
|
|
|
*AuthRequestInfo
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanOTPCheckFailedEvent) Data() interface{} {
|
2021-02-08 11:30:30 +01:00
|
|
|
return e
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *HumanOTPCheckFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *HumanOTPCheckFailedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewHumanOTPCheckFailedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
info *AuthRequestInfo,
|
|
|
|
) *HumanOTPCheckFailedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &HumanOTPCheckFailedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-04 14:52:13 +01:00
|
|
|
HumanMFAOTPCheckFailedType,
|
|
|
|
),
|
2021-02-08 11:30:30 +01:00
|
|
|
AuthRequestInfo: info,
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HumanOTPCheckFailedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
2021-02-08 11:30:30 +01:00
|
|
|
otpAdded := &HumanOTPCheckFailedEvent{
|
2021-01-04 14:52:13 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2021-02-08 11:30:30 +01:00
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, otpAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-Ns9df", "unable to unmarshal human otp check failed")
|
|
|
|
}
|
|
|
|
return otpAdded, nil
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|