2023-02-27 16:32:18 +01:00
|
|
|
package idp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OIDCIDPAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-03-16 16:47:22 +01:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
ClientID string `json:"clientId"`
|
|
|
|
ClientSecret *crypto.CryptoValue `json:"clientSecret"`
|
|
|
|
Scopes []string `json:"scopes,omitempty"`
|
|
|
|
IsIDTokenMapping bool `json:"idTokenMapping,omitempty"`
|
2023-02-27 16:32:18 +01:00
|
|
|
Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOIDCIDPAddedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
issuer,
|
|
|
|
clientID string,
|
|
|
|
clientSecret *crypto.CryptoValue,
|
|
|
|
scopes []string,
|
2023-03-16 16:47:22 +01:00
|
|
|
isIDTokenMapping bool,
|
2023-02-27 16:32:18 +01:00
|
|
|
options Options,
|
|
|
|
) *OIDCIDPAddedEvent {
|
|
|
|
return &OIDCIDPAddedEvent{
|
2023-03-16 16:47:22 +01:00
|
|
|
BaseEvent: *base,
|
|
|
|
ID: id,
|
|
|
|
Name: name,
|
|
|
|
Issuer: issuer,
|
|
|
|
ClientID: clientID,
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
Scopes: scopes,
|
|
|
|
IsIDTokenMapping: isIDTokenMapping,
|
|
|
|
Options: options,
|
2023-02-27 16:32:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *OIDCIDPAddedEvent) Payload() interface{} {
|
2023-02-27 16:32:18 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *OIDCIDPAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-02-27 16:32:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func OIDCIDPAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-02-27 16:32:18 +01:00
|
|
|
e := &OIDCIDPAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2023-02-27 16:32:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "IDP-Et1dq", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type OIDCIDPChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-03-16 16:47:22 +01:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
Issuer *string `json:"issuer,omitempty"`
|
|
|
|
ClientID *string `json:"clientId,omitempty"`
|
|
|
|
ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"`
|
|
|
|
Scopes []string `json:"scopes,omitempty"`
|
|
|
|
IsIDTokenMapping *bool `json:"idTokenMapping,omitempty"`
|
2023-02-27 16:32:18 +01:00
|
|
|
OptionChanges
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOIDCIDPChangedEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
id string,
|
|
|
|
changes []OIDCIDPChanges,
|
|
|
|
) (*OIDCIDPChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "IDP-BH3dl", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changedEvent := &OIDCIDPChangedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
for _, change := range changes {
|
|
|
|
change(changedEvent)
|
|
|
|
}
|
|
|
|
return changedEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type OIDCIDPChanges func(*OIDCIDPChangedEvent)
|
|
|
|
|
|
|
|
func ChangeOIDCName(name string) func(*OIDCIDPChangedEvent) {
|
|
|
|
return func(e *OIDCIDPChangedEvent) {
|
|
|
|
e.Name = &name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeOIDCIssuer(issuer string) func(*OIDCIDPChangedEvent) {
|
|
|
|
return func(e *OIDCIDPChangedEvent) {
|
|
|
|
e.Issuer = &issuer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeOIDCClientID(clientID string) func(*OIDCIDPChangedEvent) {
|
|
|
|
return func(e *OIDCIDPChangedEvent) {
|
|
|
|
e.ClientID = &clientID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeOIDCClientSecret(clientSecret *crypto.CryptoValue) func(*OIDCIDPChangedEvent) {
|
|
|
|
return func(e *OIDCIDPChangedEvent) {
|
|
|
|
e.ClientSecret = clientSecret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeOIDCOptions(options OptionChanges) func(*OIDCIDPChangedEvent) {
|
|
|
|
return func(e *OIDCIDPChangedEvent) {
|
|
|
|
e.OptionChanges = options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeOIDCScopes(scopes []string) func(*OIDCIDPChangedEvent) {
|
|
|
|
return func(e *OIDCIDPChangedEvent) {
|
|
|
|
e.Scopes = scopes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 16:47:22 +01:00
|
|
|
func ChangeOIDCIsIDTokenMapping(idTokenMapping bool) func(*OIDCIDPChangedEvent) {
|
|
|
|
return func(e *OIDCIDPChangedEvent) {
|
|
|
|
e.IsIDTokenMapping = &idTokenMapping
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *OIDCIDPChangedEvent) Payload() interface{} {
|
2023-02-27 16:32:18 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *OIDCIDPChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-02-27 16:32:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func OIDCIDPChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-02-27 16:32:18 +01:00
|
|
|
e := &OIDCIDPChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2023-02-27 16:32:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "IDP-D3gjzh", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
2023-06-08 10:13:25 +02:00
|
|
|
|
|
|
|
type OIDCIDPMigratedAzureADEvent struct {
|
|
|
|
AzureADIDPAddedEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOIDCIDPMigratedAzureADEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
clientID string,
|
|
|
|
clientSecret *crypto.CryptoValue,
|
|
|
|
scopes []string,
|
|
|
|
tenant string,
|
|
|
|
isEmailVerified bool,
|
|
|
|
options Options,
|
|
|
|
) *OIDCIDPMigratedAzureADEvent {
|
|
|
|
return &OIDCIDPMigratedAzureADEvent{
|
|
|
|
AzureADIDPAddedEvent: AzureADIDPAddedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
ID: id,
|
|
|
|
Name: name,
|
|
|
|
ClientID: clientID,
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
Scopes: scopes,
|
|
|
|
Tenant: tenant,
|
|
|
|
IsEmailVerified: isEmailVerified,
|
|
|
|
Options: options,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *OIDCIDPMigratedAzureADEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *OIDCIDPMigratedAzureADEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-06-08 10:13:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func OIDCIDPMigratedAzureADEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-06-08 10:13:25 +02:00
|
|
|
e, err := AzureADIDPAddedEventMapper(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &OIDCIDPMigratedAzureADEvent{AzureADIDPAddedEvent: *e.(*AzureADIDPAddedEvent)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type OIDCIDPMigratedGoogleEvent struct {
|
|
|
|
GoogleIDPAddedEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOIDCIDPMigratedGoogleEvent(
|
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
clientID string,
|
|
|
|
clientSecret *crypto.CryptoValue,
|
|
|
|
scopes []string,
|
|
|
|
options Options,
|
|
|
|
) *OIDCIDPMigratedGoogleEvent {
|
|
|
|
return &OIDCIDPMigratedGoogleEvent{
|
|
|
|
GoogleIDPAddedEvent: GoogleIDPAddedEvent{
|
|
|
|
BaseEvent: *base,
|
|
|
|
ID: id,
|
|
|
|
Name: name,
|
|
|
|
ClientID: clientID,
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
Scopes: scopes,
|
|
|
|
Options: options,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *OIDCIDPMigratedGoogleEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *OIDCIDPMigratedGoogleEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-06-08 10:13:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func OIDCIDPMigratedGoogleEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2023-06-08 10:13:25 +02:00
|
|
|
e, err := GoogleIDPAddedEventMapper(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &OIDCIDPMigratedGoogleEvent{GoogleIDPAddedEvent: *e.(*GoogleIDPAddedEvent)}, nil
|
|
|
|
}
|