zitadel/internal/v2/business/iam/idp_oidc_config.go

52 lines
1.3 KiB
Go
Raw Normal View History

2020-11-25 19:04:32 +00:00
package iam
import (
"context"
2020-11-26 12:14:07 +00:00
"github.com/caos/zitadel/internal/crypto"
2020-11-25 19:04:32 +00:00
iam_model "github.com/caos/zitadel/internal/iam/model"
"github.com/caos/zitadel/internal/v2/repository/iam"
2020-11-26 12:14:07 +00:00
"github.com/caos/zitadel/internal/v2/repository/idp/oidc"
2020-11-25 19:04:32 +00:00
)
2020-11-27 10:30:56 +00:00
func (r *Repository) ChangeIDPOIDCConfig(ctx context.Context, config *iam_model.OIDCIDPConfig) (*iam_model.OIDCIDPConfig, error) {
writeModel := iam.NewIDPOIDCConfigWriteModel(config.AggregateID, config.IDPConfigID)
2020-11-25 19:04:32 +00:00
2020-11-27 10:30:56 +00:00
err := r.eventstore.FilterToQueryReducer(ctx, writeModel)
2020-11-25 19:04:32 +00:00
if err != nil {
return nil, err
}
2020-11-27 10:30:56 +00:00
var clientSecret *crypto.CryptoValue
2020-11-26 08:19:14 +00:00
2020-11-27 10:30:56 +00:00
if config.ClientSecretString != "" {
clientSecret, err = crypto.Crypt([]byte(config.ClientSecretString), r.secretCrypto)
2020-11-26 12:14:07 +00:00
if err != nil {
return nil, err
}
2020-11-27 10:30:56 +00:00
}
aggregate := iam.AggregateFromWriteModel(&writeModel.ConfigWriteModel.WriteModel).
PushIDPOIDCConfigChanged(
2020-11-26 12:14:07 +00:00
ctx,
2020-11-27 10:30:56 +00:00
writeModel,
config.ClientID,
config.Issuer,
2020-11-26 12:14:07 +00:00
clientSecret,
2020-11-27 10:30:56 +00:00
oidc.MappingField(config.IDPDisplayNameMapping),
oidc.MappingField(config.UsernameMapping),
config.Scopes...)
2020-11-26 12:14:07 +00:00
events, err := r.eventstore.PushAggregates(ctx, aggregate)
if err != nil {
return nil, err
}
2020-11-27 10:30:56 +00:00
writeModel.AppendEvents(events...)
if err = writeModel.Reduce(); err != nil {
2020-11-26 12:14:07 +00:00
return nil, err
}
2020-11-27 10:30:56 +00:00
return writeModelToIDPOIDCConfig(&writeModel.ConfigWriteModel), nil
2020-11-26 08:19:14 +00:00
}