zitadel/internal/repository/user/personal_access_token.go
Silvan b5564572bc
feat(eventstore): increase parallel write capabilities (#5940)
This implementation increases parallel write capabilities of the eventstore.
Please have a look at the technical advisories: [05](https://zitadel.com/docs/support/advisory/a10005) and  [06](https://zitadel.com/docs/support/advisory/a10006).
The implementation of eventstore.push is rewritten and stored events are migrated to a new table `eventstore.events2`.
If you are using cockroach: make sure that the database user of ZITADEL has `VIEWACTIVITY` grant. This is used to query events.
2023-10-19 12:19:10 +02:00

104 lines
2.5 KiB
Go

package user
import (
"context"
"time"
"github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/eventstore"
)
const (
personalAccessTokenEventPrefix = userEventTypePrefix + "pat."
PersonalAccessTokenAddedType = personalAccessTokenEventPrefix + "added"
PersonalAccessTokenRemovedType = personalAccessTokenEventPrefix + "removed"
)
type PersonalAccessTokenAddedEvent struct {
eventstore.BaseEvent `json:"-"`
TokenID string `json:"tokenId"`
Expiration time.Time `json:"expiration"`
Scopes []string `json:"scopes"`
}
func (e *PersonalAccessTokenAddedEvent) Payload() interface{} {
return e
}
func (e *PersonalAccessTokenAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewPersonalAccessTokenAddedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
tokenID string,
expiration time.Time,
scopes []string,
) *PersonalAccessTokenAddedEvent {
return &PersonalAccessTokenAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
PersonalAccessTokenAddedType,
),
TokenID: tokenID,
Expiration: expiration,
Scopes: scopes,
}
}
func PersonalAccessTokenAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
tokenAdded := &PersonalAccessTokenAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(tokenAdded)
if err != nil {
return nil, errors.ThrowInternal(err, "USER-Dbges", "unable to unmarshal token added")
}
return tokenAdded, nil
}
type PersonalAccessTokenRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
TokenID string `json:"tokenId"`
}
func (e *PersonalAccessTokenRemovedEvent) Payload() interface{} {
return e
}
func (e *PersonalAccessTokenRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewPersonalAccessTokenRemovedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
tokenID string,
) *PersonalAccessTokenRemovedEvent {
return &PersonalAccessTokenRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
PersonalAccessTokenRemovedType,
),
TokenID: tokenID,
}
}
func PersonalAccessTokenRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
tokenRemoved := &PersonalAccessTokenRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(tokenRemoved)
if err != nil {
return nil, errors.ThrowInternal(err, "USER-Dbneg", "unable to unmarshal token removed")
}
return tokenRemoved, nil
}