2021-05-20 11:33:35 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-11-22 06:36:48 +00:00
|
|
|
"context"
|
2021-05-20 11:33:35 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
auth_view "github.com/zitadel/zitadel/internal/auth/repository/eventsourcing/view"
|
2022-04-26 23:01:45 +00:00
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
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/user"
|
|
|
|
view_model "github.com/zitadel/zitadel/internal/user/repository/view/model"
|
2021-05-20 11:33:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
refreshTokenTable = "auth.refresh_tokens"
|
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
var _ handler.Projection = (*RefreshToken)(nil)
|
|
|
|
|
2021-05-20 11:33:35 +00:00
|
|
|
type RefreshToken struct {
|
2023-10-19 10:19:10 +00:00
|
|
|
view *auth_view.View
|
2021-05-20 11:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRefreshToken(
|
2022-11-22 06:36:48 +00:00
|
|
|
ctx context.Context,
|
2023-10-19 10:19:10 +00:00
|
|
|
config handler.Config,
|
|
|
|
view *auth_view.View,
|
|
|
|
) *handler.Handler {
|
|
|
|
return handler.NewHandler(
|
|
|
|
ctx,
|
|
|
|
&config,
|
|
|
|
&RefreshToken{
|
|
|
|
view: view,
|
|
|
|
},
|
|
|
|
)
|
2021-05-20 11:33:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
// Name implements [handler.Projection]
|
|
|
|
func (*RefreshToken) Name() string {
|
2021-05-20 11:33:35 +00:00
|
|
|
return refreshTokenTable
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
// Reducers implements [handler.Projection]
|
|
|
|
func (t *RefreshToken) Reducers() []handler.AggregateReducer {
|
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: user.HumanRefreshTokenAddedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanRefreshTokenRenewedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanRefreshTokenRemovedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserLockedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserDeactivatedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: t.Reduce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-20 11:33:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (t *RefreshToken) Reduce(event eventstore.Event) (_ *handler.Statement, err error) {
|
|
|
|
return handler.NewStatement(event, func(ex handler.Executer, projectionName string) error {
|
|
|
|
switch event.Type() {
|
|
|
|
case user.HumanRefreshTokenAddedType:
|
|
|
|
token := new(view_model.RefreshTokenView)
|
|
|
|
err := token.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.view.PutRefreshToken(token)
|
|
|
|
case user.HumanRefreshTokenRenewedType:
|
|
|
|
e := new(user.HumanRefreshTokenRenewedEvent)
|
|
|
|
if err := event.Unmarshal(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.Aggregate().InstanceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = token.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return t.view.PutRefreshToken(token)
|
|
|
|
case user.HumanRefreshTokenRemovedType:
|
|
|
|
e := new(user.HumanRefreshTokenRemovedEvent)
|
|
|
|
if err := event.Unmarshal(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.Aggregate().InstanceID)
|
|
|
|
case user.UserLockedType,
|
|
|
|
user.UserDeactivatedType,
|
|
|
|
user.UserRemovedType:
|
|
|
|
|
|
|
|
return t.view.DeleteUserRefreshTokens(event.Aggregate().ID, event.Aggregate().InstanceID)
|
|
|
|
case instance.InstanceRemovedEventType:
|
|
|
|
|
|
|
|
return t.view.DeleteInstanceRefreshTokens(event.Aggregate().InstanceID)
|
|
|
|
case org.OrgRemovedEventType:
|
|
|
|
return t.view.DeleteOrgRefreshTokens(event)
|
|
|
|
default:
|
|
|
|
return nil
|
2021-05-20 11:33:35 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
}), nil
|
2021-05-20 11:33:35 +00:00
|
|
|
}
|