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