2021-01-04 14:52:13 +01:00
|
|
|
package domain
|
2020-12-10 16:18:52 +01:00
|
|
|
|
2021-02-08 11:30:30 +01:00
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MachineKey struct {
|
|
|
|
models.ObjectRoot
|
|
|
|
|
|
|
|
KeyID string
|
|
|
|
Type MachineKeyType
|
|
|
|
ExpirationDate time.Time
|
|
|
|
PrivateKey []byte
|
|
|
|
PublicKey []byte
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:18:52 +01:00
|
|
|
type MachineKeyType int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
MachineKeyTypeNONE = iota
|
|
|
|
MachineKeyTypeJSON
|
|
|
|
|
|
|
|
keyCount
|
|
|
|
)
|
|
|
|
|
2021-02-08 11:30:30 +01:00
|
|
|
type MachineKeyState int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
MachineKeyStateUnspecified MachineKeyState = iota
|
|
|
|
MachineKeyStateActive
|
|
|
|
MachineKeyStateRemoved
|
|
|
|
|
|
|
|
machineKeyStateCount
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f MachineKeyState) Valid() bool {
|
|
|
|
return f >= 0 && f < machineKeyStateCount
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:18:52 +01:00
|
|
|
func (f MachineKeyType) Valid() bool {
|
|
|
|
return f >= 0 && f < keyCount
|
|
|
|
}
|
2021-02-08 11:30:30 +01:00
|
|
|
|
|
|
|
func (key *MachineKey) GenerateNewMachineKeyPair(keySize int) error {
|
|
|
|
privateKey, publicKey, err := crypto.GenerateKeyPair(keySize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key.PublicKey, err = crypto.PublicKeyToBytes(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key.PrivateKey = crypto.PrivateKeyToBytes(privateKey)
|
|
|
|
return nil
|
|
|
|
}
|