zitadel/internal/v2/repository/policy/password_lockout.go

169 lines
4.1 KiB
Go
Raw Normal View History

2020-11-06 17:25:07 +01:00
package policy
import (
"encoding/json"
2020-11-06 17:25:07 +01:00
"github.com/caos/zitadel/internal/errors"
2020-11-06 17:25:07 +01:00
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
2020-11-06 17:25:07 +01:00
)
const (
2020-11-11 17:51:44 +01:00
PasswordLockoutPolicyAddedEventType = "policy.password.lockout.added"
PasswordLockoutPolicyChangedEventType = "policy.password.lockout.changed"
PasswordLockoutPolicyRemovedEventType = "policy.password.lockout.removed"
2020-11-06 17:25:07 +01:00
)
2020-11-11 17:51:44 +01:00
type PasswordLockoutPolicyAggregate struct {
2020-11-06 22:09:19 +01:00
eventstore.Aggregate
}
2020-11-11 17:51:44 +01:00
type PasswordLockoutPolicyReadModel struct {
eventstore.ReadModel
MaxAttempts uint8
2020-11-11 17:51:44 +01:00
ShowLockOutFailures bool
}
func (rm *PasswordLockoutPolicyReadModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *PasswordLockoutPolicyAddedEvent:
rm.MaxAttempts = e.MaxAttempts
rm.ShowLockOutFailures = e.ShowLockOutFailures
case *PasswordLockoutPolicyChangedEvent:
rm.MaxAttempts = e.MaxAttempts
rm.ShowLockOutFailures = e.ShowLockOutFailures
}
}
return rm.ReadModel.Reduce()
}
type PasswordLockoutPolicyWriteModel struct {
eventstore.WriteModel
MaxAttempts uint8
ShowLockOutFailures bool
}
func (wm *PasswordLockoutPolicyWriteModel) Reduce() error {
return errors.ThrowUnimplemented(nil, "POLIC-xJjvN", "reduce unimpelemnted")
}
2020-11-06 17:25:07 +01:00
type PasswordLockoutPolicyAddedEvent struct {
eventstore.BaseEvent `json:"-"`
2020-11-27 11:30:56 +01:00
MaxAttempts uint8 `json:"maxAttempts,omitempty"`
ShowLockOutFailures bool `json:"showLockOutFailures"`
2020-11-06 17:25:07 +01:00
}
func (e *PasswordLockoutPolicyAddedEvent) CheckPrevious() bool {
return true
}
func (e *PasswordLockoutPolicyAddedEvent) Data() interface{} {
return e
}
func NewPasswordLockoutPolicyAddedEvent(
base *eventstore.BaseEvent,
maxAttempts uint8,
2020-11-06 17:25:07 +01:00
showLockOutFailures bool,
) *PasswordLockoutPolicyAddedEvent {
return &PasswordLockoutPolicyAddedEvent{
BaseEvent: *base,
2020-11-06 17:25:07 +01:00
MaxAttempts: maxAttempts,
ShowLockOutFailures: showLockOutFailures,
}
}
2020-11-06 22:09:19 +01:00
func PasswordLockoutPolicyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &PasswordLockoutPolicyAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "POLIC-8XiVd", "unable to unmarshal policy")
}
return e, nil
}
2020-11-06 22:09:19 +01:00
type PasswordLockoutPolicyChangedEvent struct {
eventstore.BaseEvent `json:"-"`
MaxAttempts uint8 `json:"maxAttempts,omitempty"`
ShowLockOutFailures bool `json:"showLockOutFailures,omitempty"`
2020-11-06 22:09:19 +01:00
}
func (e *PasswordLockoutPolicyChangedEvent) CheckPrevious() bool {
return true
}
func (e *PasswordLockoutPolicyChangedEvent) Data() interface{} {
2020-11-11 17:51:44 +01:00
return e
2020-11-06 22:09:19 +01:00
}
func NewPasswordLockoutPolicyChangedEvent(
base *eventstore.BaseEvent,
current *PasswordLockoutPolicyWriteModel,
maxAttempts uint8,
showLockOutFailures bool,
2020-11-06 22:09:19 +01:00
) *PasswordLockoutPolicyChangedEvent {
2020-11-11 17:51:44 +01:00
e := &PasswordLockoutPolicyChangedEvent{
BaseEvent: *base,
2020-11-06 22:09:19 +01:00
}
2020-11-11 17:51:44 +01:00
if current.MaxAttempts != maxAttempts {
e.MaxAttempts = maxAttempts
2020-11-11 17:51:44 +01:00
}
if current.ShowLockOutFailures != showLockOutFailures {
e.ShowLockOutFailures = showLockOutFailures
2020-11-11 17:51:44 +01:00
}
return e
2020-11-06 22:09:19 +01:00
}
func PasswordLockoutPolicyChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &PasswordLockoutPolicyChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "POLIC-lWGRc", "unable to unmarshal policy")
}
return e, nil
}
2020-11-06 22:09:19 +01:00
type PasswordLockoutPolicyRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *PasswordLockoutPolicyRemovedEvent) CheckPrevious() bool {
return true
}
func (e *PasswordLockoutPolicyRemovedEvent) Data() interface{} {
return nil
}
func NewPasswordLockoutPolicyRemovedEvent(
base *eventstore.BaseEvent,
2020-11-06 22:09:19 +01:00
) *PasswordLockoutPolicyRemovedEvent {
return &PasswordLockoutPolicyRemovedEvent{
BaseEvent: *base,
2020-11-06 22:09:19 +01:00
}
}
func PasswordLockoutPolicyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &PasswordLockoutPolicyRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}