From 9ad35793c61fea74ec2ee0cc61f70703400722f4 Mon Sep 17 00:00:00 2001 From: Rein Krul Date: Tue, 17 Dec 2024 09:26:20 +0100 Subject: [PATCH 1/3] Encode accented letters as well --- did_x509/did_x509.go | 17 +++++++++++------ did_x509/did_x509_test.go | 11 +++++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/did_x509/did_x509.go b/did_x509/did_x509.go index 9e03065..804975d 100644 --- a/did_x509/did_x509.go +++ b/did_x509/did_x509.go @@ -5,13 +5,11 @@ import ( "encoding/base64" "errors" "fmt" + "github.com/nuts-foundation/go-did/did" + "github.com/nuts-foundation/uzi-did-x509-issuer/x509_cert" "net/url" "regexp" "strings" - "unicode" - - "github.com/nuts-foundation/go-did/did" - "github.com/nuts-foundation/uzi-did-x509-issuer/x509_cert" ) type X509Did struct { @@ -60,8 +58,15 @@ func CreateDid(signingCert, caCert *x509.Certificate, subjectAttributes []x509_c // See https://github.com/golang/go/issues/27559#issuecomment-449652574 func PercentEncode(input string) string { var encoded strings.Builder - for _, r := range input { - if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' || r == '_' || r == '.' { + // Unicode characters might consist of multiple bytes, so first we encode the string using url.PathEscape (which supports multi-byte characters), + // then we encode the characters that weren't encoded by url.PathEscape, but need to be encoded according to the DID specification. + preEscaped := url.PathEscape(input) + encoded.Grow(len(preEscaped)) + for _, r := range preEscaped { + if r == '%' || + (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || + (r >= '0' && r <= '9') || + r == '-' || r == '.' || r == '_' { encoded.WriteRune(r) } else { encoded.WriteString(fmt.Sprintf("%%%02X", r)) diff --git a/did_x509/did_x509_test.go b/did_x509/did_x509_test.go index b190945..abd4387 100644 --- a/did_x509/did_x509_test.go +++ b/did_x509/did_x509_test.go @@ -21,13 +21,16 @@ func TestPercentEncode(t *testing.T) { {"a+b=c", "a%2Bb%3Dc"}, {"~!@#$%^&*()_+", "%7E%21%40%23%24%25%5E%26%2A%28%29_%2B"}, {"FauxCare & Co", "FauxCare%20%26%20Co"}, + {"FåúxCaré & Có", "F%C3%A5%C3%BAxCar%C3%A9%20%26%20C%C3%B3"}, } for _, test := range tests { - result := PercentEncode(test.input) - if result != test.expected { - t.Errorf("PercentEncode(%q) = %q; want %q", test.input, result, test.expected) - } + t.Run(test.input, func(t *testing.T) { + result := PercentEncode(test.input) + if result != test.expected { + t.Errorf("PercentEncode(%q) = %q; want %q", test.input, result, test.expected) + } + }) } } From 699db1c0d07db0d6722f572bef7a347ceb41dcbe Mon Sep 17 00:00:00 2001 From: Rein Krul Date: Tue, 17 Dec 2024 11:26:41 +0100 Subject: [PATCH 2/3] PR feedback --- did_x509/did_x509_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/did_x509/did_x509_test.go b/did_x509/did_x509_test.go index abd4387..32a90d9 100644 --- a/did_x509/did_x509_test.go +++ b/did_x509/did_x509_test.go @@ -3,6 +3,8 @@ package did_x509 import ( "crypto/x509" "encoding/base64" + "github.com/stretchr/testify/require" + "net/url" "strings" "testing" @@ -22,14 +24,16 @@ func TestPercentEncode(t *testing.T) { {"~!@#$%^&*()_+", "%7E%21%40%23%24%25%5E%26%2A%28%29_%2B"}, {"FauxCare & Co", "FauxCare%20%26%20Co"}, {"FåúxCaré & Có", "F%C3%A5%C3%BAxCar%C3%A9%20%26%20C%C3%B3"}, + {"💩", "%F0%9F%92%A9"}, } for _, test := range tests { t.Run(test.input, func(t *testing.T) { result := PercentEncode(test.input) - if result != test.expected { - t.Errorf("PercentEncode(%q) = %q; want %q", test.input, result, test.expected) - } + require.Equal(t, test.expected, result) + unescaped, err := url.PathUnescape(result) + require.NoError(t, err) + require.Equal(t, test.input, unescaped) }) } } From ff6d1251b8b4eae760cd8136a5376156d37b1dfc Mon Sep 17 00:00:00 2001 From: Rein Krul Date: Wed, 18 Dec 2024 10:28:21 +0100 Subject: [PATCH 3/3] Remove println, breaks automation --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 069cbb4..9e6cf16 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,6 @@ func main() { fmt.Println(err) os.Exit(-1) } - fmt.Println("VC result:") err = printLineAndFlush(jwt) if err != nil { fmt.Println(err)