mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 19:44:21 +00:00
e8babf1048
* fix: reduce load on view tables * create prerelease * linting: pass context to view handlers * fix error handling of refresh token handler * fix: improve processing of successful instanceIDs on views * fix revert intended change in .golangci.yaml * fix: set timeout for processInstances in spooler * fix: reduce update to active tokens on profile change * change token expiration query to db now() * remove branch from .releaserc.js
130 lines
4.0 KiB
Go
130 lines
4.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"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"
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/spooler"
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
"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"
|
|
)
|
|
|
|
const (
|
|
refreshTokenTable = "auth.refresh_tokens"
|
|
)
|
|
|
|
type RefreshToken struct {
|
|
handler
|
|
subscription *v1.Subscription
|
|
}
|
|
|
|
func newRefreshToken(
|
|
ctx context.Context,
|
|
handler handler,
|
|
) *RefreshToken {
|
|
h := &RefreshToken{
|
|
handler: handler,
|
|
}
|
|
|
|
h.subscribe(ctx)
|
|
|
|
return h
|
|
}
|
|
|
|
func (t *RefreshToken) subscribe(ctx context.Context) {
|
|
t.subscription = t.es.Subscribe(t.AggregateTypes()...)
|
|
go func() {
|
|
for event := range t.subscription.Events {
|
|
query.ReduceEvent(ctx, t, event)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (t *RefreshToken) ViewModel() string {
|
|
return refreshTokenTable
|
|
}
|
|
|
|
func (t *RefreshToken) Subscription() *v1.Subscription {
|
|
return t.subscription
|
|
}
|
|
|
|
func (t *RefreshToken) AggregateTypes() []es_models.AggregateType {
|
|
return []es_models.AggregateType{user.AggregateType, project.AggregateType, instance.AggregateType}
|
|
}
|
|
|
|
func (t *RefreshToken) CurrentSequence(instanceID string) (uint64, error) {
|
|
sequence, err := t.view.GetLatestRefreshTokenSequence(instanceID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return sequence.CurrentSequence, nil
|
|
}
|
|
|
|
func (t *RefreshToken) EventQuery(instanceIDs []string) (*es_models.SearchQuery, error) {
|
|
sequences, err := t.view.GetLatestRefreshTokenSequences(instanceIDs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return newSearchQuery(sequences, t.AggregateTypes(), instanceIDs), nil
|
|
}
|
|
|
|
func (t *RefreshToken) Reduce(event *es_models.Event) (err error) {
|
|
switch eventstore.EventType(event.Type) {
|
|
case user.HumanRefreshTokenAddedType:
|
|
token := new(view_model.RefreshTokenView)
|
|
err := token.AppendEvent(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return t.view.PutRefreshToken(token, event)
|
|
case user.HumanRefreshTokenRenewedType:
|
|
e := new(user.HumanRefreshTokenRenewedEvent)
|
|
if err := json.Unmarshal(event.Data, e); err != nil {
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
|
return caos_errs.ThrowInternal(nil, "MODEL-BHn75", "could not unmarshal data")
|
|
}
|
|
token, err := t.view.RefreshTokenByID(e.TokenID, event.InstanceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = token.AppendEvent(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return t.view.PutRefreshToken(token, event)
|
|
case user.HumanRefreshTokenRemovedType:
|
|
e := new(user.HumanRefreshTokenRemovedEvent)
|
|
if err := json.Unmarshal(event.Data, e); err != nil {
|
|
logging.WithError(err).Error("could not unmarshal event data")
|
|
return caos_errs.ThrowInternal(nil, "MODEL-Bz653", "could not unmarshal data")
|
|
}
|
|
return t.view.DeleteRefreshToken(e.TokenID, event.InstanceID, event)
|
|
case user.UserLockedType,
|
|
user.UserDeactivatedType,
|
|
user.UserRemovedType:
|
|
return t.view.DeleteUserRefreshTokens(event.AggregateID, event.InstanceID, event)
|
|
case instance.InstanceRemovedEventType:
|
|
return t.view.DeleteInstanceRefreshTokens(event)
|
|
default:
|
|
return t.view.ProcessedRefreshTokenSequence(event)
|
|
}
|
|
}
|
|
|
|
func (t *RefreshToken) OnError(event *es_models.Event, err error) error {
|
|
logging.WithFields("id", event.AggregateID).WithError(err).Warn("something went wrong in token handler")
|
|
return spooler.HandleError(event, err, t.view.GetLatestRefreshTokenFailedEvent, t.view.ProcessedRefreshTokenFailedEvent, t.view.ProcessedRefreshTokenSequence, t.errorCountUntilSkip)
|
|
}
|
|
|
|
func (t *RefreshToken) OnSuccess(instanceIDs []string) error {
|
|
return spooler.HandleSuccess(t.view.UpdateRefreshTokenSpoolerRunTimestamp, instanceIDs)
|
|
}
|