-
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 #45 from FIWARE/did-elsi
Did elsi
- Loading branch information
Showing
18 changed files
with
522 additions
and
348 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/fiware/VCVerifier | ||
|
||
go 1.21 | ||
go 1.23 | ||
|
||
require ( | ||
github.com/deepmap/oapi-codegen v1.12.3 | ||
|
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,125 @@ | ||
package jades | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/fiware/VCVerifier/common" | ||
"github.com/fiware/VCVerifier/logging" | ||
) | ||
|
||
const TOKEN_EXTRACTION_STRATEGY = "NONE" | ||
const DOCUMENT_NAME = "RemoteDocument" | ||
|
||
var ErrorBadResponse = errors.New("bad_response_from_validation_endpoint") | ||
var ErrorEmptyBodyResponse = errors.New("empty_body_response_from_validation_endpoint") | ||
var ErrorValidationServiceNotReady = errors.New("validation_service_not_ready") | ||
|
||
// Validator for JAdES(https://www.etsi.org/deliver/etsi_ts/119100_119199/11918201/01.01.01_60/ts_11918201v010101p.pdf) signatures | ||
type JAdESValidator interface { | ||
ValidateSignature(signature string) (bool, error) | ||
} | ||
|
||
// Validator implementation, that uses an external validation service(based on https://github.com/esig/dss) | ||
type ExternalJAdESValidator struct { | ||
HttpClient common.HttpClient | ||
ValidationAddress string | ||
HealthAddress string | ||
} | ||
|
||
// structs to be used with the dss-library | ||
|
||
type SignedDocument struct { | ||
Bytes string `json:"bytes"` | ||
Name string `json:"name"` | ||
} | ||
|
||
type ValidationRequest struct { | ||
SignedDocument SignedDocument `json:"signedDocument"` | ||
TokenExtractionStrategy string `json:"tokenExtractionStrategy"` | ||
} | ||
|
||
type SimpleReport struct { | ||
DocumentName string `json:"documentName"` | ||
// we only need to see that all signatures are valid, then nothing else has to be mapped | ||
ValidSignaturesCount int `json:"validSignaturesCount"` | ||
SignaturesCount int `json:"signaturesCount"` | ||
} | ||
|
||
type ValidationResponse struct { | ||
SimpleReport SimpleReport `json:"simpleReport"` | ||
} | ||
|
||
func (v *ExternalJAdESValidator) ValidateSignature(signature string) (success bool, err error) { | ||
validationRequest := ValidationRequest{ | ||
SignedDocument: SignedDocument{Bytes: signature, Name: DOCUMENT_NAME}, | ||
TokenExtractionStrategy: TOKEN_EXTRACTION_STRATEGY, | ||
} | ||
requestBody, err := json.Marshal(validationRequest) | ||
if err != nil { | ||
logging.Log().Warnf("Was not able to marshal the validation request. Error: %v", err) | ||
return success, err | ||
} | ||
|
||
validationHttpRequest, err := http.NewRequest("POST", v.ValidationAddress, bytes.NewBuffer(requestBody)) | ||
if err != nil { | ||
logging.Log().Warnf("Was not able to create validation request. Err: %v", err) | ||
return success, err | ||
} | ||
validationHttpRequest.Header.Set("Content-Type", "application/json") | ||
validationHttpRequest.Header.Set("Accept", "application/json") | ||
validationHttpResponse, err := v.HttpClient.Do(validationHttpRequest) | ||
if err != nil { | ||
logging.Log().Warnf("Did not receive a valid validation response. Err: %v", err) | ||
return false, err | ||
} | ||
|
||
defer func(Body io.ReadCloser) { | ||
err := Body.Close() | ||
if err != nil { | ||
logging.Log().Warnf("Was not able to close the response body. Err: %v", err) | ||
} | ||
}(validationHttpResponse.Body) | ||
|
||
if validationHttpResponse.StatusCode != 200 { | ||
logging.Log().Warnf("Did not receive an OK from the validation endpoint. Was: %s", logging.PrettyPrintObject(validationHttpResponse)) | ||
return false, ErrorBadResponse | ||
} | ||
|
||
if validationHttpResponse.Body == nil { | ||
logging.Log().Warnf("Received an empty body from the validation endpoint.") | ||
return false, ErrorEmptyBodyResponse | ||
} | ||
validationResponse := &ValidationResponse{} | ||
err = json.NewDecoder(validationHttpResponse.Body).Decode(validationResponse) | ||
if err != nil { | ||
logging.Log().Warnf("Was not able to decode the validation response. Error: %v", err) | ||
return false, err | ||
} | ||
// if all signatures in the report are valid, the the validation was successful | ||
if validationResponse.SimpleReport.SignaturesCount == 0 || | ||
(validationResponse.SimpleReport.SignaturesCount != validationResponse.SimpleReport.ValidSignaturesCount) { | ||
logging.Log().Infof("Signature was invalid.") | ||
return false, err | ||
} | ||
return true, err | ||
} | ||
|
||
// health check function, to signal the external service beeing ready | ||
func (v *ExternalJAdESValidator) IsReady() error { | ||
healthRequest, err := http.NewRequest("GET", v.HealthAddress, nil) | ||
if err != nil { | ||
return err | ||
} | ||
response, err := v.HttpClient.Do(healthRequest) | ||
if err != nil { | ||
return err | ||
} | ||
if response.StatusCode != 200 { | ||
return ErrorValidationServiceNotReady | ||
} | ||
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
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
Oops, something went wrong.