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
This commit is contained in:
Fabi
2020-12-11 15:49:19 +01:00
committed by GitHub
parent 31ea9d1acd
commit 5b433dcaa3
85 changed files with 3477 additions and 1648 deletions
@@ -0,0 +1,131 @@
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
}
@@ -0,0 +1,24 @@
package password_lockout
import "github.com/caos/zitadel/internal/eventstore/v2"
type ReadModel struct {
eventstore.ReadModel
MaxAttempts uint64
ShowLockOutFailures bool
}
func (rm *ReadModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *AddedEvent:
rm.MaxAttempts = e.MaxAttempts
rm.ShowLockOutFailures = e.ShowLockOutFailures
case *ChangedEvent:
rm.MaxAttempts = e.MaxAttempts
rm.ShowLockOutFailures = e.ShowLockOutFailures
}
}
return rm.ReadModel.Reduce()
}
@@ -0,0 +1,26 @@
package password_lockout
import (
"github.com/caos/zitadel/internal/eventstore/v2"
)
type WriteModel struct {
eventstore.WriteModel
MaxAttempts uint64
ShowLockOutFailures bool
}
func (wm *WriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *AddedEvent:
wm.MaxAttempts = e.MaxAttempts
wm.ShowLockOutFailures = e.ShowLockOutFailures
case *ChangedEvent:
wm.MaxAttempts = e.MaxAttempts
wm.ShowLockOutFailures = e.ShowLockOutFailures
}
}
return wm.WriteModel.Reduce()
}