|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +type HashingType string |
| 9 | + |
| 10 | +const ( |
| 11 | + HashingSoloType HashingType = "solo" |
| 12 | + HashingCustomType HashingType = "custom" |
| 13 | +) |
| 14 | + |
| 15 | +type EncryptedValue struct { |
| 16 | + HashingType HashingType `json:"hashing_type"` |
| 17 | + Encrypted *string `json:"encrypted,omitempty"` |
| 18 | + Plaintext *string `json:"plaintext,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +type EncryptedValuesManager struct { |
| 22 | + preferServerState bool `json:"-"` |
| 23 | + EncryptedValues map[string]*EncryptedValue `json:"values"` |
| 24 | +} |
| 25 | + |
| 26 | +type EncryptedHashingTypeMismatchError struct { |
| 27 | + key string |
| 28 | + expected HashingType |
| 29 | + actual HashingType |
| 30 | +} |
| 31 | + |
| 32 | +func (o EncryptedHashingTypeMismatchError) Error() string { |
| 33 | + return fmt.Sprintf("unexpected hashing type for key '%s': %s != %s", o.key, o.expected, o.actual) |
| 34 | +} |
| 35 | + |
| 36 | +func NewEncryptedValuesManager(payload []byte, preferServerState bool) (*EncryptedValuesManager, error) { |
| 37 | + manager := &EncryptedValuesManager{ |
| 38 | + preferServerState: preferServerState, |
| 39 | + } |
| 40 | + |
| 41 | + if payload != nil { |
| 42 | + err := json.Unmarshal(payload, manager) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + } else { |
| 47 | + manager.EncryptedValues = make(map[string]*EncryptedValue) |
| 48 | + } |
| 49 | + |
| 50 | + return manager, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (o EncryptedValuesManager) PreferServerState() bool { |
| 54 | + return o.preferServerState |
| 55 | +} |
| 56 | + |
| 57 | +func (o *EncryptedValuesManager) StorePlaintextValue(key string, hashing_type HashingType, value string) error { |
| 58 | + values, found := o.EncryptedValues[key] |
| 59 | + if !found { |
| 60 | + values = &EncryptedValue{ |
| 61 | + Plaintext: &value, |
| 62 | + HashingType: hashing_type, |
| 63 | + } |
| 64 | + o.EncryptedValues[key] = values |
| 65 | + } else if values.HashingType != hashing_type { |
| 66 | + return EncryptedHashingTypeMismatchError{ |
| 67 | + key: key, |
| 68 | + expected: hashing_type, |
| 69 | + actual: values.HashingType, |
| 70 | + } |
| 71 | + } else { |
| 72 | + values.Plaintext = &value |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func (o *EncryptedValuesManager) StoreEncryptedValue(key string, hashing_type HashingType, value string) error { |
| 79 | + values, found := o.EncryptedValues[key] |
| 80 | + if !found { |
| 81 | + values = &EncryptedValue{ |
| 82 | + Encrypted: &value, |
| 83 | + HashingType: hashing_type, |
| 84 | + } |
| 85 | + o.EncryptedValues[key] = values |
| 86 | + } else if values.HashingType != hashing_type { |
| 87 | + return EncryptedHashingTypeMismatchError{ |
| 88 | + key: key, |
| 89 | + expected: hashing_type, |
| 90 | + actual: values.HashingType, |
| 91 | + } |
| 92 | + } else { |
| 93 | + values.Encrypted = &value |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (o EncryptedValuesManager) GetPlaintextValue(key string) (string, bool) { |
| 100 | + if values, found := o.EncryptedValues[key]; !found { |
| 101 | + return "", false |
| 102 | + } else if values.Plaintext == nil { |
| 103 | + return "", false |
| 104 | + } else { |
| 105 | + return *values.Plaintext, true |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func (o EncryptedValuesManager) GetEncryptedValue(key string) (string, bool) { |
| 110 | + if values, found := o.EncryptedValues[key]; !found { |
| 111 | + return "", false |
| 112 | + } else if values.Encrypted == nil { |
| 113 | + return "", false |
| 114 | + } else { |
| 115 | + return *values.Encrypted, true |
| 116 | + } |
| 117 | +} |
0 commit comments