Skip to content

Commit

Permalink
Merge pull request #40 from nuts-foundation/escape-accented-letters
Browse files Browse the repository at this point in the history
Encode accented letters as well
  • Loading branch information
reinkrul authored Dec 18, 2024
2 parents 637ccd7 + ff6d125 commit 8982367
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
17 changes: 11 additions & 6 deletions did_x509/did_x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,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"
)

// hashAlg is the default hash algorithm used for hashing issuerCertificate
Expand Down Expand Up @@ -67,8 +65,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))
Expand Down
15 changes: 11 additions & 4 deletions did_x509/did_x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package did_x509
import (
"crypto/x509"
"encoding/base64"
"github.com/stretchr/testify/require"
"net/url"
"strings"
"testing"

Expand All @@ -22,13 +24,18 @@ 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"},
{"💩", "%F0%9F%92%A9"},
}

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)
require.Equal(t, test.expected, result)
unescaped, err := url.PathUnescape(result)
require.NoError(t, err)
require.Equal(t, test.input, unescaped)
})
}
}

Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8982367

Please sign in to comment.