diff --git a/attestation/didmailto/did.go b/attestation/didmailto/did.go index 41453e5..7c92302 100644 --- a/attestation/didmailto/did.go +++ b/attestation/didmailto/did.go @@ -14,7 +14,12 @@ const InvalidMailtoDIDErrorName = "InvalidMailtoDID" var ErrInvalidMailtoDID = errors.New(InvalidMailtoDIDErrorName, "invalid mailto DID") -// New creates a new mailto DID from an RFC 5322 formatted email address. +// New creates a new mailto DID from an RFC 5322 formatted email address: +// user@example.com is did:mailto:example.com:user. +// +// A DID identifier holds only letters, digits, ".", "-", "_" and +// percent-escapes, so every other character of the address is percent-encoded: +// user+tag@example.com is did:mailto:example.com:user%2Btag. func New(email string) (did.DID, error) { a, err := mail.ParseAddress(email) if err != nil { @@ -27,7 +32,23 @@ func New(email string) (did.DID, error) { return did.DID{}, fmt.Errorf("malformed email address: %s", email) } local, domain = a.Address[:at], a.Address[at+1:] - return did.New(Method, fmt.Sprintf("%s:%s", url.QueryEscape(domain), url.QueryEscape(local))), nil + return did.Parse(fmt.Sprintf("did:%s:%s:%s", Method, escape(domain), escape(local))) +} + +// escape percent-encodes every character of s that a DID identifier cannot +// carry unescaped. +// https://www.w3.org/TR/did-core/#did-syntax +func escape(s string) string { + var b strings.Builder + for i := 0; i < len(s); i++ { + c := s[i] + if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' || c == '.' || c == '-' || c == '_' { + b.WriteByte(c) + } else { + fmt.Fprintf(&b, "%%%02X", c) + } + } + return b.String() } // Email extracts the email address from the DID. @@ -36,6 +57,9 @@ func Email(d did.DID) (string, error) { return "", ErrInvalidMailtoDID } parts := strings.Split(d.String(), ":") + if len(parts) != 4 { + return "", ErrInvalidMailtoDID + } emailLocal, err := url.QueryUnescape(parts[3]) if err != nil { return "", ErrInvalidMailtoDID diff --git a/attestation/didmailto/did_test.go b/attestation/didmailto/did_test.go index 1c77ca5..fc57e34 100644 --- a/attestation/didmailto/did_test.go +++ b/attestation/didmailto/did_test.go @@ -20,6 +20,26 @@ func TestNew(t *testing.T) { require.Equal(t, "did:mailto:mail.example.com:user", d.String()) }) + t.Run("characters a DID cannot carry are percent-encoded", func(t *testing.T) { + cases := map[string]string{ + "user+tag@example.com": "did:mailto:example.com:user%2Btag", + "user~name@example.com": "did:mailto:example.com:user%7Ename", + "o'brien@example.com": "did:mailto:example.com:o%27brien", + "a!b#c$d@example.com": "did:mailto:example.com:a%21b%23c%24d", + "user*name@example.com": "did:mailto:example.com:user%2Aname", + "email@[123.123.123.123]": "did:mailto:%5B123.123.123.123%5D:email", + } + for email, want := range cases { + d, err := New(email) + require.NoError(t, err, email) + require.Equal(t, want, d.String()) + + got, err := Email(d) + require.NoError(t, err, email) + require.Equal(t, email, got) + } + }) + t.Run("missing @ returns error", func(t *testing.T) { _, err := New("notanemail") require.Error(t, err) @@ -60,6 +80,15 @@ func TestEmail(t *testing.T) { require.ErrorIs(t, err, ErrInvalidMailtoDID) }) + t.Run("a mailto DID without a domain and a local part returns error", func(t *testing.T) { + for _, str := range []string{"did:mailto:example.com", "did:mailto:example.com:user:extra"} { + d, err := did.Parse(str) + require.NoError(t, err) + _, err = Email(d) + require.ErrorIs(t, err, ErrInvalidMailtoDID, str) + } + }) + t.Run("round-trips unusual emails", func(t *testing.T) { emails := []string{ // https://gist.github.com/cjaoude/fd9910626629b53c4d25#file-gistfile1-txt-L5 @@ -80,6 +109,9 @@ func TestEmail(t *testing.T) { "email@example.museum", "email@example.co.jp", "firstname-lastname@example.com", + "first~last@example.com", + "first'last@example.com", + "first!#$%&*/=?^`{|}last@example.com", // https://gist.github.com/cjaoude/fd9910626629b53c4d25#file-gistfile1-txt-L24 // TODO: none of these parse with net/mail // "much.”more\\ unusual”@example.com",