zitadel/internal/user/repository/view/model/machine_key.go
Silvan 5a8cafcae5
fix: JWT Profile (#748)
* fix: correct env var for tracing type

* fix: local env tracing

* fix: key in detail as string

* fix: implement storage

* fix: machine key by id
fix: store public key as bytes instead of crypto value

* update oidc pkg

* dont check origins for service account tokens

* fix: scopes

* fix: dependencies

* fix: dependencies

* fix: remove unused code

* fix: variable naming

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2020-09-17 08:49:33 +02:00

85 lines
2.4 KiB
Go

package model
import (
"encoding/json"
"time"
"github.com/caos/logging"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/user/model"
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
)
const (
MachineKeyKeyID = "id"
MachineKeyKeyUserID = "user_id"
)
type MachineKeyView struct {
ID string `json:"keyId" gorm:"column:id;primary_key"`
UserID string `json:"-" gorm:"column:user_id;primary_key"`
Type int32 `json:"type" gorm:"column:machine_type"`
ExpirationDate time.Time `json:"expirationDate" gorm:"column:expiration_date"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
PublicKey []byte `json:"publicKey" gorm:"column:public_key"`
}
func MachineKeyViewFromModel(key *model.MachineKeyView) *MachineKeyView {
return &MachineKeyView{
ID: key.ID,
UserID: key.UserID,
Type: int32(key.Type),
ExpirationDate: key.ExpirationDate,
Sequence: key.Sequence,
CreationDate: key.CreationDate,
}
}
func MachineKeyToModel(key *MachineKeyView) *model.MachineKeyView {
return &model.MachineKeyView{
ID: key.ID,
UserID: key.UserID,
Type: model.MachineKeyType(key.Type),
ExpirationDate: key.ExpirationDate,
Sequence: key.Sequence,
CreationDate: key.CreationDate,
PublicKey: key.PublicKey,
}
}
func MachineKeysToModel(keys []*MachineKeyView) []*model.MachineKeyView {
result := make([]*model.MachineKeyView, len(keys))
for i, key := range keys {
result[i] = MachineKeyToModel(key)
}
return result
}
func (k *MachineKeyView) AppendEvent(event *models.Event) (err error) {
k.Sequence = event.Sequence
switch event.Type {
case es_model.MachineKeyAdded:
k.setRootData(event)
k.CreationDate = event.CreationDate
err = k.SetData(event)
}
return err
}
func (k *MachineKeyView) setRootData(event *models.Event) {
k.UserID = event.AggregateID
}
func (r *MachineKeyView) SetData(event *models.Event) error {
if err := json.Unmarshal(event.Data, r); err != nil {
logging.Log("EVEN-Sj90d").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-lub6s", "Could not unmarshal data")
}
return nil
}