-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
237 additions
and
0 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,65 @@ | ||
/* | ||
* Copyright (C) 2023 Nuts community | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package credential | ||
|
||
import ( | ||
"errors" | ||
"github.com/nuts-foundation/go-did/did" | ||
"github.com/nuts-foundation/go-did/vc" | ||
) | ||
|
||
// ResolveSubjectDID resolves the subject DID from the given credentials. | ||
// It returns an error if: | ||
// - the credentials do not have the same subject DID. | ||
// - the credentials do not have a subject DID. | ||
func ResolveSubjectDID(credentials ...vc.VerifiableCredential) (*did.DID, error) { | ||
var subjectID did.DID | ||
for _, credential := range credentials { | ||
sid, err := credential.SubjectDID() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !subjectID.Empty() && !subjectID.Equals(*sid) { | ||
return nil, errors.New("not all VCs have the same credentialSubject.id") | ||
} | ||
subjectID = *sid | ||
} | ||
return &subjectID, nil | ||
} | ||
|
||
// VerifyPresenterIsHolder checks if the holder of the VP is the same as the subject of the VCs being presented. | ||
// It returns an error when: | ||
// - the VP does not have a holder. | ||
// - the VP holder is not the same as the subject of the VCs. | ||
// If the check succeeds, it returns nil. | ||
func VerifyPresenterIsHolder(vp vc.VerifiablePresentation) error { | ||
// Check VP signer == VC subject (presenter is holder of VCs) | ||
if vp.Holder == nil { | ||
// Is this even possible? | ||
return errors.New("no holder") | ||
} | ||
credentialSubjectID, err := ResolveSubjectDID(vp.VerifiableCredential...) | ||
if err != nil { | ||
return err | ||
} | ||
if *vp.Holder != credentialSubjectID.URI() { | ||
return errors.New("not all VC credentialSubject.id match VP holder") | ||
} | ||
return nil | ||
} |
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,117 @@ | ||
/* | ||
* Copyright (C) 2023 Nuts community | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package credential | ||
|
||
import ( | ||
ssi "github.com/nuts-foundation/go-did" | ||
"github.com/nuts-foundation/go-did/did" | ||
"github.com/nuts-foundation/go-did/vc" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestResolveSubjectDID(t *testing.T) { | ||
did1 := did.MustParseDID("did:test:123") | ||
did2 := did.MustParseDID("did:test:456") | ||
credential1 := vc.VerifiableCredential{ | ||
CredentialSubject: []interface{}{map[string]interface{}{"id": did1}}, | ||
} | ||
credential2 := vc.VerifiableCredential{ | ||
CredentialSubject: []interface{}{map[string]interface{}{"id": did1}}, | ||
} | ||
credential3 := vc.VerifiableCredential{ | ||
CredentialSubject: []interface{}{map[string]interface{}{"id": did2}}, | ||
} | ||
t.Run("all the same", func(t *testing.T) { | ||
actual, err := ResolveSubjectDID(credential1, credential2) | ||
assert.NoError(t, err) | ||
assert.Equal(t, did1, *actual) | ||
}) | ||
t.Run("differ", func(t *testing.T) { | ||
actual, err := ResolveSubjectDID(credential1, credential3) | ||
assert.EqualError(t, err, "not all VCs have the same credentialSubject.id") | ||
assert.Nil(t, actual) | ||
}) | ||
t.Run("no ID", func(t *testing.T) { | ||
actual, err := ResolveSubjectDID(vc.VerifiableCredential{CredentialSubject: []interface{}{map[string]interface{}{}}}) | ||
assert.EqualError(t, err, "unable to get subject DID from VC: credential subjects have no ID") | ||
assert.Nil(t, actual) | ||
}) | ||
t.Run("no credentialSubject", func(t *testing.T) { | ||
actual, err := ResolveSubjectDID(vc.VerifiableCredential{}) | ||
assert.EqualError(t, err, "unable to get subject DID from VC: there must be at least 1 credentialSubject") | ||
assert.Nil(t, actual) | ||
}) | ||
|
||
} | ||
|
||
func TestVerifyPresenterIsHolder(t *testing.T) { | ||
holder, _ := ssi.ParseURI("did:test:123") | ||
t.Run("ok", func(t *testing.T) { | ||
vp := vc.VerifiablePresentation{ | ||
Holder: holder, | ||
VerifiableCredential: []vc.VerifiableCredential{ | ||
{ | ||
CredentialSubject: []interface{}{map[string]interface{}{"id": holder}}, | ||
}, | ||
}, | ||
} | ||
err := VerifyPresenterIsHolder(vp) | ||
assert.NoError(t, err) | ||
}) | ||
t.Run("no holder", func(t *testing.T) { | ||
vp := vc.VerifiablePresentation{} | ||
err := VerifyPresenterIsHolder(vp) | ||
assert.EqualError(t, err, "no holder") | ||
}) | ||
t.Run("no VC subject", func(t *testing.T) { | ||
vp := vc.VerifiablePresentation{ | ||
Holder: holder, | ||
VerifiableCredential: []vc.VerifiableCredential{ | ||
{}, | ||
}, | ||
} | ||
err := VerifyPresenterIsHolder(vp) | ||
assert.EqualError(t, err, "unable to get subject DID from VC: there must be at least 1 credentialSubject") | ||
}) | ||
t.Run("no VC subject ID", func(t *testing.T) { | ||
vp := vc.VerifiablePresentation{ | ||
Holder: holder, | ||
VerifiableCredential: []vc.VerifiableCredential{ | ||
{ | ||
CredentialSubject: []interface{}{map[string]interface{}{}}, | ||
}, | ||
}, | ||
} | ||
err := VerifyPresenterIsHolder(vp) | ||
assert.EqualError(t, err, "unable to get subject DID from VC: credential subjects have no ID") | ||
}) | ||
t.Run("holder does not equal VC subject ID", func(t *testing.T) { | ||
vp := vc.VerifiablePresentation{ | ||
Holder: holder, | ||
VerifiableCredential: []vc.VerifiableCredential{ | ||
{ | ||
CredentialSubject: []interface{}{map[string]interface{}{"id": did.MustParseDID("did:test:456")}}, | ||
}, | ||
}, | ||
} | ||
err := VerifyPresenterIsHolder(vp) | ||
assert.EqualError(t, err, "not all VC credentialSubject.id match VP holder") | ||
}) | ||
} |