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

Encode accented letters as well #40

Merged
merged 3 commits into from
Dec 18, 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
17 changes: 11 additions & 6 deletions did_x509/did_x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
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 @@ -21,13 +23,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"},
reinkrul marked this conversation as resolved.
Show resolved Hide resolved
{"💩", "%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
Loading