mirror of
https://github.com/warmbly/warmbly.git
synced 2026-10-28 08:02:30 +00:00
54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
package cipher
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
"github.com/google/uuid"
|
|
"github.com/warmbly/warmbly/internal/repository"
|
|
)
|
|
|
|
type Cipher struct {
|
|
plainDEK []byte
|
|
}
|
|
|
|
func (s *cipherService) Cipher(ctx context.Context, userID uuid.UUID) (*Cipher, error) {
|
|
key, err := s.getDecryptedKey(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
encDEKB64, err := s.userEncryptedKeysRepository.Get(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if encDEKB64 == "" {
|
|
var encryptedDEK string
|
|
key, encryptedDEK, err = s.kms.GenerateDataKey(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := s.userEncryptedKeysRepository.Put(context.TODO(), repository.UserEncryptedKeysItem{
|
|
UserID: userID.String(),
|
|
EncryptedDataKey: encryptedDEK,
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
key, err = s.kms.GetDecryptedKey(ctx, encDEKB64)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if err := s.saveDecryptedKey(ctx, userID, key); err != nil {
|
|
sentry.CaptureException(err)
|
|
}
|
|
|
|
return &Cipher{
|
|
plainDEK: key,
|
|
}, nil
|
|
}
|