mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 04:18:01 +00:00
fa9f581d56
* chore: move to new org * logging * fix: org rename caos -> zitadel Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
205 lines
4.9 KiB
Go
205 lines
4.9 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
func (e *HumanOTPAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewHumanOTPAddedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
secret *crypto.CryptoValue,
|
|
) *HumanOTPAddedEvent {
|
|
return &HumanOTPAddedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
HumanMFAOTPAddedType,
|
|
),
|
|
Secret: secret,
|
|
}
|
|
}
|
|
|
|
func HumanOTPAddedEventMapper(event *repository.Event) (eventstore.Event, 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:"-"`
|
|
UserAgentID string `json:"userAgentID,omitempty"`
|
|
}
|
|
|
|
func (e *HumanOTPVerifiedEvent) Data() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *HumanOTPVerifiedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewHumanOTPVerifiedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
userAgentID string,
|
|
) *HumanOTPVerifiedEvent {
|
|
return &HumanOTPVerifiedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
HumanMFAOTPVerifiedType,
|
|
),
|
|
UserAgentID: userAgentID,
|
|
}
|
|
}
|
|
|
|
func HumanOTPVerifiedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
|
return &HumanOTPVerifiedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}, nil
|
|
}
|
|
|
|
type HumanOTPRemovedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
}
|
|
|
|
func (e *HumanOTPRemovedEvent) Data() interface{} {
|
|
return nil
|
|
}
|
|
|
|
func (e *HumanOTPRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewHumanOTPRemovedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
) *HumanOTPRemovedEvent {
|
|
return &HumanOTPRemovedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
HumanMFAOTPRemovedType,
|
|
),
|
|
}
|
|
}
|
|
|
|
func HumanOTPRemovedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
|
return &HumanOTPRemovedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}, nil
|
|
}
|
|
|
|
type HumanOTPCheckSucceededEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
*AuthRequestInfo
|
|
}
|
|
|
|
func (e *HumanOTPCheckSucceededEvent) Data() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *HumanOTPCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewHumanOTPCheckSucceededEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
info *AuthRequestInfo,
|
|
) *HumanOTPCheckSucceededEvent {
|
|
return &HumanOTPCheckSucceededEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
HumanMFAOTPCheckSucceededType,
|
|
),
|
|
AuthRequestInfo: info,
|
|
}
|
|
}
|
|
|
|
func HumanOTPCheckSucceededEventMapper(event *repository.Event) (eventstore.Event, error) {
|
|
otpAdded := &HumanOTPCheckSucceededEvent{
|
|
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 check succeeded")
|
|
}
|
|
return otpAdded, nil
|
|
}
|
|
|
|
type HumanOTPCheckFailedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
*AuthRequestInfo
|
|
}
|
|
|
|
func (e *HumanOTPCheckFailedEvent) Data() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *HumanOTPCheckFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewHumanOTPCheckFailedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
info *AuthRequestInfo,
|
|
) *HumanOTPCheckFailedEvent {
|
|
return &HumanOTPCheckFailedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
HumanMFAOTPCheckFailedType,
|
|
),
|
|
AuthRequestInfo: info,
|
|
}
|
|
}
|
|
|
|
func HumanOTPCheckFailedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
|
otpAdded := &HumanOTPCheckFailedEvent{
|
|
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 check failed")
|
|
}
|
|
return otpAdded, nil
|
|
}
|