2021-01-04 14:52:13 +01:00
|
|
|
package policy
|
2020-12-11 15:49:19 +01:00
|
|
|
|
|
|
|
import (
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
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"`
|
2024-04-10 11:14:55 +02:00
|
|
|
MaxOTPAttempts uint64 `json:"maxOTPAttempts,omitempty"`
|
2021-01-06 10:47:55 +01:00
|
|
|
ShowLockOutFailures bool `json:"showLockOutFailures,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *LockoutPolicyAddedEvent) Payload() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *LockoutPolicyAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
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,
|
2024-04-10 11:14:55 +02:00
|
|
|
maxPasswordAttempts,
|
|
|
|
maxOTPAttempts uint64,
|
2020-12-11 15:49:19 +01:00
|
|
|
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,
|
2024-04-10 11:14:55 +02:00
|
|
|
MaxPasswordAttempts: maxPasswordAttempts,
|
|
|
|
MaxOTPAttempts: maxOTPAttempts,
|
2020-12-11 15:49:19 +01:00
|
|
|
ShowLockOutFailures: showLockOutFailures,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func LockoutPolicyAddedEventMapper(event eventstore.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),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-11 15:49:19 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "POLIC-8XiVd", "unable to unmarshal policy")
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
2024-04-10 11:14:55 +02:00
|
|
|
MaxOTPAttempts *uint64 `json:"maxOTPAttempts,omitempty"`
|
2021-01-06 11:12:56 +01:00
|
|
|
ShowLockOutFailures *bool `json:"showLockOutFailures,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *LockoutPolicyChangedEvent) Payload() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *LockoutPolicyChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
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 {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "POLICY-sdgh6", "Errors.NoChangesFound")
|
2021-01-18 11:24:15 +01:00
|
|
|
}
|
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
|
|
|
|
2024-04-10 11:14:55 +02:00
|
|
|
func ChangeMaxPasswordAttempts(maxAttempts uint64) func(*LockoutPolicyChangedEvent) {
|
2021-08-11 08:36:32 +02:00
|
|
|
return func(e *LockoutPolicyChangedEvent) {
|
|
|
|
e.MaxPasswordAttempts = &maxAttempts
|
2021-01-18 11:24:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-10 11:14:55 +02:00
|
|
|
func ChangeMaxOTPAttempts(maxAttempts uint64) func(*LockoutPolicyChangedEvent) {
|
|
|
|
return func(e *LockoutPolicyChangedEvent) {
|
|
|
|
e.MaxOTPAttempts = &maxAttempts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func LockoutPolicyChangedEventMapper(event eventstore.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),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-11 15:49:19 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "POLIC-lWGRc", "unable to unmarshal policy")
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *LockoutPolicyRemovedEvent) Payload() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *LockoutPolicyRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func LockoutPolicyRemovedEventMapper(event eventstore.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
|
|
|
|
}
|