Skip to content

Commit

Permalink
refactoring (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scratch-net authored Jun 12, 2020
1 parent 4944e5d commit a621ff3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
22 changes: 11 additions & 11 deletions sdk/cardverifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func VirgilCardVerifierDisableVirgilSignature() VirgilCardVerifierOption {
}
}

func VirgilCardVerifierAddWhitelist(wl Whitelist) VirgilCardVerifierOption {
func VirgilCardVerifierAddAllowList(wl *AllowList) VirgilCardVerifierOption {
return func(v *VirgilCardVerifier) {
v.whitelists = append(v.whitelists, wl)
v.allowLists = append(v.allowLists, wl)
}
}

Expand All @@ -86,7 +86,7 @@ type VirgilCardVerifier struct {
crypto *CardCrypto
verifySelfSignature bool
verifyVirgilSignature bool
whitelists []Whitelist
allowLists []*AllowList
virgilPublicKey crypto.PublicKey

// virgilPublicKeySource is used to update Virgil Cards service public key
Expand Down Expand Up @@ -137,15 +137,15 @@ func (v *VirgilCardVerifier) VerifyCard(card *Card) error {
return errors.NewSDKError(err, "action", "VirgilCardVerifier.VerifyCard", "validate", "virgil")
}
}
return v.verifyCardByWhitelist(card)
return v.verifyCardByAllowList(card)
}

func (v *VirgilCardVerifier) verifyCardByWhitelist(card *Card) error {
for _, whitelist := range v.whitelists {
func (v *VirgilCardVerifier) verifyCardByAllowList(card *Card) error {
for _, allowList := range v.allowLists {
signatureVerified := false
var err error
for i := range whitelist.VerifierCredentials {
var cred = whitelist.VerifierCredentials[i]
for i := range allowList.VerifierCredentials {
var cred = allowList.VerifierCredentials[i]
if err = v.ValidateSignerSignature(card, cred.Signer, cred.PublicKey); err != nil {
continue
}
Expand Down Expand Up @@ -181,12 +181,12 @@ func (v *VirgilCardVerifier) ValidateSignerSignature(card *Card, signer string,
return ErrSignerWasNotFound
}

type Whitelist struct {
type AllowList struct {
VerifierCredentials []*VerifierCredentials
}

func NewWhitelist(credentials ...*VerifierCredentials) Whitelist {
return Whitelist{
func NewAllowList(credentials ...*VerifierCredentials) *AllowList {
return &AllowList{
VerifierCredentials: credentials,
}
}
Expand Down
16 changes: 8 additions & 8 deletions sdk/cardverifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type testCredentials struct {
PrivateKey crypto.PrivateKey
}

func TestWhitelist(t *testing.T) {
func TestAllowList(t *testing.T) {
const credsCount = 5
creds := make([]testCredentials, 5)
for i := 0; i < credsCount; i++ {
Expand Down Expand Up @@ -84,8 +84,8 @@ func TestWhitelist(t *testing.T) {
verifier := NewVirgilCardVerifier(
VirgilCardVerifierSetCrypto(cryptoNative),
VirgilCardVerifierDisableVirgilSignature(),
VirgilCardVerifierAddWhitelist(NewWhitelist(creds[0].VerifierCredentials, creds[1].VerifierCredentials)),
VirgilCardVerifierAddWhitelist(NewWhitelist(creds[2].VerifierCredentials)),
VirgilCardVerifierAddAllowList(NewAllowList(creds[0].VerifierCredentials, creds[1].VerifierCredentials)),
VirgilCardVerifierAddAllowList(NewAllowList(creds[2].VerifierCredentials)),
)

card, err := ParseRawCard(cryptoNative, model, false)
Expand All @@ -95,24 +95,24 @@ func TestWhitelist(t *testing.T) {
err = verifier.VerifyCard(card)
require.NoError(t, err)

//check that everything is ok if at least one signature in whitelist is valid
//check that everything is ok if at least one signature in allow list is valid
// creds[4] doesn't exist
verifier = NewVirgilCardVerifier(
VirgilCardVerifierSetCrypto(cryptoNative),
VirgilCardVerifierDisableVirgilSignature(),
VirgilCardVerifierAddWhitelist(NewWhitelist(creds[4].VerifierCredentials, creds[1].VerifierCredentials)),
VirgilCardVerifierAddAllowList(NewAllowList(creds[4].VerifierCredentials, creds[1].VerifierCredentials)),
)

err = verifier.VerifyCard(card)
require.NoError(t, err)

//Check that verification fails if no signature exists for whitelist
//Check that verification fails if no signature exists for allow list
// creds[3] doesn't exist
verifier = NewVirgilCardVerifier(
VirgilCardVerifierSetCrypto(cryptoNative),
VirgilCardVerifierDisableVirgilSignature(),
VirgilCardVerifierAddWhitelist(NewWhitelist(creds[2].VerifierCredentials)),
VirgilCardVerifierAddWhitelist(NewWhitelist(creds[3].VerifierCredentials)),
VirgilCardVerifierAddAllowList(NewAllowList(creds[2].VerifierCredentials)),
VirgilCardVerifierAddAllowList(NewAllowList(creds[3].VerifierCredentials)),
)

err = verifier.VerifyCard(card)
Expand Down

0 comments on commit a621ff3

Please sign in to comment.