2020-11-06 17:25:07 +01:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-12 22:50:01 +01:00
|
|
|
"encoding/json"
|
2020-11-06 17:25:07 +01:00
|
|
|
|
2020-11-12 22:50:01 +01:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2020-11-06 17:25:07 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2020-11-12 22:50:01 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
2020-11-06 17:25:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-11-06 22:09:19 +01:00
|
|
|
LoginPolicyAddedEventType = "policy.login.added"
|
|
|
|
LoginPolicyChangedEventType = "policy.login.changed"
|
|
|
|
LoginPolicyRemovedEventType = "policy.login.removed"
|
2020-11-06 17:25:07 +01:00
|
|
|
)
|
|
|
|
|
2020-11-06 22:09:19 +01:00
|
|
|
type LoginPolicyAggregate struct {
|
|
|
|
eventstore.Aggregate
|
|
|
|
|
|
|
|
AllowUserNamePassword bool
|
|
|
|
AllowRegister bool
|
|
|
|
AllowExternalIDP bool
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:51:44 +01:00
|
|
|
type LoginPolicyReadModel struct {
|
|
|
|
eventstore.ReadModel
|
|
|
|
|
|
|
|
AllowUserNamePassword bool
|
|
|
|
AllowRegister bool
|
|
|
|
AllowExternalIDP bool
|
|
|
|
}
|
|
|
|
|
2020-11-12 22:50:01 +01:00
|
|
|
func (rm *LoginPolicyReadModel) Reduce() error {
|
|
|
|
for _, event := range rm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *LoginPolicyAddedEvent:
|
|
|
|
rm.AllowUserNamePassword = e.AllowUserNamePassword
|
|
|
|
rm.AllowExternalIDP = e.AllowExternalIDP
|
|
|
|
rm.AllowRegister = e.AllowRegister
|
|
|
|
case *LoginPolicyChangedEvent:
|
|
|
|
rm.AllowUserNamePassword = e.AllowUserNamePassword
|
|
|
|
rm.AllowExternalIDP = e.AllowExternalIDP
|
|
|
|
rm.AllowRegister = e.AllowRegister
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rm.ReadModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:25:07 +01:00
|
|
|
type LoginPolicyAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
AllowUserNamePassword bool `json:"allowUsernamePassword"`
|
|
|
|
AllowRegister bool `json:"allowRegister"`
|
|
|
|
AllowExternalIDP bool `json:"allowExternalIdp"`
|
|
|
|
// TODO: IDPProviders
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *LoginPolicyAddedEvent) CheckPrevious() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *LoginPolicyAddedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLoginPolicyAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
allowUserNamePassword,
|
|
|
|
allowRegister,
|
|
|
|
allowExternalIDP bool,
|
|
|
|
) *LoginPolicyAddedEvent {
|
|
|
|
|
|
|
|
return &LoginPolicyAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
LoginPolicyAddedEventType,
|
|
|
|
),
|
|
|
|
AllowExternalIDP: allowExternalIDP,
|
|
|
|
AllowRegister: allowRegister,
|
|
|
|
AllowUserNamePassword: allowUserNamePassword,
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 22:09:19 +01:00
|
|
|
|
2020-11-12 22:50:01 +01:00
|
|
|
func LoginPolicyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &LoginPolicyAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "POLIC-nWndT", "unable to unmarshal policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2020-11-06 22:09:19 +01:00
|
|
|
type LoginPolicyChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2020-11-11 17:51:44 +01:00
|
|
|
AllowUserNamePassword bool `json:"allowUsernamePassword,omitempty"`
|
|
|
|
AllowRegister bool `json:"allowRegister"`
|
|
|
|
AllowExternalIDP bool `json:"allowExternalIdp"`
|
2020-11-06 22:09:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *LoginPolicyChangedEvent) CheckPrevious() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *LoginPolicyChangedEvent) Data() interface{} {
|
2020-11-11 17:51:44 +01:00
|
|
|
return e
|
2020-11-06 22:09:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLoginPolicyChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
current,
|
|
|
|
changed *LoginPolicyAggregate,
|
|
|
|
) *LoginPolicyChangedEvent {
|
|
|
|
|
2020-11-11 17:51:44 +01:00
|
|
|
e := &LoginPolicyChangedEvent{
|
2020-11-06 22:09:19 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
LoginPolicyChangedEventType,
|
|
|
|
),
|
|
|
|
}
|
2020-11-11 17:51:44 +01:00
|
|
|
|
|
|
|
if current.AllowUserNamePassword != changed.AllowUserNamePassword {
|
|
|
|
e.AllowUserNamePassword = changed.AllowUserNamePassword
|
|
|
|
}
|
|
|
|
if current.AllowRegister != changed.AllowRegister {
|
|
|
|
e.AllowRegister = changed.AllowRegister
|
|
|
|
}
|
|
|
|
if current.AllowExternalIDP != changed.AllowExternalIDP {
|
|
|
|
e.AllowExternalIDP = changed.AllowExternalIDP
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
2020-11-06 22:09:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 22:50:01 +01:00
|
|
|
func LoginPolicyChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &LoginPolicyChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "POLIC-ehssl", "unable to unmarshal policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2020-11-06 22:09:19 +01:00
|
|
|
type LoginPolicyRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *LoginPolicyRemovedEvent) CheckPrevious() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *LoginPolicyRemovedEvent) Data() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLoginPolicyRemovedEvent(ctx context.Context) *LoginPolicyRemovedEvent {
|
|
|
|
return &LoginPolicyRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
LoginPolicyRemovedEventType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2020-11-12 22:50:01 +01:00
|
|
|
|
|
|
|
func LoginPolicyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &LoginPolicyRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|