2021-05-20 13:33:35 +02:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-05-20 13:33:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
refreshTokenEventPrefix = humanEventPrefix + "refresh.token."
|
|
|
|
HumanRefreshTokenAddedType = refreshTokenEventPrefix + "added"
|
|
|
|
HumanRefreshTokenRenewedType = refreshTokenEventPrefix + "renewed"
|
|
|
|
HumanRefreshTokenRemovedType = refreshTokenEventPrefix + "removed"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HumanRefreshTokenAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
TokenID string `json:"tokenId"`
|
|
|
|
ClientID string `json:"clientId"`
|
|
|
|
UserAgentID string `json:"userAgentId"`
|
|
|
|
Audience []string `json:"audience"`
|
|
|
|
Scopes []string `json:"scopes"`
|
|
|
|
AuthMethodsReferences []string `json:"authMethodReferences"`
|
|
|
|
AuthTime time.Time `json:"authTime"`
|
|
|
|
IdleExpiration time.Duration `json:"idleExpiration"`
|
|
|
|
Expiration time.Duration `json:"expiration"`
|
|
|
|
PreferredLanguage string `json:"preferredLanguage"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *HumanRefreshTokenAddedEvent) Payload() interface{} {
|
2021-05-20 13:33:35 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *HumanRefreshTokenAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-05-20 13:33:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanRefreshTokenAddedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHumanRefreshTokenAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tokenID,
|
|
|
|
clientID,
|
|
|
|
userAgentID,
|
|
|
|
preferredLanguage string,
|
|
|
|
audience,
|
|
|
|
scopes,
|
|
|
|
authMethodsReferences []string,
|
|
|
|
authTime time.Time,
|
|
|
|
idleExpiration,
|
|
|
|
expiration time.Duration,
|
|
|
|
) *HumanRefreshTokenAddedEvent {
|
|
|
|
return &HumanRefreshTokenAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
HumanRefreshTokenAddedType,
|
|
|
|
),
|
|
|
|
TokenID: tokenID,
|
|
|
|
ClientID: clientID,
|
|
|
|
UserAgentID: userAgentID,
|
|
|
|
Audience: audience,
|
|
|
|
Scopes: scopes,
|
|
|
|
AuthMethodsReferences: authMethodsReferences,
|
|
|
|
AuthTime: authTime,
|
|
|
|
IdleExpiration: idleExpiration,
|
|
|
|
Expiration: expiration,
|
|
|
|
PreferredLanguage: preferredLanguage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func HumanRefreshTokenAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-05-20 13:33:35 +02:00
|
|
|
refreshTokenAdded := &HumanRefreshTokenAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(refreshTokenAdded)
|
2021-05-20 13:33:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-DGr14", "unable to unmarshal refresh token added")
|
|
|
|
}
|
|
|
|
|
|
|
|
return refreshTokenAdded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HumanRefreshTokenRenewedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
TokenID string `json:"tokenId"`
|
|
|
|
RefreshToken string `json:"refreshToken"`
|
|
|
|
IdleExpiration time.Duration `json:"idleExpiration"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *HumanRefreshTokenRenewedEvent) Payload() interface{} {
|
2021-05-20 13:33:35 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *HumanRefreshTokenRenewedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-05-20 13:33:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanRefreshTokenRenewedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHumanRefreshTokenRenewedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tokenID,
|
|
|
|
refreshToken string,
|
|
|
|
idleExpiration time.Duration,
|
|
|
|
) *HumanRefreshTokenRenewedEvent {
|
|
|
|
return &HumanRefreshTokenRenewedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
HumanRefreshTokenRenewedType,
|
|
|
|
),
|
|
|
|
TokenID: tokenID,
|
|
|
|
IdleExpiration: idleExpiration,
|
|
|
|
RefreshToken: refreshToken,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func HumanRefreshTokenRenewedEventEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-05-20 13:33:35 +02:00
|
|
|
tokenAdded := &HumanRefreshTokenRenewedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(tokenAdded)
|
2021-05-20 13:33:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-GBt21", "unable to unmarshal refresh token renewed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenAdded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HumanRefreshTokenRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
TokenID string `json:"tokenId"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *HumanRefreshTokenRemovedEvent) Payload() interface{} {
|
2021-05-20 13:33:35 +02:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *HumanRefreshTokenRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-05-20 13:33:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *HumanRefreshTokenRemovedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHumanRefreshTokenRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tokenID string,
|
|
|
|
) *HumanRefreshTokenRemovedEvent {
|
|
|
|
return &HumanRefreshTokenRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
HumanRefreshTokenRemovedType,
|
|
|
|
),
|
|
|
|
TokenID: tokenID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func HumanRefreshTokenRemovedEventEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-05-20 13:33:35 +02:00
|
|
|
tokenAdded := &HumanRefreshTokenRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(tokenAdded)
|
2021-05-20 13:33:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-Dggs2", "unable to unmarshal refresh token removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenAdded, nil
|
|
|
|
}
|