zitadel/internal/v2/repository/user/machine_key.go
Fabi 28bfe72930
feat: uniqueness (#1190)
* feat: uniqueness on events

* fix: some tests

* fix: add unique username

* fix: nice error message

* fix: add unique test

* fix: add unique test

* fix: add unique constraint to events

* fix: correct unique constraint on user events

* fix: remove user constraint

* fix: add unique constraints

* fix: add unique constraints

* fix: add unique constraints

* fix: unnique constraints without interface

* fix: unique idp config

* fix: unique constraint comments

* fix: unique constraints in one table

* fix: unique constraints error

* fix: fix unique constraint on create user

* fix: fix unique constraint on create project

* fix: fix unique constraint on idp configs
2021-01-21 10:49:38 +01:00

105 lines
2.6 KiB
Go

package user
import (
"context"
"encoding/json"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
"github.com/caos/zitadel/internal/v2/domain"
"time"
)
const (
machineKeyEventPrefix = machineEventPrefix + "key."
MachineKeyAddedEventType = machineKeyEventPrefix + "added"
MachineKeyRemovedEventType = machineKeyEventPrefix + "removed"
)
type MachineKeyAddedEvent struct {
eventstore.BaseEvent `json:"-"`
KeyID string `json:"keyId,omitempty"`
KeyType domain.MachineKeyType `json:"type,omitempty"`
ExpirationDate time.Time `json:"expirationDate,omitempty"`
PublicKey []byte `json:"publicKey,omitempty"`
}
func (e *MachineKeyAddedEvent) Data() interface{} {
return e
}
func (e *MachineKeyAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewMachineKeyAddedEvent(
ctx context.Context,
keyID string,
keyType domain.MachineKeyType,
expirationDate time.Time,
publicKey []byte,
) *MachineKeyAddedEvent {
return &MachineKeyAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
MachineKeyAddedEventType,
),
KeyID: keyID,
KeyType: keyType,
ExpirationDate: expirationDate,
PublicKey: publicKey,
}
}
func MachineKeyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
machineKeyAdded := &MachineKeyAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, machineKeyAdded)
if err != nil {
return nil, errors.ThrowInternal(err, "USER-p0ovS", "unable to unmarshal machine key removed")
}
return machineKeyAdded, nil
}
type MachineKeyRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
KeyID string `json:"keyId,omitempty"`
}
func (e *MachineKeyRemovedEvent) Data() interface{} {
return e
}
func (e *MachineKeyRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewMachineKeyRemovedEvent(
ctx context.Context,
keyID string,
) *MachineKeyRemovedEvent {
return &MachineKeyRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
MachineKeyRemovedEventType,
),
KeyID: keyID,
}
}
func MachineKeyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
machineRemoved := &MachineKeyRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, machineRemoved)
if err != nil {
return nil, errors.ThrowInternal(err, "USER-5Gm9s", "unable to unmarshal machine key removed")
}
return machineRemoved, nil
}