2023-04-19 08:46:02 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/deviceauth"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DeviceAuthWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2023-12-20 12:21:08 +00:00
|
|
|
ClientID string
|
|
|
|
DeviceCode string
|
|
|
|
UserCode string
|
|
|
|
Expires time.Time
|
|
|
|
Scopes []string
|
|
|
|
State domain.DeviceAuthState
|
|
|
|
Subject string
|
|
|
|
UserAuthMethods []domain.UserAuthMethodType
|
|
|
|
AuthTime time.Time
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 12:21:08 +00:00
|
|
|
func NewDeviceAuthWriteModel(deviceCode, resourceOwner string) *DeviceAuthWriteModel {
|
2023-04-19 08:46:02 +00:00
|
|
|
return &DeviceAuthWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2023-12-20 12:21:08 +00:00
|
|
|
AggregateID: deviceCode,
|
2023-04-19 08:46:02 +00:00
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DeviceAuthWriteModel) Reduce() error {
|
|
|
|
for _, event := range m.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *deviceauth.AddedEvent:
|
|
|
|
m.ClientID = e.ClientID
|
|
|
|
m.DeviceCode = e.DeviceCode
|
|
|
|
m.UserCode = e.UserCode
|
|
|
|
m.Expires = e.Expires
|
|
|
|
m.Scopes = e.Scopes
|
|
|
|
m.State = e.State
|
|
|
|
case *deviceauth.ApprovedEvent:
|
|
|
|
m.State = domain.DeviceAuthStateApproved
|
2023-12-20 12:21:08 +00:00
|
|
|
m.Subject = e.Subject
|
|
|
|
m.UserAuthMethods = e.UserAuthMethods
|
|
|
|
m.AuthTime = e.AuthTime
|
2023-04-19 08:46:02 +00:00
|
|
|
case *deviceauth.CanceledEvent:
|
|
|
|
m.State = e.Reason.State()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DeviceAuthWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2023-12-20 12:21:08 +00:00
|
|
|
ResourceOwner(m.ResourceOwner).
|
2023-04-19 08:46:02 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(deviceauth.AggregateType).
|
|
|
|
AggregateIDs(m.AggregateID).
|
2023-12-20 12:21:08 +00:00
|
|
|
EventTypes(
|
|
|
|
deviceauth.AddedEventType,
|
|
|
|
deviceauth.ApprovedEventType,
|
|
|
|
deviceauth.CanceledEventType,
|
|
|
|
).
|
2023-04-19 08:46:02 +00:00
|
|
|
Builder()
|
|
|
|
}
|