2023-04-19 11:46:02 +03:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 12:19:10 +02:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2023-04-19 11:46:02 +03:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/deviceauth"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-04-19 11:46:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-04-16 11:34:38 +03:00
|
|
|
DeviceAuthRequestProjectionTable = "projections.device_auth_requests2"
|
2023-04-19 11:46:02 +03:00
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnClientID = "client_id"
|
|
|
|
DeviceAuthRequestColumnDeviceCode = "device_code"
|
|
|
|
DeviceAuthRequestColumnUserCode = "user_code"
|
|
|
|
DeviceAuthRequestColumnScopes = "scopes"
|
2024-04-03 09:06:21 +03:00
|
|
|
DeviceAuthRequestColumnAudience = "audience"
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnCreationDate = "creation_date"
|
|
|
|
DeviceAuthRequestColumnChangeDate = "change_date"
|
|
|
|
DeviceAuthRequestColumnSequence = "sequence"
|
|
|
|
DeviceAuthRequestColumnInstanceID = "instance_id"
|
2023-04-19 11:46:02 +03:00
|
|
|
)
|
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
// deviceAuthRequestProjection holds device authorization requests
|
|
|
|
// and makes them search-able by User Code.
|
|
|
|
// In principle the projected data is only needed during user login.
|
|
|
|
// Device Token logic uses the eventstore directly.
|
|
|
|
type deviceAuthRequestProjection struct{}
|
2023-10-19 12:19:10 +02:00
|
|
|
|
|
|
|
func newDeviceAuthProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
2023-12-20 14:21:08 +02:00
|
|
|
return handler.NewHandler(ctx, &config, new(deviceAuthRequestProjection))
|
2023-10-19 12:19:10 +02:00
|
|
|
}
|
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
func (*deviceAuthRequestProjection) Name() string {
|
|
|
|
return DeviceAuthRequestProjectionTable
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
func (*deviceAuthRequestProjection) Init() *old_handler.Check {
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
2023-12-20 14:21:08 +02:00
|
|
|
handler.NewColumn(DeviceAuthRequestColumnClientID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(DeviceAuthRequestColumnDeviceCode, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(DeviceAuthRequestColumnUserCode, handler.ColumnTypeText),
|
2024-04-16 11:34:38 +03:00
|
|
|
handler.NewColumn(DeviceAuthRequestColumnScopes, handler.ColumnTypeTextArray, handler.Nullable()),
|
|
|
|
handler.NewColumn(DeviceAuthRequestColumnAudience, handler.ColumnTypeTextArray, handler.Nullable()),
|
2023-12-20 14:21:08 +02:00
|
|
|
handler.NewColumn(DeviceAuthRequestColumnCreationDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(DeviceAuthRequestColumnChangeDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(DeviceAuthRequestColumnSequence, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(DeviceAuthRequestColumnInstanceID, handler.ColumnTypeText),
|
2023-04-19 11:46:02 +03:00
|
|
|
},
|
2023-12-20 14:21:08 +02:00
|
|
|
handler.NewPrimaryKey(DeviceAuthRequestColumnInstanceID, DeviceAuthRequestColumnDeviceCode),
|
|
|
|
handler.WithIndex(handler.NewIndex("user_code", []string{DeviceAuthRequestColumnInstanceID, DeviceAuthRequestColumnUserCode})),
|
2023-04-19 11:46:02 +03:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
func (p *deviceAuthRequestProjection) Reducers() []handler.AggregateReducer {
|
2023-04-19 11:46:02 +03:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: deviceauth.AggregateType,
|
2023-10-19 12:19:10 +02:00
|
|
|
EventReducers: []handler.EventReducer{
|
2023-04-19 11:46:02 +03:00
|
|
|
{
|
|
|
|
Event: deviceauth.AddedEventType,
|
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: deviceauth.ApprovedEventType,
|
2023-12-20 14:21:08 +02:00
|
|
|
Reduce: p.reduceDoneEvents,
|
2023-04-19 11:46:02 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: deviceauth.CanceledEventType,
|
2023-12-20 14:21:08 +02:00
|
|
|
Reduce: p.reduceDoneEvents,
|
2023-04-19 11:46:02 +03:00
|
|
|
},
|
2024-05-16 08:07:56 +03:00
|
|
|
{
|
|
|
|
Event: deviceauth.DoneEventType,
|
|
|
|
Reduce: p.reduceDoneEvents,
|
|
|
|
},
|
2023-04-19 11:46:02 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
func (p *deviceAuthRequestProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2023-04-19 11:46:02 +03:00
|
|
|
e, ok := event.(*deviceauth.AddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-chu6O", "reduce.wrong.event.type %T != %s", event, deviceauth.AddedEventType)
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return handler.NewCreateStatement(
|
2023-04-19 11:46:02 +03:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
2023-12-20 14:21:08 +02:00
|
|
|
handler.NewCol(DeviceAuthRequestColumnClientID, e.ClientID),
|
|
|
|
handler.NewCol(DeviceAuthRequestColumnDeviceCode, e.DeviceCode),
|
|
|
|
handler.NewCol(DeviceAuthRequestColumnUserCode, e.UserCode),
|
|
|
|
handler.NewCol(DeviceAuthRequestColumnScopes, e.Scopes),
|
2024-04-03 09:06:21 +03:00
|
|
|
handler.NewCol(DeviceAuthRequestColumnAudience, e.Audience),
|
2023-12-20 14:21:08 +02:00
|
|
|
handler.NewCol(DeviceAuthRequestColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(DeviceAuthRequestColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(DeviceAuthRequestColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(DeviceAuthRequestColumnInstanceID, e.Aggregate().InstanceID),
|
2023-04-19 11:46:02 +03:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
// reduceDoneEvents removes the device auth request from the projection.
|
|
|
|
func (p *deviceAuthRequestProjection) reduceDoneEvents(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
switch event.(type) {
|
2024-05-16 08:07:56 +03:00
|
|
|
case *deviceauth.ApprovedEvent, *deviceauth.CanceledEvent, *deviceauth.DoneEvent:
|
2023-12-20 14:21:08 +02:00
|
|
|
return handler.NewDeleteStatement(event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(DeviceAuthRequestColumnInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(DeviceAuthRequestColumnDeviceCode, event.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
2023-04-19 11:46:02 +03:00
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
default:
|
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-eeS8d", "reduce.wrong.event.type %T", event)
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
}
|