2023-04-19 08:46:02 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-12-20 12:21:08 +00:00
|
|
|
func (c *Commands) AddDeviceAuth(ctx context.Context, clientID, deviceCode, userCode string, expires time.Time, scopes []string) (*domain.ObjectDetails, error) {
|
|
|
|
aggr := deviceauth.NewAggregate(deviceCode, authz.GetInstance(ctx).InstanceID())
|
|
|
|
model := NewDeviceAuthWriteModel(deviceCode, aggr.ResourceOwner)
|
2023-04-19 08:46:02 +00:00
|
|
|
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, deviceauth.NewAddedEvent(
|
|
|
|
ctx,
|
|
|
|
aggr,
|
|
|
|
clientID,
|
|
|
|
deviceCode,
|
|
|
|
userCode,
|
|
|
|
expires,
|
|
|
|
scopes,
|
|
|
|
))
|
|
|
|
if err != nil {
|
2023-12-20 12:21:08 +00:00
|
|
|
return nil, err
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
err = AppendAndReduce(model, pushedEvents...)
|
|
|
|
if err != nil {
|
2023-12-20 12:21:08 +00:00
|
|
|
return nil, err
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 12:21:08 +00:00
|
|
|
return writeModelToObjectDetails(&model.WriteModel), nil
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 12:21:08 +00:00
|
|
|
func (c *Commands) ApproveDeviceAuth(ctx context.Context, deviceCode, subject string, authMethods []domain.UserAuthMethodType, authTime time.Time) (*domain.ObjectDetails, error) {
|
|
|
|
model, err := c.getDeviceAuthWriteModelByDeviceCode(ctx, deviceCode)
|
2023-04-19 08:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !model.State.Exists() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-Hief9", "Errors.DeviceAuth.NotFound")
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
aggr := deviceauth.NewAggregate(model.AggregateID, model.InstanceID)
|
|
|
|
|
2023-12-20 12:21:08 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, deviceauth.NewApprovedEvent(ctx, aggr, subject, authMethods, authTime))
|
2023-04-19 08:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(model, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModelToObjectDetails(&model.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) CancelDeviceAuth(ctx context.Context, id string, reason domain.DeviceAuthCanceled) (*domain.ObjectDetails, error) {
|
2023-12-20 12:21:08 +00:00
|
|
|
model, err := c.getDeviceAuthWriteModelByDeviceCode(ctx, id)
|
2023-04-19 08:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !model.State.Exists() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-gee5A", "Errors.DeviceAuth.NotFound")
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
aggr := deviceauth.NewAggregate(model.AggregateID, model.InstanceID)
|
|
|
|
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, deviceauth.NewCanceledEvent(ctx, aggr, reason))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(model, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModelToObjectDetails(&model.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2023-12-20 12:21:08 +00:00
|
|
|
func (c *Commands) getDeviceAuthWriteModelByDeviceCode(ctx context.Context, deviceCode string) (*DeviceAuthWriteModel, error) {
|
|
|
|
model := &DeviceAuthWriteModel{WriteModel: eventstore.WriteModel{AggregateID: deviceCode}}
|
2023-04-19 08:46:02 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, model)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return model, nil
|
|
|
|
}
|