-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from FIWARE/support-holder-verification
verify the holder if configured
- Loading branch information
Showing
8 changed files
with
204 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package verifier | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/fiware/VCVerifier/logging" | ||
"github.com/trustbloc/vc-go/verifiable" | ||
) | ||
|
||
type HolderValidationService struct{} | ||
|
||
func (hvs *HolderValidationService) ValidateVC(verifiableCredential *verifiable.Credential, validationContext ValidationContext) (result bool, err error) { | ||
logging.Log().Debugf("Validate holder for %s", logging.PrettyPrintObject(verifiableCredential)) | ||
defer func() { | ||
if recErr := recover(); recErr != nil { | ||
logging.Log().Warnf("Was not able to convert context. Err: %v", recErr) | ||
err = ErrorCannotConverContext | ||
} | ||
}() | ||
holderContext := validationContext.(HolderValidationContext) | ||
|
||
path := strings.Split(holderContext.claim, ".") | ||
pathLength := len(path) | ||
|
||
credentialJson := verifiableCredential.ToRawJSON() | ||
currentClaim := credentialJson["credentialSubject"].(map[string]interface{}) | ||
for i, p := range path { | ||
if i == pathLength-1 { | ||
return currentClaim[p].(string) == holderContext.holder, err | ||
} | ||
currentClaim = currentClaim[p].(verifiable.JSONObject) | ||
} | ||
return false, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package verifier | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/fiware/VCVerifier/logging" | ||
"github.com/trustbloc/vc-go/verifiable" | ||
) | ||
|
||
func TestValidateVC(t *testing.T) { | ||
|
||
type test struct { | ||
testName string | ||
credentialToVerifiy verifiable.Credential | ||
validationContext ValidationContext | ||
expectedResult bool | ||
} | ||
tests := []test{ | ||
{testName: "If the holder is correct, the vc should be allowed.", credentialToVerifiy: getCredentialWithHolder("subject", "holder"), validationContext: HolderValidationContext{claim: "subject", holder: "holder"}, expectedResult: true}, | ||
{testName: "If the holder is correct inside the sub element, the vc should be allowed.", credentialToVerifiy: getCredentialWithHolderInSubelement("holder"), validationContext: HolderValidationContext{claim: "sub.holder", holder: "holder"}, expectedResult: true}, | ||
{testName: "If the holder is not correct, the vc should be rejected.", credentialToVerifiy: getCredentialWithHolder("subject", "holder"), validationContext: HolderValidationContext{claim: "subject", holder: "someOneElse"}, expectedResult: false}, | ||
{testName: "If the holder is not correct inside the sub element, the vc should be rejected.", credentialToVerifiy: getCredentialWithHolderInSubelement("holder"), validationContext: HolderValidationContext{claim: "sub.holder", holder: "someOneElse"}, expectedResult: false}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.testName, func(t *testing.T) { | ||
|
||
logging.Log().Info("TestValidateVC +++++++++++++++++ Running test: ", tc.testName) | ||
|
||
holderValidationService := HolderValidationService{} | ||
|
||
result, _ := holderValidationService.ValidateVC(&tc.credentialToVerifiy, tc.validationContext) | ||
if result != tc.expectedResult { | ||
t.Errorf("%s - Expected result %v but was %v.", tc.testName, tc.expectedResult, result) | ||
return | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func getCredentialWithHolder(holderClaim, holder string) verifiable.Credential { | ||
vc, _ := verifiable.CreateCredential(verifiable.CredentialContents{ | ||
Issuer: &verifiable.Issuer{ID: "did:test:issuer"}, | ||
Types: []string{"VerifiableCredential"}, | ||
Subject: []verifiable.Subject{ | ||
{ | ||
CustomFields: map[string]interface{}{holderClaim: holder}, | ||
}, | ||
}}, verifiable.CustomFields{}) | ||
return *vc | ||
} | ||
|
||
func getCredentialWithHolderInSubelement(holder string) verifiable.Credential { | ||
|
||
vc, _ := verifiable.CreateCredential(verifiable.CredentialContents{ | ||
Issuer: &verifiable.Issuer{ID: "did:test:issuer"}, | ||
Types: []string{"VerifiableCredential"}, | ||
Subject: []verifiable.Subject{ | ||
{ | ||
CustomFields: map[string]interface{}{"sub": map[string]interface{}{"holder": holder}}, | ||
}, | ||
}}, verifiable.CustomFields{}) | ||
return *vc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.