-
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.
support verification with gaia-x registry
- Loading branch information
Showing
18 changed files
with
269 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Check trust anchor | ||
|
||
allow to configure for a credential the registry to be used | ||
|
||
-> check linked certificate chain at <registry>/api/trustAnchor/chain/file | ||
|
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
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,107 @@ | ||
package gaiax | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/fiware/VCVerifier/common" | ||
"github.com/fiware/VCVerifier/logging" | ||
"github.com/trustbloc/did-go/doc/did" | ||
"github.com/trustbloc/did-go/vdr" | ||
) | ||
|
||
const GAIAX_REGISTRY_TRUSTANCHOR_FILE = "/v2/api/trustAnchor/chain/file" | ||
|
||
var ErrorUnresolvableDid = errors.New("unresolvable_did") | ||
|
||
/** | ||
* A client to retrieve infromation from EBSI-compatible TrustedIssuerRegistry APIs. | ||
*/ | ||
type GaiaXHttpClient struct { | ||
client common.HttpClient | ||
// TODO: check if needed | ||
didCache common.Cache | ||
didRegistry *vdr.Registry | ||
} | ||
|
||
func (ghc GaiaXHttpClient) IsTrustedParticipant(registryEndpoint string, did string) (trusted bool) { | ||
|
||
// 1. get jwk from did | ||
didDocument, err := ghc.resolveIssuer(did) | ||
|
||
if err != nil { | ||
return false | ||
} | ||
|
||
// 2. verify at the registry | ||
for _, verficationMethod := range didDocument.DIDDocument.VerificationMethod { | ||
if verficationMethod.ID == did { | ||
return ghc.verifiyIssuer(registryEndpoint, verficationMethod) | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (ghc GaiaXHttpClient) verifiyIssuer(registryEndpoint string, verificationMethod did.VerificationMethod) (trusted bool) { | ||
jwk := verificationMethod.JSONWebKey() | ||
|
||
if jwk.CertificatesURL != nil { | ||
return ghc.verifyFileChain(registryEndpoint, jwk.CertificatesURL.String()) | ||
} | ||
// gaia-x did-json need to provide an x5u, thus x5c checks should not be required. | ||
return false | ||
} | ||
|
||
func (ghc GaiaXHttpClient) verifyFileChain(registryEndpoint string, x5u string) (trusted bool) { | ||
requestBody := FileChainRequest{Uri: x5u} | ||
|
||
encodedRequest, err := json.Marshal(requestBody) | ||
if err != nil { | ||
logging.Log().Warnf("Was not able to build a valid certificate check bode. E: %v", err) | ||
return false | ||
} | ||
|
||
request, err := http.NewRequest("POST", buildURL(registryEndpoint, GAIAX_REGISTRY_TRUSTANCHOR_FILE), bytes.NewBuffer(encodedRequest)) | ||
|
||
response, err := ghc.client.Do(request) | ||
if err != nil { | ||
logging.Log().Infof("Was not able to check cert chain %s at %s. E: %v", x5u, registryEndpoint, err) | ||
return false | ||
} | ||
defer response.Body.Close() | ||
if response.StatusCode != 200 { | ||
logging.Log().Infof("x5u %s was not verified to be a trust anchor at %s.", x5u, registryEndpoint) | ||
return false | ||
} | ||
// according to the doc, all 200s are valid chains, thus no need to parse the body | ||
return true | ||
} | ||
|
||
func (ghc GaiaXHttpClient) resolveIssuer(did string) (didDocument *did.DocResolution, err error) { | ||
didDocument, err = ghc.didRegistry.Resolve(did) | ||
if err != nil { | ||
logging.Log().Warnf("Was not able to resolve the issuer %s.", did) | ||
return nil, ErrorUnresolvableDid | ||
} | ||
return didDocument, err | ||
} | ||
|
||
func buildURL(host, path string) string { | ||
return strings.TrimSuffix(host, "/") + "/" + strings.TrimPrefix(path, "/") | ||
} | ||
|
||
type FileChainRequest struct { | ||
Uri string `json:"uri"` | ||
} | ||
|
||
type CertChainRequest struct { | ||
Certs string `json:"certs"` | ||
} | ||
|
||
type VerificationResult struct { | ||
Result bool `json:"result"` | ||
} |
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
Oops, something went wrong.