2023-04-19 08:46:02 +00:00
|
|
|
package deviceauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2024-05-16 05:07:56 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2023-04-19 08:46:02 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
eventTypePrefix eventstore.EventType = "device.authorization."
|
|
|
|
AddedEventType = eventTypePrefix + "added"
|
|
|
|
ApprovedEventType = eventTypePrefix + "approved"
|
|
|
|
CanceledEventType = eventTypePrefix + "canceled"
|
2024-05-16 05:07:56 +00:00
|
|
|
DoneEventType = eventTypePrefix + "done"
|
2023-04-19 08:46:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AddedEvent struct {
|
2023-11-03 11:02:17 +00:00
|
|
|
*eventstore.BaseEvent `json:"-"`
|
2023-04-19 08:46:02 +00:00
|
|
|
|
2024-05-16 05:07:56 +00:00
|
|
|
ClientID string
|
|
|
|
DeviceCode string
|
|
|
|
UserCode string
|
|
|
|
Expires time.Time
|
|
|
|
Scopes []string
|
|
|
|
Audience []string
|
|
|
|
State domain.DeviceAuthState
|
|
|
|
NeedRefreshToken bool
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *AddedEvent) SetBaseEvent(b *eventstore.BaseEvent) {
|
|
|
|
e.BaseEvent = b
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *AddedEvent) Payload() any {
|
2023-04-19 08:46:02 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *AddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-12-20 12:21:08 +00:00
|
|
|
return NewAddUniqueConstraints(e.DeviceCode, e.UserCode)
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
clientID string,
|
|
|
|
deviceCode string,
|
|
|
|
userCode string,
|
|
|
|
expires time.Time,
|
|
|
|
scopes []string,
|
2024-04-03 06:06:21 +00:00
|
|
|
audience []string,
|
2024-05-16 05:07:56 +00:00
|
|
|
needRefreshToken bool,
|
2023-04-19 08:46:02 +00:00
|
|
|
) *AddedEvent {
|
|
|
|
return &AddedEvent{
|
|
|
|
eventstore.NewBaseEventForPush(
|
|
|
|
ctx, aggregate, AddedEventType,
|
|
|
|
),
|
2024-05-16 05:07:56 +00:00
|
|
|
clientID, deviceCode, userCode, expires, scopes, audience,
|
|
|
|
domain.DeviceAuthStateInitiated, needRefreshToken,
|
|
|
|
}
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ApprovedEvent struct {
|
2023-11-03 11:02:17 +00:00
|
|
|
*eventstore.BaseEvent `json:"-"`
|
2023-04-19 08:46:02 +00:00
|
|
|
|
2024-05-16 05:07:56 +00:00
|
|
|
UserID string
|
|
|
|
UserOrgID string
|
|
|
|
UserAuthMethods []domain.UserAuthMethodType
|
|
|
|
AuthTime time.Time
|
|
|
|
PreferredLanguage *language.Tag
|
|
|
|
UserAgent *domain.UserAgent
|
2024-09-03 13:19:00 +00:00
|
|
|
SessionID string
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ApprovedEvent) SetBaseEvent(b *eventstore.BaseEvent) {
|
|
|
|
e.BaseEvent = b
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ApprovedEvent) Payload() any {
|
2023-04-19 08:46:02 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ApprovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-04-19 08:46:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApprovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
2024-05-16 05:07:56 +00:00
|
|
|
userID,
|
|
|
|
userOrgID string,
|
2023-12-20 12:21:08 +00:00
|
|
|
userAuthMethods []domain.UserAuthMethodType,
|
|
|
|
authTime time.Time,
|
2024-05-16 05:07:56 +00:00
|
|
|
preferredLanguage *language.Tag,
|
|
|
|
userAgent *domain.UserAgent,
|
2024-09-03 13:19:00 +00:00
|
|
|
sessionID string,
|
2023-04-19 08:46:02 +00:00
|
|
|
) *ApprovedEvent {
|
|
|
|
return &ApprovedEvent{
|
2024-09-03 13:19:00 +00:00
|
|
|
BaseEvent: eventstore.NewBaseEventForPush(
|
2023-04-19 08:46:02 +00:00
|
|
|
ctx, aggregate, ApprovedEventType,
|
|
|
|
),
|
2024-09-03 13:19:00 +00:00
|
|
|
UserID: userID,
|
|
|
|
UserOrgID: userOrgID,
|
|
|
|
UserAuthMethods: userAuthMethods,
|
|
|
|
AuthTime: authTime,
|
|
|
|
PreferredLanguage: preferredLanguage,
|
|
|
|
UserAgent: userAgent,
|
|
|
|
SessionID: sessionID,
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CanceledEvent struct {
|
2023-11-03 11:02:17 +00:00
|
|
|
*eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-04-19 08:46:02 +00:00
|
|
|
Reason domain.DeviceAuthCanceled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *CanceledEvent) SetBaseEvent(b *eventstore.BaseEvent) {
|
|
|
|
e.BaseEvent = b
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *CanceledEvent) Payload() any {
|
2023-04-19 08:46:02 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *CanceledEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2023-04-19 08:46:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCanceledEvent(ctx context.Context, aggregate *eventstore.Aggregate, reason domain.DeviceAuthCanceled) *CanceledEvent {
|
|
|
|
return &CanceledEvent{eventstore.NewBaseEventForPush(ctx, aggregate, CanceledEventType), reason}
|
|
|
|
}
|
2024-05-16 05:07:56 +00:00
|
|
|
|
|
|
|
type DoneEvent struct {
|
|
|
|
*eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *DoneEvent) SetBaseEvent(b *eventstore.BaseEvent) {
|
|
|
|
e.BaseEvent = b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *DoneEvent) Payload() any {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *DoneEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDoneEvent(ctx context.Context, aggregate *eventstore.Aggregate) *DoneEvent {
|
|
|
|
return &DoneEvent{eventstore.NewBaseEventForPush(ctx, aggregate, DoneEventType)}
|
|
|
|
}
|