2020-05-18 10:06:36 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-06-19 12:52:04 +00:00
|
|
|
"context"
|
2020-05-18 10:06:36 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
v1 "github.com/zitadel/zitadel/internal/eventstore/v1"
|
|
|
|
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/query"
|
|
|
|
es_sdk "github.com/zitadel/zitadel/internal/eventstore/v1/sdk"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/spooler"
|
|
|
|
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 13:06:48 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
user_repo "github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
view_model "github.com/zitadel/zitadel/internal/user/repository/view/model"
|
2020-05-18 10:06:36 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
tokenTable = "auth.tokens"
|
|
|
|
)
|
|
|
|
|
2020-05-18 10:06:36 +00:00
|
|
|
type Token struct {
|
|
|
|
handler
|
2021-02-23 14:13:04 +00:00
|
|
|
subscription *v1.Subscription
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func newToken(
|
2022-11-22 06:36:48 +00:00
|
|
|
ctx context.Context,
|
2020-12-18 15:47:45 +00:00
|
|
|
handler handler,
|
|
|
|
) *Token {
|
|
|
|
h := &Token{
|
2021-02-22 13:08:47 +00:00
|
|
|
handler: handler,
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 06:36:48 +00:00
|
|
|
h.subscribe(ctx)
|
2020-12-18 15:47:45 +00:00
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2022-11-22 06:36:48 +00:00
|
|
|
func (t *Token) subscribe(ctx context.Context) {
|
2020-12-18 15:47:45 +00:00
|
|
|
t.subscription = t.es.Subscribe(t.AggregateTypes()...)
|
|
|
|
go func() {
|
|
|
|
for event := range t.subscription.Events {
|
2022-11-22 06:36:48 +00:00
|
|
|
query.ReduceEvent(ctx, t, event)
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-05-18 10:06:36 +00:00
|
|
|
|
2020-10-27 14:53:36 +00:00
|
|
|
func (t *Token) ViewModel() string {
|
2020-05-18 10:06:36 +00:00
|
|
|
return tokenTable
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:36:35 +00:00
|
|
|
func (t *Token) Subscription() *v1.Subscription {
|
|
|
|
return t.subscription
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (_ *Token) AggregateTypes() []es_models.AggregateType {
|
2022-10-26 13:06:48 +00:00
|
|
|
return []es_models.AggregateType{user.AggregateType, project.AggregateType, instance.AggregateType}
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
func (t *Token) CurrentSequence(ctx context.Context, instanceID string) (uint64, error) {
|
|
|
|
sequence, err := t.view.GetLatestTokenSequence(ctx, instanceID)
|
2020-12-18 15:47:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sequence.CurrentSequence, nil
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
func (t *Token) EventQuery(ctx context.Context, instanceIDs []string) (*es_models.SearchQuery, error) {
|
|
|
|
sequences, err := t.view.GetLatestTokenSequences(ctx, instanceIDs)
|
2020-05-18 10:06:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-22 10:08:39 +00:00
|
|
|
return newSearchQuery(sequences, t.AggregateTypes(), instanceIDs), nil
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func (t *Token) Reduce(event *es_models.Event) (err error) {
|
2022-03-31 09:36:26 +00:00
|
|
|
switch eventstore.EventType(event.Type) {
|
|
|
|
case user.UserTokenAddedType,
|
|
|
|
user_repo.PersonalAccessTokenAddedType:
|
2020-10-15 11:52:41 +00:00
|
|
|
token := new(view_model.TokenView)
|
|
|
|
err := token.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return t.view.PutToken(token, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case user.UserV1ProfileChangedType,
|
|
|
|
user.HumanProfileChangedType:
|
2020-08-28 07:44:43 +00:00
|
|
|
user := new(view_model.UserView)
|
2022-11-22 06:36:48 +00:00
|
|
|
err := user.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
tokens, err := t.view.TokensByUserID(event.AggregateID, event.InstanceID)
|
2020-08-28 07:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, token := range tokens {
|
|
|
|
token.PreferredLanguage = user.PreferredLanguage
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return t.view.PutTokens(tokens, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case user.UserV1SignedOutType,
|
|
|
|
user.HumanSignedOutType:
|
2020-05-18 10:06:36 +00:00
|
|
|
id, err := agentIDFromSession(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
return t.view.DeleteSessionTokens(id, event.AggregateID, event.InstanceID, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case user.UserLockedType,
|
|
|
|
user.UserDeactivatedType,
|
|
|
|
user.UserRemovedType:
|
2022-04-19 06:26:12 +00:00
|
|
|
return t.view.DeleteUserTokens(event.AggregateID, event.InstanceID, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case user_repo.UserTokenRemovedType,
|
|
|
|
user_repo.PersonalAccessTokenRemovedType:
|
2021-11-03 07:35:24 +00:00
|
|
|
id, err := tokenIDFromRemovedEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
return t.view.DeleteToken(id, event.InstanceID, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case user_repo.HumanRefreshTokenRemovedType:
|
2021-11-03 07:35:24 +00:00
|
|
|
id, err := refreshTokenIDFromRemovedEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
return t.view.DeleteTokensFromRefreshToken(id, event.InstanceID, event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case project.ApplicationDeactivatedType,
|
|
|
|
project.ApplicationRemovedType:
|
2020-06-19 12:52:04 +00:00
|
|
|
application, err := applicationFromSession(event)
|
2020-05-18 10:06:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return t.view.DeleteApplicationTokens(event, application.AppID)
|
2022-03-31 09:36:26 +00:00
|
|
|
case project.ProjectDeactivatedType,
|
|
|
|
project.ProjectRemovedType:
|
2022-04-19 06:26:12 +00:00
|
|
|
project, err := t.getProjectByID(context.Background(), event.AggregateID, event.InstanceID)
|
2020-06-19 12:52:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-28 14:28:13 +00:00
|
|
|
clientIDs := make([]string, 0, len(project.Applications))
|
2020-06-19 12:52:04 +00:00
|
|
|
for _, app := range project.Applications {
|
2023-04-28 14:28:13 +00:00
|
|
|
if app.OIDCConfig != nil {
|
|
|
|
clientIDs = append(clientIDs, app.OIDCConfig.ClientID)
|
|
|
|
}
|
2020-06-19 12:52:04 +00:00
|
|
|
}
|
2023-04-28 14:28:13 +00:00
|
|
|
return t.view.DeleteApplicationTokens(event, clientIDs...)
|
2022-10-26 13:06:48 +00:00
|
|
|
case instance.InstanceRemovedEventType:
|
|
|
|
return t.view.DeleteInstanceTokens(event)
|
2022-11-30 16:01:17 +00:00
|
|
|
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)
|
2020-05-18 10:06:36 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return t.view.ProcessedTokenSequence(event)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func (t *Token) OnError(event *es_models.Event, err error) error {
|
2022-11-22 06:36:48 +00:00
|
|
|
logging.WithFields("id", event.AggregateID).WithError(err).Warn("something went wrong in token handler")
|
2020-10-27 14:53:36 +00:00
|
|
|
return spooler.HandleError(event, err, t.view.GetLatestTokenFailedEvent, t.view.ProcessedTokenFailedEvent, t.view.ProcessedTokenSequence, t.errorCountUntilSkip)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func agentIDFromSession(event *es_models.Event) (string, error) {
|
2020-05-18 10:06:36 +00:00
|
|
|
session := make(map[string]interface{})
|
2020-06-11 14:34:54 +00:00
|
|
|
if err := json.Unmarshal(event.Data, &session); err != nil {
|
2022-11-22 06:36:48 +00:00
|
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
2020-05-18 10:06:36 +00:00
|
|
|
return "", caos_errs.ThrowInternal(nil, "MODEL-sd325", "could not unmarshal data")
|
|
|
|
}
|
2020-06-11 14:34:54 +00:00
|
|
|
return session["userAgentID"].(string), nil
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
2020-06-19 12:52:04 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func applicationFromSession(event *es_models.Event) (*project_es_model.Application, error) {
|
2020-06-19 12:52:04 +00:00
|
|
|
application := new(project_es_model.Application)
|
|
|
|
if err := json.Unmarshal(event.Data, &application); err != nil {
|
2022-11-22 06:36:48 +00:00
|
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
2020-06-19 12:52:04 +00:00
|
|
|
return nil, caos_errs.ThrowInternal(nil, "MODEL-Hrw1q", "could not unmarshal data")
|
|
|
|
}
|
|
|
|
return application, nil
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
|
2021-11-03 07:35:24 +00:00
|
|
|
func tokenIDFromRemovedEvent(event *es_models.Event) (string, error) {
|
|
|
|
removed := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(event.Data, &removed); err != nil {
|
2022-11-22 06:36:48 +00:00
|
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
2021-11-03 07:35:24 +00:00
|
|
|
return "", caos_errs.ThrowInternal(nil, "MODEL-Sff32", "could not unmarshal data")
|
|
|
|
}
|
|
|
|
return removed["tokenId"].(string), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshTokenIDFromRemovedEvent(event *es_models.Event) (string, error) {
|
|
|
|
removed := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(event.Data, &removed); err != nil {
|
2022-11-22 06:36:48 +00:00
|
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
2021-11-03 07:35:24 +00:00
|
|
|
return "", caos_errs.ThrowInternal(nil, "MODEL-Dfb3w", "could not unmarshal data")
|
|
|
|
}
|
|
|
|
return removed["tokenId"].(string), nil
|
|
|
|
}
|
|
|
|
|
2022-11-22 06:36:48 +00:00
|
|
|
func (t *Token) OnSuccess(instanceIDs []string) error {
|
|
|
|
return spooler.HandleSuccess(t.view.UpdateTokenSpoolerRunTimestamp, instanceIDs)
|
2020-12-02 07:50:59 +00:00
|
|
|
}
|
2021-02-22 13:08:47 +00:00
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (t *Token) getProjectByID(ctx context.Context, projID, instanceID string) (*proj_model.Project, error) {
|
2023-04-28 14:28:13 +00:00
|
|
|
projectQuery, err := proj_view.ProjectByIDQuery(projID, instanceID, 0)
|
2021-02-22 13:08:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
esProject := &project_es_model.Project{
|
2021-02-23 14:13:04 +00:00
|
|
|
ObjectRoot: es_models.ObjectRoot{
|
2021-02-22 13:08:47 +00:00
|
|
|
AggregateID: projID,
|
|
|
|
},
|
|
|
|
}
|
2023-04-28 14:28:13 +00:00
|
|
|
err = es_sdk.Filter(ctx, t.Eventstore().FilterEvents, esProject.AppendEvents, projectQuery)
|
2021-04-15 13:30:19 +00:00
|
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
2021-02-22 13:08:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if esProject.Sequence == 0 {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-Dsdw2", "Errors.Project.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
return project_es_model.ProjectToModel(esProject), nil
|
|
|
|
}
|