improve some functions

This commit is contained in:
Livio Amstutz 2020-03-30 09:28:00 +02:00
parent fa750a5068
commit 59dc4dbe85
8 changed files with 81 additions and 84 deletions

View File

@ -74,10 +74,8 @@ func Test_GetUserMethodPermissions(t *testing.T) {
},
},
wantErr: true,
errFunc: func(err error) bool {
return caos_errs.IsUnauthenticated(err)
},
result: []string{"project.read"},
errFunc: caos_errs.IsUnauthenticated,
result: []string{"project.read"},
},
{
name: "No Grants",
@ -393,7 +391,6 @@ func Test_AddRoleContextIDToPerm(t *testing.T) {
}
func Test_ExistisPerm(t *testing.T) {
type args struct {
existing []string
perm string

View File

@ -10,7 +10,7 @@ import (
"github.com/caos/zitadel/internal/errors"
)
var _ EncryptionAlg = (*AESCrypto)(nil)
var _ EncryptionAlgorithm = (*AESCrypto)(nil)
type AESCrypto struct {
keys map[string]string

View File

@ -4,7 +4,7 @@ import (
"golang.org/x/crypto/bcrypt"
)
var _ HashAlg = (*BCrypt)(nil)
var _ HashAlgorithm = (*BCrypt)(nil)
type BCrypt struct {
cost int

View File

@ -24,7 +24,7 @@ type Generator interface {
type EncryptionGenerator struct {
length uint
expiry time.Duration
alg EncryptionAlg
alg EncryptionAlgorithm
runes []rune
}
@ -44,7 +44,7 @@ func (g *EncryptionGenerator) Runes() []rune {
return g.runes
}
func NewEncryptionGenerator(length uint, expiry time.Duration, alg EncryptionAlg, runes []rune) *EncryptionGenerator {
func NewEncryptionGenerator(length uint, expiry time.Duration, alg EncryptionAlgorithm, runes []rune) *EncryptionGenerator {
return &EncryptionGenerator{
length: length,
expiry: expiry,
@ -56,7 +56,7 @@ func NewEncryptionGenerator(length uint, expiry time.Duration, alg EncryptionAlg
type HashGenerator struct {
length uint
expiry time.Duration
alg HashAlg
alg HashAlgorithm
runes []rune
}
@ -76,7 +76,7 @@ func (g *HashGenerator) Runes() []rune {
return g.runes
}
func NewHashGenerator(length uint, expiry time.Duration, alg HashAlg, runes []rune) *HashGenerator {
func NewHashGenerator(length uint, expiry time.Duration, alg HashAlgorithm, runes []rune) *HashGenerator {
return &HashGenerator{
length: length,
expiry: expiry,
@ -106,9 +106,9 @@ func VerifyCode(creationDate time.Time, expiry time.Duration, cryptoCode *Crypto
return errors.ThrowPreconditionFailed(nil, "CODE-QvUQ4P", "verification code is expired")
}
switch alg := g.Alg().(type) {
case EncryptionAlg:
case EncryptionAlgorithm:
return verifyEncryptedCode(cryptoCode, verificationCode, alg)
case HashAlg:
case HashAlgorithm:
return verifyHashedCode(cryptoCode, verificationCode, alg)
}
return errors.ThrowInvalidArgument(nil, "CODE-fW2gNa", "generator alg is not supported")
@ -136,7 +136,7 @@ func generateRandomString(length uint, chars []rune) (string, error) {
return "", nil
}
func verifyEncryptedCode(cryptoCode *CryptoValue, verificationCode string, alg EncryptionAlg) error {
func verifyEncryptedCode(cryptoCode *CryptoValue, verificationCode string, alg EncryptionAlgorithm) error {
code, err := DecryptString(cryptoCode, alg)
if err != nil {
return err
@ -148,6 +148,6 @@ func verifyEncryptedCode(cryptoCode *CryptoValue, verificationCode string, alg E
return nil
}
func verifyHashedCode(cryptoCode *CryptoValue, verificationCode string, alg HashAlg) error {
func verifyHashedCode(cryptoCode *CryptoValue, verificationCode string, alg HashAlgorithm) error {
return CompareHash(cryptoCode, []byte(verificationCode), alg)
}

View File

@ -9,7 +9,7 @@ import (
)
func Test_Encrypted_OK(t *testing.T) {
mCrypto := NewMockEncryptionAlg(gomock.NewController(t))
mCrypto := NewMockEncryptionAlgorithm(gomock.NewController(t))
mCrypto.EXPECT().Algorithm().AnyTimes().Return("enc")
mCrypto.EXPECT().EncryptionKeyID().AnyTimes().Return("id")
mCrypto.EXPECT().DecryptionKeyIDs().AnyTimes().Return([]string{"id"})
@ -35,7 +35,7 @@ func Test_Encrypted_OK(t *testing.T) {
}
func Test_Verify_Encrypted_OK(t *testing.T) {
mCrypto := NewMockEncryptionAlg(gomock.NewController(t))
mCrypto := NewMockEncryptionAlgorithm(gomock.NewController(t))
mCrypto.EXPECT().Algorithm().AnyTimes().Return("enc")
mCrypto.EXPECT().EncryptionKeyID().AnyTimes().Return("id")
mCrypto.EXPECT().DecryptionKeyIDs().AnyTimes().Return([]string{"id"})
@ -57,7 +57,7 @@ func Test_Verify_Encrypted_OK(t *testing.T) {
assert.NoError(t, err)
}
func Test_Verify_Encrypted_Invalid_Err(t *testing.T) {
mCrypto := NewMockEncryptionAlg(gomock.NewController(t))
mCrypto := NewMockEncryptionAlgorithm(gomock.NewController(t))
mCrypto.EXPECT().Algorithm().AnyTimes().Return("enc")
mCrypto.EXPECT().EncryptionKeyID().AnyTimes().Return("id")
mCrypto.EXPECT().DecryptionKeyIDs().AnyTimes().Return([]string{"id"})

View File

@ -13,7 +13,7 @@ type Crypto interface {
Algorithm() string
}
type EncryptionAlg interface {
type EncryptionAlgorithm interface {
Crypto
EncryptionKeyID() string
DecryptionKeyIDs() []string
@ -22,7 +22,7 @@ type EncryptionAlg interface {
DecryptString(hashed []byte, keyID string) (string, error)
}
type HashAlg interface {
type HashAlgorithm interface {
Crypto
Hash(value []byte) ([]byte, error)
CompareHash(hashed, comparer []byte) error
@ -39,15 +39,15 @@ type CryptoType int
func Crypt(value []byte, c Crypto) (*CryptoValue, error) {
switch alg := c.(type) {
case EncryptionAlg:
case EncryptionAlgorithm:
return Encrypt(value, alg)
case HashAlg:
case HashAlgorithm:
return Hash(value, alg)
}
return nil, errors.ThrowInternal(nil, "CRYPT-r4IaHZ", "algorithm not supported")
}
func Encrypt(value []byte, alg EncryptionAlg) (*CryptoValue, error) {
func Encrypt(value []byte, alg EncryptionAlgorithm) (*CryptoValue, error) {
encrypted, err := alg.Encrypt(value)
if err != nil {
return nil, errors.ThrowInternal(err, "CRYPT-qCD0JB", "error encrypting value")
@ -60,21 +60,21 @@ func Encrypt(value []byte, alg EncryptionAlg) (*CryptoValue, error) {
}, nil
}
func Decrypt(value *CryptoValue, alg EncryptionAlg) ([]byte, error) {
if err := checkEncAlg(value, alg); err != nil {
func Decrypt(value *CryptoValue, alg EncryptionAlgorithm) ([]byte, error) {
if err := checkEncryptionAlgorithm(value, alg); err != nil {
return nil, err
}
return alg.Decrypt(value.Crypted, value.KeyID)
}
func DecryptString(value *CryptoValue, alg EncryptionAlg) (string, error) {
if err := checkEncAlg(value, alg); err != nil {
func DecryptString(value *CryptoValue, alg EncryptionAlgorithm) (string, error) {
if err := checkEncryptionAlgorithm(value, alg); err != nil {
return "", err
}
return alg.DecryptString(value.Crypted, value.KeyID)
}
func checkEncAlg(value *CryptoValue, alg EncryptionAlg) error {
func checkEncryptionAlgorithm(value *CryptoValue, alg EncryptionAlgorithm) error {
if value.Algorithm != alg.Algorithm() {
return errors.ThrowInvalidArgument(nil, "CRYPT-Nx7XlT", "value was encrypted with a different key")
}
@ -86,7 +86,7 @@ func checkEncAlg(value *CryptoValue, alg EncryptionAlg) error {
return errors.ThrowInvalidArgument(nil, "CRYPT-Kq12vn", "value was encrypted with a different key")
}
func Hash(value []byte, alg HashAlg) (*CryptoValue, error) {
func Hash(value []byte, alg HashAlgorithm) (*CryptoValue, error) {
hashed, err := alg.Hash(value)
if err != nil {
return nil, errors.ThrowInternal(err, "CRYPT-rBVaJU", "error hashing value")
@ -98,6 +98,6 @@ func Hash(value []byte, alg HashAlg) (*CryptoValue, error) {
}, nil
}
func CompareHash(value *CryptoValue, comparer []byte, alg HashAlg) error {
func CompareHash(value *CryptoValue, comparer []byte, alg HashAlgorithm) error {
return alg.CompareHash(value.Crypted, comparer)
}

View File

@ -46,31 +46,31 @@ func (mr *MockCryptoMockRecorder) Algorithm() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Algorithm", reflect.TypeOf((*MockCrypto)(nil).Algorithm))
}
// MockEncryptionAlg is a mock of EncryptionAlg interface
type MockEncryptionAlg struct {
// MockEncryptionAlgorithm is a mock of EncryptionAlgorithm interface
type MockEncryptionAlgorithm struct {
ctrl *gomock.Controller
recorder *MockEncryptionAlgMockRecorder
recorder *MockEncryptionAlgorithmMockRecorder
}
// MockEncryptionAlgMockRecorder is the mock recorder for MockEncryptionAlg
type MockEncryptionAlgMockRecorder struct {
mock *MockEncryptionAlg
// MockEncryptionAlgorithmMockRecorder is the mock recorder for MockEncryptionAlgorithm
type MockEncryptionAlgorithmMockRecorder struct {
mock *MockEncryptionAlgorithm
}
// NewMockEncryptionAlg creates a new mock instance
func NewMockEncryptionAlg(ctrl *gomock.Controller) *MockEncryptionAlg {
mock := &MockEncryptionAlg{ctrl: ctrl}
mock.recorder = &MockEncryptionAlgMockRecorder{mock}
// NewMockEncryptionAlgorithm creates a new mock instance
func NewMockEncryptionAlgorithm(ctrl *gomock.Controller) *MockEncryptionAlgorithm {
mock := &MockEncryptionAlgorithm{ctrl: ctrl}
mock.recorder = &MockEncryptionAlgorithmMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockEncryptionAlg) EXPECT() *MockEncryptionAlgMockRecorder {
func (m *MockEncryptionAlgorithm) EXPECT() *MockEncryptionAlgorithmMockRecorder {
return m.recorder
}
// Algorithm mocks base method
func (m *MockEncryptionAlg) Algorithm() string {
func (m *MockEncryptionAlgorithm) Algorithm() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Algorithm")
ret0, _ := ret[0].(string)
@ -78,13 +78,13 @@ func (m *MockEncryptionAlg) Algorithm() string {
}
// Algorithm indicates an expected call of Algorithm
func (mr *MockEncryptionAlgMockRecorder) Algorithm() *gomock.Call {
func (mr *MockEncryptionAlgorithmMockRecorder) Algorithm() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Algorithm", reflect.TypeOf((*MockEncryptionAlg)(nil).Algorithm))
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Algorithm", reflect.TypeOf((*MockEncryptionAlgorithm)(nil).Algorithm))
}
// EncryptionKeyID mocks base method
func (m *MockEncryptionAlg) EncryptionKeyID() string {
func (m *MockEncryptionAlgorithm) EncryptionKeyID() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "EncryptionKeyID")
ret0, _ := ret[0].(string)
@ -92,13 +92,13 @@ func (m *MockEncryptionAlg) EncryptionKeyID() string {
}
// EncryptionKeyID indicates an expected call of EncryptionKeyID
func (mr *MockEncryptionAlgMockRecorder) EncryptionKeyID() *gomock.Call {
func (mr *MockEncryptionAlgorithmMockRecorder) EncryptionKeyID() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EncryptionKeyID", reflect.TypeOf((*MockEncryptionAlg)(nil).EncryptionKeyID))
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EncryptionKeyID", reflect.TypeOf((*MockEncryptionAlgorithm)(nil).EncryptionKeyID))
}
// DecryptionKeyIDs mocks base method
func (m *MockEncryptionAlg) DecryptionKeyIDs() []string {
func (m *MockEncryptionAlgorithm) DecryptionKeyIDs() []string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "DecryptionKeyIDs")
ret0, _ := ret[0].([]string)
@ -106,13 +106,13 @@ func (m *MockEncryptionAlg) DecryptionKeyIDs() []string {
}
// DecryptionKeyIDs indicates an expected call of DecryptionKeyIDs
func (mr *MockEncryptionAlgMockRecorder) DecryptionKeyIDs() *gomock.Call {
func (mr *MockEncryptionAlgorithmMockRecorder) DecryptionKeyIDs() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DecryptionKeyIDs", reflect.TypeOf((*MockEncryptionAlg)(nil).DecryptionKeyIDs))
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DecryptionKeyIDs", reflect.TypeOf((*MockEncryptionAlgorithm)(nil).DecryptionKeyIDs))
}
// Encrypt mocks base method
func (m *MockEncryptionAlg) Encrypt(value []byte) ([]byte, error) {
func (m *MockEncryptionAlgorithm) Encrypt(value []byte) ([]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Encrypt", value)
ret0, _ := ret[0].([]byte)
@ -121,13 +121,13 @@ func (m *MockEncryptionAlg) Encrypt(value []byte) ([]byte, error) {
}
// Encrypt indicates an expected call of Encrypt
func (mr *MockEncryptionAlgMockRecorder) Encrypt(value interface{}) *gomock.Call {
func (mr *MockEncryptionAlgorithmMockRecorder) Encrypt(value interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Encrypt", reflect.TypeOf((*MockEncryptionAlg)(nil).Encrypt), value)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Encrypt", reflect.TypeOf((*MockEncryptionAlgorithm)(nil).Encrypt), value)
}
// Decrypt mocks base method
func (m *MockEncryptionAlg) Decrypt(hashed []byte, keyID string) ([]byte, error) {
func (m *MockEncryptionAlgorithm) Decrypt(hashed []byte, keyID string) ([]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Decrypt", hashed, keyID)
ret0, _ := ret[0].([]byte)
@ -136,13 +136,13 @@ func (m *MockEncryptionAlg) Decrypt(hashed []byte, keyID string) ([]byte, error)
}
// Decrypt indicates an expected call of Decrypt
func (mr *MockEncryptionAlgMockRecorder) Decrypt(hashed, keyID interface{}) *gomock.Call {
func (mr *MockEncryptionAlgorithmMockRecorder) Decrypt(hashed, keyID interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Decrypt", reflect.TypeOf((*MockEncryptionAlg)(nil).Decrypt), hashed, keyID)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Decrypt", reflect.TypeOf((*MockEncryptionAlgorithm)(nil).Decrypt), hashed, keyID)
}
// DecryptString mocks base method
func (m *MockEncryptionAlg) DecryptString(hashed []byte, keyID string) (string, error) {
func (m *MockEncryptionAlgorithm) DecryptString(hashed []byte, keyID string) (string, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "DecryptString", hashed, keyID)
ret0, _ := ret[0].(string)
@ -151,36 +151,36 @@ func (m *MockEncryptionAlg) DecryptString(hashed []byte, keyID string) (string,
}
// DecryptString indicates an expected call of DecryptString
func (mr *MockEncryptionAlgMockRecorder) DecryptString(hashed, keyID interface{}) *gomock.Call {
func (mr *MockEncryptionAlgorithmMockRecorder) DecryptString(hashed, keyID interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DecryptString", reflect.TypeOf((*MockEncryptionAlg)(nil).DecryptString), hashed, keyID)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DecryptString", reflect.TypeOf((*MockEncryptionAlgorithm)(nil).DecryptString), hashed, keyID)
}
// MockHashAlg is a mock of HashAlg interface
type MockHashAlg struct {
// MockHashAlgorithm is a mock of HashAlgorithm interface
type MockHashAlgorithm struct {
ctrl *gomock.Controller
recorder *MockHashAlgMockRecorder
recorder *MockHashAlgorithmMockRecorder
}
// MockHashAlgMockRecorder is the mock recorder for MockHashAlg
type MockHashAlgMockRecorder struct {
mock *MockHashAlg
// MockHashAlgorithmMockRecorder is the mock recorder for MockHashAlgorithm
type MockHashAlgorithmMockRecorder struct {
mock *MockHashAlgorithm
}
// NewMockHashAlg creates a new mock instance
func NewMockHashAlg(ctrl *gomock.Controller) *MockHashAlg {
mock := &MockHashAlg{ctrl: ctrl}
mock.recorder = &MockHashAlgMockRecorder{mock}
// NewMockHashAlgorithm creates a new mock instance
func NewMockHashAlgorithm(ctrl *gomock.Controller) *MockHashAlgorithm {
mock := &MockHashAlgorithm{ctrl: ctrl}
mock.recorder = &MockHashAlgorithmMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockHashAlg) EXPECT() *MockHashAlgMockRecorder {
func (m *MockHashAlgorithm) EXPECT() *MockHashAlgorithmMockRecorder {
return m.recorder
}
// Algorithm mocks base method
func (m *MockHashAlg) Algorithm() string {
func (m *MockHashAlgorithm) Algorithm() string {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Algorithm")
ret0, _ := ret[0].(string)
@ -188,13 +188,13 @@ func (m *MockHashAlg) Algorithm() string {
}
// Algorithm indicates an expected call of Algorithm
func (mr *MockHashAlgMockRecorder) Algorithm() *gomock.Call {
func (mr *MockHashAlgorithmMockRecorder) Algorithm() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Algorithm", reflect.TypeOf((*MockHashAlg)(nil).Algorithm))
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Algorithm", reflect.TypeOf((*MockHashAlgorithm)(nil).Algorithm))
}
// Hash mocks base method
func (m *MockHashAlg) Hash(value []byte) ([]byte, error) {
func (m *MockHashAlgorithm) Hash(value []byte) ([]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Hash", value)
ret0, _ := ret[0].([]byte)
@ -203,13 +203,13 @@ func (m *MockHashAlg) Hash(value []byte) ([]byte, error) {
}
// Hash indicates an expected call of Hash
func (mr *MockHashAlgMockRecorder) Hash(value interface{}) *gomock.Call {
func (mr *MockHashAlgorithmMockRecorder) Hash(value interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Hash", reflect.TypeOf((*MockHashAlg)(nil).Hash), value)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Hash", reflect.TypeOf((*MockHashAlgorithm)(nil).Hash), value)
}
// CompareHash mocks base method
func (m *MockHashAlg) CompareHash(hashed, comparer []byte) error {
func (m *MockHashAlgorithm) CompareHash(hashed, comparer []byte) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "CompareHash", hashed, comparer)
ret0, _ := ret[0].(error)
@ -217,7 +217,7 @@ func (m *MockHashAlg) CompareHash(hashed, comparer []byte) error {
}
// CompareHash indicates an expected call of CompareHash
func (mr *MockHashAlgMockRecorder) CompareHash(hashed, comparer interface{}) *gomock.Call {
func (mr *MockHashAlgorithmMockRecorder) CompareHash(hashed, comparer interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CompareHash", reflect.TypeOf((*MockHashAlg)(nil).CompareHash), hashed, comparer)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CompareHash", reflect.TypeOf((*MockHashAlgorithm)(nil).CompareHash), hashed, comparer)
}

View File

@ -104,7 +104,7 @@ func TestCrypt(t *testing.T) {
func TestEncrypt(t *testing.T) {
type args struct {
value []byte
c EncryptionAlg
c EncryptionAlgorithm
}
tests := []struct {
name string
@ -136,7 +136,7 @@ func TestEncrypt(t *testing.T) {
func TestDecrypt(t *testing.T) {
type args struct {
value *CryptoValue
c EncryptionAlg
c EncryptionAlgorithm
}
tests := []struct {
name string
@ -174,7 +174,7 @@ func TestDecrypt(t *testing.T) {
func TestDecryptString(t *testing.T) {
type args struct {
value *CryptoValue
c EncryptionAlg
c EncryptionAlgorithm
}
tests := []struct {
name string
@ -212,7 +212,7 @@ func TestDecryptString(t *testing.T) {
func TestHash(t *testing.T) {
type args struct {
value []byte
c HashAlg
c HashAlgorithm
}
tests := []struct {
name string
@ -245,7 +245,7 @@ func TestCompareHash(t *testing.T) {
type args struct {
value *CryptoValue
comparer []byte
c HashAlg
c HashAlgorithm
}
tests := []struct {
name string