fix: handle project deactivate and remove correctly on tokens (#6947)

(cherry picked from commit ad3563d58b)
This commit is contained in:
Livio Spring 2023-11-21 11:05:22 +02:00
parent 3001d03bca
commit 7786b09444
No known key found for this signature in database
GPG Key ID: 26BB1C2FA5952CF0
3 changed files with 50 additions and 2 deletions

View File

@ -219,7 +219,7 @@ func (t *Token) Reduce(event eventstore.Event) (_ *handler.Statement, err error)
}
applicationIDs := make([]string, 0, len(project.Applications))
for _, app := range project.Applications {
if app.OIDCConfig != nil {
if app.OIDCConfig != nil && app.OIDCConfig.ClientID != "" {
applicationIDs = append(applicationIDs, app.OIDCConfig.ClientID)
}
}

View File

@ -99,7 +99,7 @@ func (p *Project) appendRemovedEvent() error {
func (p *Project) appendOIDCConfig(event eventstore.Event) error {
appEvent := new(oidcApp)
if err := event.Unmarshal(p); err != nil {
if err := event.Unmarshal(appEvent); err != nil {
return err
}
p.OIDCApplications = append(p.OIDCApplications, appEvent)

View File

@ -4,6 +4,8 @@ import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zitadel/zitadel/internal/eventstore"
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
"github.com/zitadel/zitadel/internal/project/model"
@ -40,6 +42,27 @@ func TestProjectFromEvents(t *testing.T) {
},
result: &Project{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, State: int32(model.ProjectStateActive)},
},
{
name: "project from events with OIDC Application, ok",
args: args{
event: []eventstore.Event{
&es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ProjectAddedType, Data: []byte(`{"name": "ProjectName"}`)},
&es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.OIDCConfigAddedType, Data: []byte(`{"appId":"appId", "clientId": "clientID"}`)},
},
project: nil,
},
result: &Project{
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"},
State: int32(model.ProjectStateActive),
Name: "ProjectName",
OIDCApplications: []*oidcApp{
{
AppID: "appID",
ClientID: "clientID",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -95,6 +118,30 @@ func TestAppendEvent(t *testing.T) {
},
result: &Project{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, State: int32(model.ProjectStateActive)},
},
{
name: "append oidc config added event",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.OIDCConfigAddedType, Data: []byte(`{"appId":"appID", "clientId": "clientID"}`)},
},
result: &Project{
ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"},
State: int32(model.ProjectStateActive),
OIDCApplications: []*oidcApp{{
AppID: "appID",
ClientID: "clientID",
}}},
},
{
name: "append application removed event",
args: args{
project: &Project{Name: "ProjectName", OIDCApplications: []*oidcApp{{
AppID: "appID",
ClientID: "clientID",
}}},
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: project.ApplicationRemovedType, Data: []byte(`{"appId": "appID"}`)},
},
result: &Project{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, State: int32(model.ProjectStateActive)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -113,6 +160,7 @@ func TestAppendEvent(t *testing.T) {
if result.ObjectRoot.AggregateID != tt.result.ObjectRoot.AggregateID {
t.Errorf("got wrong result id: expected: %v, actual: %v ", tt.result.ObjectRoot.AggregateID, result.ObjectRoot.AggregateID)
}
assert.Equal(t, tt.result.OIDCApplications, result.OIDCApplications)
})
}
}