2023-04-19 11:46:02 +03:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2024-05-16 08:07:56 +03:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2023-04-19 11:46:02 +03:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/deviceauth"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DeviceAuthWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
2024-05-16 08:07:56 +03:00
|
|
|
aggregate *eventstore.Aggregate
|
2023-04-19 11:46:02 +03:00
|
|
|
|
2024-05-16 08:07:56 +03:00
|
|
|
ClientID string
|
|
|
|
DeviceCode string
|
|
|
|
UserCode string
|
|
|
|
Expires time.Time
|
|
|
|
Scopes []string
|
|
|
|
Audience []string
|
|
|
|
State domain.DeviceAuthState
|
|
|
|
UserID string
|
|
|
|
UserOrgID string
|
|
|
|
UserAuthMethods []domain.UserAuthMethodType
|
|
|
|
AuthTime time.Time
|
|
|
|
PreferredLanguage *language.Tag
|
|
|
|
UserAgent *domain.UserAgent
|
|
|
|
NeedRefreshToken bool
|
2024-09-03 15:19:00 +02:00
|
|
|
SessionID string
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
func NewDeviceAuthWriteModel(deviceCode, resourceOwner string) *DeviceAuthWriteModel {
|
2023-04-19 11:46:02 +03:00
|
|
|
return &DeviceAuthWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2023-12-20 14:21:08 +02:00
|
|
|
AggregateID: deviceCode,
|
2023-04-19 11:46:02 +03:00
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
2024-05-16 08:07:56 +03:00
|
|
|
aggregate: deviceauth.NewAggregate(deviceCode, resourceOwner),
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-05-16 08:07:56 +03:00
|
|
|
m.Audience = e.Audience
|
2023-04-19 11:46:02 +03:00
|
|
|
m.State = e.State
|
2024-05-16 08:07:56 +03:00
|
|
|
m.NeedRefreshToken = e.NeedRefreshToken
|
2023-04-19 11:46:02 +03:00
|
|
|
case *deviceauth.ApprovedEvent:
|
|
|
|
m.State = domain.DeviceAuthStateApproved
|
2024-05-16 08:07:56 +03:00
|
|
|
m.UserID = e.UserID
|
|
|
|
m.UserOrgID = e.UserOrgID
|
2023-12-20 14:21:08 +02:00
|
|
|
m.UserAuthMethods = e.UserAuthMethods
|
|
|
|
m.AuthTime = e.AuthTime
|
2024-05-16 08:07:56 +03:00
|
|
|
m.PreferredLanguage = e.PreferredLanguage
|
|
|
|
m.UserAgent = e.UserAgent
|
2024-09-03 15:19:00 +02:00
|
|
|
m.SessionID = e.SessionID
|
2023-04-19 11:46:02 +03:00
|
|
|
case *deviceauth.CanceledEvent:
|
|
|
|
m.State = e.Reason.State()
|
2024-05-16 08:07:56 +03:00
|
|
|
case *deviceauth.DoneEvent:
|
|
|
|
m.State = domain.DeviceAuthStateDone
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *DeviceAuthWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2023-12-20 14:21:08 +02:00
|
|
|
ResourceOwner(m.ResourceOwner).
|
2023-04-19 11:46:02 +03:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(deviceauth.AggregateType).
|
|
|
|
AggregateIDs(m.AggregateID).
|
2023-12-20 14:21:08 +02:00
|
|
|
EventTypes(
|
|
|
|
deviceauth.AddedEventType,
|
|
|
|
deviceauth.ApprovedEventType,
|
|
|
|
deviceauth.CanceledEventType,
|
|
|
|
).
|
2023-04-19 11:46:02 +03:00
|
|
|
Builder()
|
|
|
|
}
|