zitadel/internal/v2/repository/user/human_mfa_otp.go
Fabi 959530ddad
feat: new user auth api (#1168)
* fix: correct selectors for extended writemodel

* fix: no previous checks in eventstore

* start check previous

* feat: auth user commands

* feat: auth user commands

* feat: auth user commands

* feat: otp

* feat: corrections from pr merge

* feat: webauthn

* feat: comment old webauthn

* feat: refactor user, human, machine

* feat: webauth command side

* feat: command and query side in login

* feat: fix user writemodel append events

* fix: remove creation dates on command side

* fix: remove previous sequence

* previous sequence

* fix: external idps

* Update internal/api/grpc/management/user.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* Update internal/v2/command/user_human_email.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix: pr changes

* fix: phone verification

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2021-01-15 09:32:59 +01:00

146 lines
3.6 KiB
Go

package user
import (
"context"
"encoding/json"
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/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 NewHumanOTPAddedEvent(ctx context.Context,
secret *crypto.CryptoValue) *HumanOTPAddedEvent {
return &HumanOTPAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
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:"-"`
UserAgentID string `json:"userAgentID,omitempty"`
}
func (e *HumanOTPVerifiedEvent) Data() interface{} {
return nil
}
func NewHumanOTPVerifiedEvent(ctx context.Context, userAgentID string) *HumanOTPVerifiedEvent {
return &HumanOTPVerifiedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
HumanMFAOTPVerifiedType,
),
UserAgentID: userAgentID,
}
}
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
}
func NewHumanOTPRemovedEvent(ctx context.Context) *HumanOTPRemovedEvent {
return &HumanOTPRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
HumanMFAOTPRemovedType,
),
}
}
func HumanOTPRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &HumanOTPRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type HumanOTPCheckSucceededEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *HumanOTPCheckSucceededEvent) Data() interface{} {
return nil
}
func NewHumanOTPCheckSucceededEvent(ctx context.Context) *HumanOTPCheckSucceededEvent {
return &HumanOTPCheckSucceededEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
HumanMFAOTPCheckSucceededType,
),
}
}
func HumanOTPCheckSucceededEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &HumanOTPCheckSucceededEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type HumanOTPCheckFailedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *HumanOTPCheckFailedEvent) Data() interface{} {
return nil
}
func NewHumanOTPCheckFailedEvent(ctx context.Context) *HumanOTPCheckFailedEvent {
return &HumanOTPCheckFailedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
HumanMFAOTPCheckFailedType,
),
}
}
func HumanOTPCheckFailedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &HumanOTPCheckFailedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}