2021-01-04 13:52:13 +00:00
|
|
|
package policy
|
2020-12-11 14:49:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-01-03 08:19:07 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-18 10:24:15 +00:00
|
|
|
|
2020-12-11 14:49:19 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-11 14:49:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-08-11 06:36:32 +00:00
|
|
|
LockoutPolicyAddedEventType = "policy.lockout.added"
|
|
|
|
LockoutPolicyChangedEventType = "policy.lockout.changed"
|
|
|
|
LockoutPolicyRemovedEventType = "policy.lockout.removed"
|
2020-12-11 14:49:19 +00:00
|
|
|
)
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
type LockoutPolicyAddedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
MaxPasswordAttempts uint64 `json:"maxPasswordAttempts,omitempty"`
|
2021-01-06 09:47:55 +00:00
|
|
|
ShowLockOutFailures bool `json:"showLockOutFailures,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func (e *LockoutPolicyAddedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func (e *LockoutPolicyAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func NewLockoutPolicyAddedEvent(
|
2020-12-11 14:49:19 +00:00
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
maxAttempts uint64,
|
|
|
|
showLockOutFailures bool,
|
2021-08-11 06:36:32 +00:00
|
|
|
) *LockoutPolicyAddedEvent {
|
2020-12-11 14:49:19 +00:00
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
return &LockoutPolicyAddedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *base,
|
2021-08-11 06:36:32 +00:00
|
|
|
MaxPasswordAttempts: maxAttempts,
|
2020-12-11 14:49:19 +00:00
|
|
|
ShowLockOutFailures: showLockOutFailures,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func LockoutPolicyAddedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-08-11 06:36:32 +00:00
|
|
|
e := &LockoutPolicyAddedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
type LockoutPolicyChangedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
MaxPasswordAttempts *uint64 `json:"maxPasswordAttempts,omitempty"`
|
2021-01-06 10:12:56 +00:00
|
|
|
ShowLockOutFailures *bool `json:"showLockOutFailures,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func (e *LockoutPolicyChangedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func (e *LockoutPolicyChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func NewLockoutPolicyChangedEvent(
|
2020-12-11 14:49:19 +00:00
|
|
|
base *eventstore.BaseEvent,
|
2021-08-11 06:36:32 +00:00
|
|
|
changes []LockoutPolicyChanges,
|
|
|
|
) (*LockoutPolicyChangedEvent, error) {
|
2021-01-18 10:24:15 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "POLICY-sdgh6", "Errors.NoChangesFound")
|
|
|
|
}
|
2021-08-11 06:36:32 +00:00
|
|
|
changeEvent := &LockoutPolicyChangedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
type LockoutPolicyChanges func(*LockoutPolicyChangedEvent)
|
2021-01-18 10:24:15 +00:00
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func ChangeMaxAttempts(maxAttempts uint64) func(*LockoutPolicyChangedEvent) {
|
|
|
|
return func(e *LockoutPolicyChangedEvent) {
|
|
|
|
e.MaxPasswordAttempts = &maxAttempts
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func ChangeShowLockOutFailures(showLockOutFailures bool) func(*LockoutPolicyChangedEvent) {
|
|
|
|
return func(e *LockoutPolicyChangedEvent) {
|
2021-01-18 10:24:15 +00:00
|
|
|
e.ShowLockOutFailures = &showLockOutFailures
|
|
|
|
}
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func LockoutPolicyChangedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-08-11 06:36:32 +00:00
|
|
|
e := &LockoutPolicyChangedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
type LockoutPolicyRemovedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func (e *LockoutPolicyRemovedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func (e *LockoutPolicyRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func NewLockoutPolicyRemovedEvent(base *eventstore.BaseEvent) *LockoutPolicyRemovedEvent {
|
|
|
|
return &LockoutPolicyRemovedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func LockoutPolicyRemovedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-08-11 06:36:32 +00:00
|
|
|
return &LockoutPolicyRemovedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|