2023-05-24 18:29:58 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/idpintent"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IDPIntentWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2023-08-16 11:29:57 +00:00
|
|
|
SuccessURL *url.URL
|
|
|
|
FailureURL *url.URL
|
|
|
|
IDPID string
|
|
|
|
IDPUser []byte
|
|
|
|
IDPUserID string
|
|
|
|
IDPUserName string
|
|
|
|
UserID string
|
|
|
|
|
2023-05-24 18:29:58 +00:00
|
|
|
IDPAccessToken *crypto.CryptoValue
|
|
|
|
IDPIDToken string
|
2023-08-16 11:29:57 +00:00
|
|
|
|
|
|
|
IDPEntryAttributes map[string][]string
|
2023-05-24 18:29:58 +00:00
|
|
|
|
|
|
|
State domain.IDPIntentState
|
|
|
|
aggregate *eventstore.Aggregate
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDPIntentWriteModel(id, resourceOwner string) *IDPIntentWriteModel {
|
|
|
|
return &IDPIntentWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: id,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
aggregate: &idpintent.NewAggregate(id, resourceOwner).Aggregate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IDPIntentWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *idpintent.StartedEvent:
|
|
|
|
wm.reduceStartedEvent(e)
|
|
|
|
case *idpintent.SucceededEvent:
|
2023-08-16 11:29:57 +00:00
|
|
|
wm.reduceOAuthSucceededEvent(e)
|
|
|
|
case *idpintent.LDAPSucceededEvent:
|
|
|
|
wm.reduceLDAPSucceededEvent(e)
|
2023-05-24 18:29:58 +00:00
|
|
|
case *idpintent.FailedEvent:
|
|
|
|
wm.reduceFailedEvent(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IDPIntentWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(idpintent.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(
|
|
|
|
idpintent.StartedEventType,
|
|
|
|
idpintent.SucceededEventType,
|
2023-08-16 11:29:57 +00:00
|
|
|
idpintent.LDAPSucceededEventType,
|
2023-05-24 18:29:58 +00:00
|
|
|
idpintent.FailedEventType,
|
|
|
|
).
|
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IDPIntentWriteModel) reduceStartedEvent(e *idpintent.StartedEvent) {
|
|
|
|
wm.SuccessURL = e.SuccessURL
|
|
|
|
wm.FailureURL = e.FailureURL
|
|
|
|
wm.IDPID = e.IDPID
|
|
|
|
wm.State = domain.IDPIntentStateStarted
|
|
|
|
}
|
|
|
|
|
2023-08-16 11:29:57 +00:00
|
|
|
func (wm *IDPIntentWriteModel) reduceLDAPSucceededEvent(e *idpintent.LDAPSucceededEvent) {
|
|
|
|
wm.UserID = e.UserID
|
|
|
|
wm.IDPUser = e.IDPUser
|
|
|
|
wm.IDPUserID = e.IDPUserID
|
|
|
|
wm.IDPUserName = e.IDPUserName
|
|
|
|
wm.IDPEntryAttributes = e.EntryAttributes
|
|
|
|
wm.State = domain.IDPIntentStateSucceeded
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IDPIntentWriteModel) reduceOAuthSucceededEvent(e *idpintent.SucceededEvent) {
|
2023-05-24 18:29:58 +00:00
|
|
|
wm.UserID = e.UserID
|
|
|
|
wm.IDPUser = e.IDPUser
|
2023-06-20 12:39:50 +00:00
|
|
|
wm.IDPUserID = e.IDPUserID
|
|
|
|
wm.IDPUserName = e.IDPUserName
|
2023-05-24 18:29:58 +00:00
|
|
|
wm.IDPAccessToken = e.IDPAccessToken
|
|
|
|
wm.IDPIDToken = e.IDPIDToken
|
|
|
|
wm.State = domain.IDPIntentStateSucceeded
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IDPIntentWriteModel) reduceFailedEvent(e *idpintent.FailedEvent) {
|
|
|
|
wm.State = domain.IDPIntentStateFailed
|
|
|
|
}
|