2021-01-06 10:47:55 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-12 12:59:51 +01:00
|
|
|
"github.com/caos/logging"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-12 12:59:51 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-06 10:47:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Step9 struct {
|
|
|
|
Passwordless bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Step9) Step() domain.Step {
|
|
|
|
return domain.Step9
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (s *Step9) execute(ctx context.Context, commandSide *Commands) error {
|
2021-01-06 10:47:55 +01:00
|
|
|
return commandSide.SetupStep9(ctx, s)
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) SetupStep9(ctx context.Context, step *Step9) error {
|
2021-02-18 14:48:27 +01:00
|
|
|
fn := func(iam *IAMWriteModel) ([]eventstore.EventPusher, error) {
|
2021-03-19 11:12:56 +01:00
|
|
|
multiFactorModel := NewIAMMultiFactorWriteModel(domain.MultiFactorTypeU2FWithPIN)
|
2021-03-25 17:26:21 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&multiFactorModel.MultiFactorWriteModel.WriteModel)
|
2021-01-06 10:47:55 +01:00
|
|
|
if !step.Passwordless {
|
2021-02-18 14:48:27 +01:00
|
|
|
return []eventstore.EventPusher{}, nil
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
2021-02-24 11:17:39 +01:00
|
|
|
passwordlessEvent, err := setPasswordlessAllowedInPolicy(ctx, c, iamAgg)
|
2021-01-06 10:47:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-12 12:59:51 +01:00
|
|
|
logging.Log("SETUP-AEG2t").Info("allowed passwordless in login policy")
|
2021-02-24 11:17:39 +01:00
|
|
|
multifactorEvent, err := c.addMultiFactorToDefaultLoginPolicy(ctx, iamAgg, multiFactorModel, domain.MultiFactorTypeU2FWithPIN)
|
2021-01-06 10:47:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-12 12:59:51 +01:00
|
|
|
logging.Log("SETUP-ADfng").Info("added passwordless to MFA login policy")
|
2021-02-18 14:48:27 +01:00
|
|
|
return []eventstore.EventPusher{passwordlessEvent, multifactorEvent}, nil
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
2021-02-24 11:17:39 +01:00
|
|
|
return c.setup(ctx, step, fn)
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func setPasswordlessAllowedInPolicy(ctx context.Context, c *Commands, iamAgg *eventstore.Aggregate) (eventstore.EventPusher, error) {
|
2021-01-18 11:24:15 +01:00
|
|
|
policy, err := c.getDefaultLoginPolicy(ctx)
|
2021-01-06 10:47:55 +01:00
|
|
|
if err != nil {
|
2021-02-18 14:48:27 +01:00
|
|
|
return nil, err
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
|
|
|
policy.PasswordlessType = domain.PasswordlessTypeAllowed
|
2021-01-12 12:59:51 +01:00
|
|
|
return c.changeDefaultLoginPolicy(ctx, iamAgg, NewIAMLoginPolicyWriteModel(), policy)
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|