2021-01-04 14:52:13 +01:00
|
|
|
package command
|
2020-12-11 15:49:19 +01:00
|
|
|
|
|
|
|
import (
|
2022-02-21 16:05:02 +01:00
|
|
|
"time"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2020-12-11 15:49:19 +01:00
|
|
|
)
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type LoginPolicyWriteModel struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.WriteModel
|
|
|
|
|
2022-02-21 16:05:02 +01:00
|
|
|
AllowUserNamePassword bool
|
|
|
|
AllowRegister bool
|
|
|
|
AllowExternalIDP bool
|
|
|
|
ForceMFA bool
|
|
|
|
HidePasswordReset bool
|
2022-05-16 15:39:09 +02:00
|
|
|
IgnoreUnknownUsernames bool
|
2022-10-06 13:30:14 +02:00
|
|
|
AllowDomainDiscovery bool
|
2022-02-21 16:05:02 +01:00
|
|
|
PasswordlessType domain.PasswordlessType
|
2022-05-16 15:39:09 +02:00
|
|
|
DefaultRedirectURI string
|
2022-02-21 16:05:02 +01:00
|
|
|
PasswordCheckLifetime time.Duration
|
|
|
|
ExternalLoginCheckLifetime time.Duration
|
|
|
|
MFAInitSkipLifetime time.Duration
|
|
|
|
SecondFactorCheckLifetime time.Duration
|
|
|
|
MultiFactorCheckLifetime time.Duration
|
|
|
|
State domain.PolicyState
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (wm *LoginPolicyWriteModel) Reduce() error {
|
2020-12-11 15:49:19 +01:00
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
2021-01-04 14:52:13 +01:00
|
|
|
case *policy.LoginPolicyAddedEvent:
|
2020-12-11 15:49:19 +01:00
|
|
|
wm.AllowRegister = e.AllowRegister
|
|
|
|
wm.AllowUserNamePassword = e.AllowUserNamePassword
|
|
|
|
wm.AllowExternalIDP = e.AllowExternalIDP
|
|
|
|
wm.ForceMFA = e.ForceMFA
|
|
|
|
wm.PasswordlessType = e.PasswordlessType
|
2021-06-03 11:53:30 +02:00
|
|
|
wm.HidePasswordReset = e.HidePasswordReset
|
2022-05-16 15:39:09 +02:00
|
|
|
wm.IgnoreUnknownUsernames = e.IgnoreUnknownUsernames
|
2022-10-06 13:30:14 +02:00
|
|
|
wm.AllowDomainDiscovery = e.AllowDomainDiscovery
|
2022-05-16 15:39:09 +02:00
|
|
|
wm.DefaultRedirectURI = e.DefaultRedirectURI
|
2022-02-21 16:05:02 +01:00
|
|
|
wm.PasswordCheckLifetime = e.PasswordCheckLifetime
|
|
|
|
wm.ExternalLoginCheckLifetime = e.ExternalLoginCheckLifetime
|
|
|
|
wm.MFAInitSkipLifetime = e.MFAInitSkipLifetime
|
|
|
|
wm.SecondFactorCheckLifetime = e.SecondFactorCheckLifetime
|
|
|
|
wm.MultiFactorCheckLifetime = e.MultiFactorCheckLifetime
|
2021-01-07 16:06:45 +01:00
|
|
|
wm.State = domain.PolicyStateActive
|
2021-01-04 14:52:13 +01:00
|
|
|
case *policy.LoginPolicyChangedEvent:
|
2021-01-06 11:12:56 +01:00
|
|
|
if e.AllowRegister != nil {
|
|
|
|
wm.AllowRegister = *e.AllowRegister
|
|
|
|
}
|
|
|
|
if e.AllowUserNamePassword != nil {
|
|
|
|
wm.AllowUserNamePassword = *e.AllowUserNamePassword
|
|
|
|
}
|
|
|
|
if e.AllowExternalIDP != nil {
|
|
|
|
wm.AllowExternalIDP = *e.AllowExternalIDP
|
|
|
|
}
|
|
|
|
if e.ForceMFA != nil {
|
|
|
|
wm.ForceMFA = *e.ForceMFA
|
|
|
|
}
|
2021-06-03 11:53:30 +02:00
|
|
|
if e.HidePasswordReset != nil {
|
|
|
|
wm.HidePasswordReset = *e.HidePasswordReset
|
|
|
|
}
|
2022-05-16 15:39:09 +02:00
|
|
|
if e.IgnoreUnknownUsernames != nil {
|
|
|
|
wm.IgnoreUnknownUsernames = *e.IgnoreUnknownUsernames
|
|
|
|
}
|
2022-10-06 13:30:14 +02:00
|
|
|
if e.AllowDomainDiscovery != nil {
|
|
|
|
wm.AllowDomainDiscovery = *e.AllowDomainDiscovery
|
|
|
|
}
|
2021-01-06 11:12:56 +01:00
|
|
|
if e.PasswordlessType != nil {
|
|
|
|
wm.PasswordlessType = *e.PasswordlessType
|
|
|
|
}
|
2022-05-16 15:39:09 +02:00
|
|
|
if e.DefaultRedirectURI != nil {
|
|
|
|
wm.DefaultRedirectURI = *e.DefaultRedirectURI
|
|
|
|
}
|
2022-02-21 16:05:02 +01:00
|
|
|
if e.PasswordCheckLifetime != nil {
|
|
|
|
wm.PasswordCheckLifetime = *e.PasswordCheckLifetime
|
|
|
|
}
|
|
|
|
if e.ExternalLoginCheckLifetime != nil {
|
|
|
|
wm.ExternalLoginCheckLifetime = *e.ExternalLoginCheckLifetime
|
|
|
|
}
|
|
|
|
if e.MFAInitSkipLifetime != nil {
|
|
|
|
wm.MFAInitSkipLifetime = *e.MFAInitSkipLifetime
|
|
|
|
}
|
|
|
|
if e.SecondFactorCheckLifetime != nil {
|
|
|
|
wm.SecondFactorCheckLifetime = *e.SecondFactorCheckLifetime
|
|
|
|
}
|
|
|
|
if e.MultiFactorCheckLifetime != nil {
|
|
|
|
wm.MultiFactorCheckLifetime = *e.MultiFactorCheckLifetime
|
|
|
|
}
|
2021-01-04 14:52:13 +01:00
|
|
|
case *policy.LoginPolicyRemovedEvent:
|
2021-01-07 16:06:45 +01:00
|
|
|
wm.State = domain.PolicyStateRemoved
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|