2020-05-18 12:06:36 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-06-19 14:52:04 +02:00
|
|
|
"context"
|
2020-05-18 12:06:36 +02:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
auth_view "github.com/zitadel/zitadel/internal/auth/repository/eventsourcing/view"
|
2022-04-27 01:01:45 +02:00
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-27 01:01:45 +02:00
|
|
|
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
proj_model "github.com/zitadel/zitadel/internal/project/model"
|
|
|
|
project_es_model "github.com/zitadel/zitadel/internal/project/repository/eventsourcing/model"
|
|
|
|
proj_view "github.com/zitadel/zitadel/internal/project/repository/view"
|
2022-10-26 15:06:48 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-11-30 17:01:17 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
view_model "github.com/zitadel/zitadel/internal/user/repository/view/model"
|
2020-05-18 12:06:36 +02:00
|
|
|
)
|
|
|
|
|
2020-12-18 16:47:45 +01:00
|
|
|
const (
|
|
|
|
tokenTable = "auth.tokens"
|
|
|
|
)
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
var _ handler.Projection = (*Token)(nil)
|
|
|
|
|
2020-05-18 12:06:36 +02:00
|
|
|
type Token struct {
|
2023-10-19 12:19:10 +02:00
|
|
|
view *auth_view.View
|
|
|
|
es handler.EventStore
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 16:47:45 +01:00
|
|
|
func newToken(
|
2022-11-22 07:36:48 +01:00
|
|
|
ctx context.Context,
|
2023-10-19 12:19:10 +02:00
|
|
|
config handler.Config,
|
|
|
|
view *auth_view.View,
|
|
|
|
) *handler.Handler {
|
|
|
|
return handler.NewHandler(
|
|
|
|
ctx,
|
|
|
|
&config,
|
|
|
|
&Token{
|
|
|
|
view: view,
|
|
|
|
es: config.Eventstore,
|
|
|
|
},
|
|
|
|
)
|
2020-12-18 16:47:45 +01:00
|
|
|
}
|
2020-05-18 12:06:36 +02:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
// Name implements [handler.Projection]
|
|
|
|
func (*Token) Name() string {
|
2020-05-18 12:06:36 +02:00
|
|
|
return tokenTable
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
// Reducers implements [handler.Projection]
|
|
|
|
func (t *Token) Reducers() []handler.AggregateReducer {
|
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: user.PersonalAccessTokenAddedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserTokenAddedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1ProfileChangedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanProfileChangedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1SignedOutType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanSignedOutType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserLockedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserDeactivatedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserTokenRemovedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.PersonalAccessTokenRemovedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanRefreshTokenRemovedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: project.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: project.ApplicationDeactivatedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.ApplicationRemovedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.ProjectDeactivatedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.ProjectRemovedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-06 13:36:35 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (t *Token) Reduce(event eventstore.Event) (_ *handler.Statement, err error) { //nolint:gocognit
|
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
switch event.Type() {
|
|
|
|
case user.UserTokenAddedType,
|
|
|
|
user.PersonalAccessTokenAddedType:
|
|
|
|
token := new(view_model.TokenView)
|
|
|
|
err := token.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return t.view.PutToken(token)
|
|
|
|
case user.UserV1ProfileChangedType,
|
|
|
|
user.HumanProfileChangedType:
|
|
|
|
user := new(view_model.UserView)
|
|
|
|
err := user.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tokens, err := t.view.TokensByUserID(event.Aggregate().ID, event.Aggregate().InstanceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, token := range tokens {
|
|
|
|
token.PreferredLanguage = user.PreferredLanguage
|
|
|
|
}
|
|
|
|
return t.view.PutTokens(tokens)
|
|
|
|
case user.UserV1SignedOutType,
|
|
|
|
user.HumanSignedOutType:
|
|
|
|
id, err := agentIDFromSession(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 16:47:45 +01:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
return t.view.DeleteSessionTokens(id, event)
|
|
|
|
case user.UserLockedType,
|
|
|
|
user.UserDeactivatedType,
|
|
|
|
user.UserRemovedType:
|
2020-12-18 16:47:45 +01:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
return t.view.DeleteUserTokens(event)
|
|
|
|
case user.UserTokenRemovedType,
|
|
|
|
user.PersonalAccessTokenRemovedType:
|
|
|
|
id, err := tokenIDFromRemovedEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-18 12:06:36 +02:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
return t.view.DeleteToken(id, event.Aggregate().InstanceID)
|
|
|
|
case user.HumanRefreshTokenRemovedType:
|
|
|
|
id, err := refreshTokenIDFromRemovedEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-04-28 16:28:13 +02:00
|
|
|
}
|
2020-05-18 12:06:36 +02:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
return t.view.DeleteTokensFromRefreshToken(id, event.Aggregate().InstanceID)
|
|
|
|
case project.ApplicationDeactivatedType,
|
|
|
|
project.ApplicationRemovedType:
|
|
|
|
application, err := applicationFromSession(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.view.DeleteApplicationTokens(event, application.AppID)
|
|
|
|
case project.ProjectDeactivatedType,
|
|
|
|
project.ProjectRemovedType:
|
|
|
|
project, err := t.getProjectByID(context.Background(), event.Aggregate().ID, event.Aggregate().InstanceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
applicationIDs := make([]string, 0, len(project.Applications))
|
|
|
|
for _, app := range project.Applications {
|
2023-11-21 11:05:22 +02:00
|
|
|
if app.OIDCConfig != nil && app.OIDCConfig.ClientID != "" {
|
2023-10-19 12:19:10 +02:00
|
|
|
applicationIDs = append(applicationIDs, app.OIDCConfig.ClientID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.view.DeleteApplicationTokens(event, applicationIDs...)
|
|
|
|
case instance.InstanceRemovedEventType:
|
|
|
|
return t.view.DeleteInstanceTokens(event)
|
|
|
|
case org.OrgRemovedEventType:
|
|
|
|
// deletes all tokens including PATs, which is expected for now
|
|
|
|
// if there is an undo of the org deletion in the future,
|
|
|
|
// we will need to have a look on how to handle the deleted PATs
|
|
|
|
return t.view.DeleteOrgTokens(event)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}), nil
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func agentIDFromSession(event eventstore.Event) (string, error) {
|
2020-05-18 12:06:36 +02:00
|
|
|
session := make(map[string]interface{})
|
2023-10-19 12:19:10 +02:00
|
|
|
if err := event.Unmarshal(&session); err != nil {
|
2022-11-22 07:36:48 +01:00
|
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
2020-05-18 12:06:36 +02:00
|
|
|
return "", caos_errs.ThrowInternal(nil, "MODEL-sd325", "could not unmarshal data")
|
|
|
|
}
|
2020-06-11 16:34:54 +02:00
|
|
|
return session["userAgentID"].(string), nil
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
2020-06-19 14:52:04 +02:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func applicationFromSession(event eventstore.Event) (*project_es_model.Application, error) {
|
2020-06-19 14:52:04 +02:00
|
|
|
application := new(project_es_model.Application)
|
2023-10-19 12:19:10 +02:00
|
|
|
if err := event.Unmarshal(application); err != nil {
|
2022-11-22 07:36:48 +01:00
|
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
2020-06-19 14:52:04 +02:00
|
|
|
return nil, caos_errs.ThrowInternal(nil, "MODEL-Hrw1q", "could not unmarshal data")
|
|
|
|
}
|
|
|
|
return application, nil
|
|
|
|
}
|
2020-12-02 08:50:59 +01:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func tokenIDFromRemovedEvent(event eventstore.Event) (string, error) {
|
2021-11-03 08:35:24 +01:00
|
|
|
removed := make(map[string]interface{})
|
2023-10-19 12:19:10 +02:00
|
|
|
if err := event.Unmarshal(&removed); err != nil {
|
2022-11-22 07:36:48 +01:00
|
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
2021-11-03 08:35:24 +01:00
|
|
|
return "", caos_errs.ThrowInternal(nil, "MODEL-Sff32", "could not unmarshal data")
|
|
|
|
}
|
|
|
|
return removed["tokenId"].(string), nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func refreshTokenIDFromRemovedEvent(event eventstore.Event) (string, error) {
|
2021-11-03 08:35:24 +01:00
|
|
|
removed := make(map[string]interface{})
|
2023-10-19 12:19:10 +02:00
|
|
|
if err := event.Unmarshal(&removed); err != nil {
|
2022-11-22 07:36:48 +01:00
|
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
2021-11-03 08:35:24 +01:00
|
|
|
return "", caos_errs.ThrowInternal(nil, "MODEL-Dfb3w", "could not unmarshal data")
|
|
|
|
}
|
|
|
|
return removed["tokenId"].(string), nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func (t *Token) getProjectByID(ctx context.Context, projID, instanceID string) (*proj_model.Project, error) {
|
2023-10-19 12:19:10 +02:00
|
|
|
query, err := proj_view.ProjectByIDQuery(projID, instanceID, 0)
|
2021-02-22 14:08:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
esProject := &project_es_model.Project{
|
2021-02-23 15:13:04 +01:00
|
|
|
ObjectRoot: es_models.ObjectRoot{
|
2021-02-22 14:08:47 +01:00
|
|
|
AggregateID: projID,
|
|
|
|
},
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
events, err := t.es.Filter(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = esProject.AppendEvents(events...); err != nil {
|
2021-02-22 14:08:47 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
|
2021-02-22 14:08:47 +01:00
|
|
|
if esProject.Sequence == 0 {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-Dsdw2", "Errors.Project.NotFound")
|
|
|
|
}
|
|
|
|
return project_es_model.ProjectToModel(esProject), nil
|
|
|
|
}
|