Fabi 5b433dcaa3
New eventstore policies (#1084)
* feat: login policy

* feat: password complexity policy

* feat: org iam policy

* feat: label policy

* feat: add and change policies

* feat: second factors

* feat: second and multi factors

* feat: better naming

* feat: better naming
2020-12-11 15:49:19 +01:00

132 lines
2.7 KiB
Go

package password_lockout
import (
"encoding/json"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
const (
PasswordLockoutPolicyAddedEventType = "policy.password.lockout.added"
PasswordLockoutPolicyChangedEventType = "policy.password.lockout.changed"
PasswordLockoutPolicyRemovedEventType = "policy.password.lockout.removed"
)
type AddedEvent struct {
eventstore.BaseEvent `json:"-"`
MaxAttempts uint64 `json:"maxAttempts,omitempty"`
ShowLockOutFailures bool `json:"showLockOutFailures"`
}
func (e *AddedEvent) CheckPrevious() bool {
return true
}
func (e *AddedEvent) Data() interface{} {
return e
}
func NewAddedEvent(
base *eventstore.BaseEvent,
maxAttempts uint64,
showLockOutFailures bool,
) *AddedEvent {
return &AddedEvent{
BaseEvent: *base,
MaxAttempts: maxAttempts,
ShowLockOutFailures: showLockOutFailures,
}
}
func AddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &AddedEvent{
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
}
type ChangedEvent struct {
eventstore.BaseEvent `json:"-"`
MaxAttempts uint64 `json:"maxAttempts,omitempty"`
ShowLockOutFailures bool `json:"showLockOutFailures,omitempty"`
}
func (e *ChangedEvent) CheckPrevious() bool {
return true
}
func (e *ChangedEvent) Data() interface{} {
return e
}
func NewChangedEvent(
base *eventstore.BaseEvent,
current *WriteModel,
maxAttempts uint64,
showLockOutFailures bool,
) *ChangedEvent {
e := &ChangedEvent{
BaseEvent: *base,
}
if current.MaxAttempts != maxAttempts {
e.MaxAttempts = maxAttempts
}
if current.ShowLockOutFailures != showLockOutFailures {
e.ShowLockOutFailures = showLockOutFailures
}
return e
}
func ChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &ChangedEvent{
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
}
type RemovedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *RemovedEvent) CheckPrevious() bool {
return true
}
func (e *RemovedEvent) Data() interface{} {
return nil
}
func NewRemovedEvent(
base *eventstore.BaseEvent,
) *RemovedEvent {
return &RemovedEvent{
BaseEvent: *base,
}
}
func RemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &RemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}