From 9a22269f21336e3eaddd562d7a513590d15abc6b Mon Sep 17 00:00:00 2001 From: Roland Groen Date: Tue, 12 Nov 2024 16:52:23 +0100 Subject: [PATCH] BUGFIX: Add buffered output for JWT printing (#15) * Add buffered output for JWT printing Replaced println with a buffered writer when outputting the JWT to ensure the output is properly flushed. Introduced a new helper function `printLineAndFlush` to handle the buffered writing and flushing process. * Handle errors from printLineAndFlush function Updated the printLineAndFlush function to return an error instead of terminating the program. Modified the main function to handle the error appropriately by logging the error and exiting when printLineAndFlush fails. * Simplified parsing --------- Co-authored-by: Rein Krul --- main.go | 25 +++++++++++++++++++++++-- uzi_vc_issuer/ura_issuer.go | 9 +-------- uzi_vc_validator/ura_validator.go | 5 +---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 348bdc0..88aa984 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "fmt" "github.com/alecthomas/kong" "github.com/nuts-foundation/uzi-did-x509-issuer/uzi_vc_issuer" @@ -48,7 +49,11 @@ func main() { fmt.Println(err) os.Exit(-1) } - println(jwt) + err = printLineAndFlush(jwt) + if err != nil { + fmt.Println(err) + os.Exit(-1) + } case "test-cert ", "test-cert ": // Format is 2.16.528.1.1007.99.2110-1-900030787-S-90000380-00.000-11223344 // ------ @@ -93,13 +98,29 @@ func main() { fmt.Println(err) os.Exit(-1) } - println(jwt) + err = printLineAndFlush(jwt) + if err != nil { + fmt.Println(err) + os.Exit(-1) + } default: fmt.Println("Unknown command") os.Exit(-1) } } +// printLineAndFlush writes a JWT (JSON Web Token) to the standard output and flushes the buffered writer. +func printLineAndFlush(jwt string) error { + f := bufio.NewWriter(os.Stdout) + // Make sure to flush + defer func(f *bufio.Writer) { + _ = f.Flush() + }(f) + // Write the JWT + _, err := f.WriteString(jwt + "\n") + return err +} + func issueVc(vc VC) (string, error) { return uzi_vc_issuer.Issue(vc.CertificateFile, vc.SigningKey, vc.SubjectDID, vc.Test, vc.IncludePermanent) } diff --git a/uzi_vc_issuer/ura_issuer.go b/uzi_vc_issuer/ura_issuer.go index e051d8b..feee2b2 100644 --- a/uzi_vc_issuer/ura_issuer.go +++ b/uzi_vc_issuer/ura_issuer.go @@ -6,7 +6,6 @@ import ( "crypto/sha1" "crypto/x509" "encoding/base64" - "encoding/json" "encoding/pem" "errors" "fmt" @@ -77,14 +76,8 @@ func Issue(certificateFile string, signingKeyFile string, subjectDID string, all if err != nil { return "", err } - credentialJSON, err := json.Marshal(credential) - if err != nil { - return "", err - } + jwtString := credential.Raw() validator := uzi_vc_validator.NewUraValidator(allowTestUraCa, allowSelfSignedCa) - jwtString := string(credentialJSON) - jwtString = jwtString[1:] // Chop start - jwtString = jwtString[:len(jwtString)-1] // Chop end err = validator.Validate(jwtString) if err != nil { return "", err diff --git a/uzi_vc_validator/ura_validator.go b/uzi_vc_validator/ura_validator.go index dc51a05..abc547b 100644 --- a/uzi_vc_validator/ura_validator.go +++ b/uzi_vc_validator/ura_validator.go @@ -4,7 +4,6 @@ import ( "crypto/sha1" "crypto/x509" "encoding/base64" - "encoding/json" "fmt" "github.com/lestrrat-go/jwx/v2/cert" "github.com/lestrrat-go/jwx/v2/jwa" @@ -38,9 +37,7 @@ type JwtHeaderValues struct { } func (u UraValidatorImpl) Validate(jwtString string) error { - credential := &vc.VerifiableCredential{} - marshal, _ := json.Marshal(jwtString) - err := json.Unmarshal(marshal, credential) + credential, err := vc.ParseVerifiableCredential(jwtString) if err != nil { return err }