2023-04-19 08:46:02 +00:00
|
|
|
package deviceauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
UniqueUserCode = "user_code"
|
|
|
|
UniqueDeviceCode = "device_code"
|
|
|
|
DuplicateUserCode = "Errors.DeviceUserCode.AlreadyExists"
|
|
|
|
DuplicateDeviceCode = "Errors.DeviceCode.AlreadyExists"
|
|
|
|
)
|
|
|
|
|
|
|
|
func deviceCodeUniqueField(clientID, deviceCode string) string {
|
|
|
|
return strings.Join([]string{clientID, deviceCode}, ":")
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func NewAddUniqueConstraints(clientID, deviceCode, userCode string) []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{
|
2023-04-19 08:46:02 +00:00
|
|
|
eventstore.NewAddEventUniqueConstraint(
|
|
|
|
UniqueDeviceCode,
|
|
|
|
deviceCodeUniqueField(clientID, deviceCode),
|
|
|
|
DuplicateDeviceCode,
|
|
|
|
),
|
|
|
|
eventstore.NewAddEventUniqueConstraint(
|
|
|
|
UniqueUserCode,
|
|
|
|
userCode,
|
|
|
|
DuplicateUserCode,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func NewRemoveUniqueConstraints(clientID, deviceCode, userCode string) []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{
|
|
|
|
eventstore.NewRemoveUniqueConstraint(
|
2023-04-19 08:46:02 +00:00
|
|
|
UniqueDeviceCode,
|
|
|
|
deviceCodeUniqueField(clientID, deviceCode),
|
|
|
|
),
|
2023-10-19 10:19:10 +00:00
|
|
|
eventstore.NewRemoveUniqueConstraint(
|
2023-04-19 08:46:02 +00:00
|
|
|
UniqueUserCode,
|
|
|
|
userCode,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|