Skip to content

Commit

Permalink
BUGFIX: Add buffered output for JWT printing (#15)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
rolandgroen and reinkrul authored Nov 12, 2024
1 parent 9e55734 commit 9a22269
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
25 changes: 23 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"fmt"
"github.com/alecthomas/kong"
"github.com/nuts-foundation/uzi-did-x509-issuer/uzi_vc_issuer"
Expand Down Expand Up @@ -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 <uzi> <ura> <agb>", "test-cert <uzi> <ura> <agb> <subject_did>":
// Format is 2.16.528.1.1007.99.2110-1-900030787-S-90000380-00.000-11223344
// <OID CA>-<versie-nr>-<UZI-nr>-<pastype>-<Abonnee-nr>-<rol>-<AGB-code>
Expand Down Expand Up @@ -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)
}
9 changes: 1 addition & 8 deletions uzi_vc_issuer/ura_issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions uzi_vc_validator/ura_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 9a22269

Please sign in to comment.