2023-04-19 08:46:02 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2023-04-19 08:46:02 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/deviceauth"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-04-19 08:46:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DeviceAuthProjectionTable = "projections.device_authorizations"
|
|
|
|
|
|
|
|
DeviceAuthColumnID = "id"
|
|
|
|
DeviceAuthColumnClientID = "client_id"
|
|
|
|
DeviceAuthColumnDeviceCode = "device_code"
|
|
|
|
DeviceAuthColumnUserCode = "user_code"
|
|
|
|
DeviceAuthColumnExpires = "expires"
|
|
|
|
DeviceAuthColumnScopes = "scopes"
|
|
|
|
DeviceAuthColumnState = "state"
|
|
|
|
DeviceAuthColumnSubject = "subject"
|
|
|
|
|
|
|
|
DeviceAuthColumnCreationDate = "creation_date"
|
|
|
|
DeviceAuthColumnChangeDate = "change_date"
|
|
|
|
DeviceAuthColumnSequence = "sequence"
|
|
|
|
DeviceAuthColumnInstanceID = "instance_id"
|
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type deviceAuthProjection struct{}
|
|
|
|
|
|
|
|
func newDeviceAuthProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, new(deviceAuthProjection))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*deviceAuthProjection) Name() string {
|
|
|
|
return DeviceAuthProjectionTable
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (*deviceAuthProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(DeviceAuthColumnID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(DeviceAuthColumnClientID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(DeviceAuthColumnDeviceCode, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(DeviceAuthColumnUserCode, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(DeviceAuthColumnExpires, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(DeviceAuthColumnScopes, handler.ColumnTypeTextArray),
|
|
|
|
handler.NewColumn(DeviceAuthColumnState, handler.ColumnTypeEnum, handler.Default(domain.DeviceAuthStateInitiated)),
|
|
|
|
handler.NewColumn(DeviceAuthColumnSubject, handler.ColumnTypeText, handler.Default("")),
|
|
|
|
handler.NewColumn(DeviceAuthColumnCreationDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(DeviceAuthColumnChangeDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(DeviceAuthColumnSequence, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(DeviceAuthColumnInstanceID, handler.ColumnTypeText),
|
2023-04-19 08:46:02 +00:00
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewPrimaryKey(DeviceAuthColumnInstanceID, DeviceAuthColumnID),
|
|
|
|
handler.WithIndex(handler.NewIndex("user_code", []string{DeviceAuthColumnInstanceID, DeviceAuthColumnUserCode})),
|
|
|
|
handler.WithIndex(handler.NewIndex("device_code", []string{DeviceAuthColumnInstanceID, DeviceAuthColumnClientID, DeviceAuthColumnDeviceCode})),
|
2023-04-19 08:46:02 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (p *deviceAuthProjection) Reducers() []handler.AggregateReducer {
|
2023-04-19 08:46:02 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: deviceauth.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2023-04-19 08:46:02 +00:00
|
|
|
{
|
|
|
|
Event: deviceauth.AddedEventType,
|
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: deviceauth.ApprovedEventType,
|
|
|
|
Reduce: p.reduceAppoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: deviceauth.CanceledEventType,
|
|
|
|
Reduce: p.reduceCanceled,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: deviceauth.RemovedEventType,
|
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *deviceAuthProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*deviceauth.AddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-chu6O", "reduce.wrong.event.type %T != %s", event, deviceauth.AddedEventType)
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewCreateStatement(
|
2023-04-19 08:46:02 +00:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(DeviceAuthColumnID, e.Aggregate().ID),
|
|
|
|
handler.NewCol(DeviceAuthColumnClientID, e.ClientID),
|
|
|
|
handler.NewCol(DeviceAuthColumnDeviceCode, e.DeviceCode),
|
|
|
|
handler.NewCol(DeviceAuthColumnUserCode, e.UserCode),
|
|
|
|
handler.NewCol(DeviceAuthColumnExpires, e.Expires),
|
|
|
|
handler.NewCol(DeviceAuthColumnScopes, e.Scopes),
|
|
|
|
handler.NewCol(DeviceAuthColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(DeviceAuthColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(DeviceAuthColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(DeviceAuthColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *deviceAuthProjection) reduceAppoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*deviceauth.ApprovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-kei0A", "reduce.wrong.event.type %T != %s", event, deviceauth.ApprovedEventType)
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(e,
|
2023-04-19 08:46:02 +00:00
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(DeviceAuthColumnState, domain.DeviceAuthStateApproved),
|
|
|
|
handler.NewCol(DeviceAuthColumnSubject, e.Subject),
|
|
|
|
handler.NewCol(DeviceAuthColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(DeviceAuthColumnSequence, e.Sequence()),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(DeviceAuthColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(DeviceAuthColumnID, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *deviceAuthProjection) reduceCanceled(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*deviceauth.CanceledEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-eeS8d", "reduce.wrong.event.type %T != %s", event, deviceauth.CanceledEventType)
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(e,
|
2023-04-19 08:46:02 +00:00
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(DeviceAuthColumnState, e.Reason.State()),
|
|
|
|
handler.NewCol(DeviceAuthColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(DeviceAuthColumnSequence, e.Sequence()),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(DeviceAuthColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(DeviceAuthColumnID, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *deviceAuthProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*deviceauth.RemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-AJi1u", "reduce.wrong.event.type %T != %s", event, deviceauth.RemovedEventType)
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(e,
|
2023-04-19 08:46:02 +00:00
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(DeviceAuthColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(DeviceAuthColumnID, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|