Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Add buffered output for JWT printing #15

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading