2021-12-03 01:58:37 +00:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-12-25 18:53:49 +00:00
|
|
|
"net/url"
|
2021-12-29 20:08:30 +00:00
|
|
|
"strconv"
|
2021-12-03 01:58:37 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-12-25 18:53:49 +00:00
|
|
|
// Me is a URL user identifier.
|
2021-12-03 01:58:37 +00:00
|
|
|
type Me struct {
|
2023-03-16 15:30:00 +00:00
|
|
|
me *url.URL
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:50:45 +00:00
|
|
|
// ParseMe parse string as me URL identifier.
|
2022-12-26 14:09:34 +00:00
|
|
|
//
|
|
|
|
//nolint:funlen,cyclop
|
2022-01-29 14:54:37 +00:00
|
|
|
func ParseMe(raw string) (*Me, error) {
|
2023-03-16 15:30:00 +00:00
|
|
|
me, err := url.Parse(raw)
|
2023-01-02 01:15:11 +00:00
|
|
|
if err != nil {
|
2022-02-01 18:38:08 +00:00
|
|
|
return nil, NewError(
|
|
|
|
ErrorCodeInvalidRequest,
|
|
|
|
err.Error(),
|
|
|
|
"https://indieauth.net/source/#user-profile-url",
|
|
|
|
"",
|
|
|
|
)
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
if me.Scheme != "http" && me.Scheme != "https" {
|
2022-02-01 18:38:08 +00:00
|
|
|
return nil, NewError(
|
|
|
|
ErrorCodeInvalidRequest,
|
2023-03-16 15:30:00 +00:00
|
|
|
"profile URL MUST have either an https or http scheme, got '"+me.Scheme+"'",
|
2022-02-01 18:38:08 +00:00
|
|
|
"https://indieauth.net/source/#user-profile-url",
|
|
|
|
"",
|
|
|
|
)
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
if me.Path == "" {
|
|
|
|
me.Path = "/"
|
2023-01-02 01:15:11 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
if strings.Contains(me.Path, "/.") || strings.Contains(me.Path, "/..") {
|
2022-02-01 18:38:08 +00:00
|
|
|
return nil, NewError(
|
|
|
|
ErrorCodeInvalidRequest,
|
|
|
|
"profile URL MUST contain a path component (/ is a valid path), MUST NOT contain single-dot "+
|
2023-03-16 15:30:00 +00:00
|
|
|
"or double-dot path segments, got '"+me.Path+"'",
|
2022-02-01 18:38:08 +00:00
|
|
|
"https://indieauth.net/source/#user-profile-url",
|
|
|
|
"",
|
|
|
|
)
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
if me.Fragment != "" {
|
2022-02-01 18:38:08 +00:00
|
|
|
return nil, NewError(
|
|
|
|
ErrorCodeInvalidRequest,
|
2023-03-16 15:30:00 +00:00
|
|
|
"profile URL MUST NOT contain a fragment component, got '"+me.Fragment+"'",
|
2022-02-01 18:38:08 +00:00
|
|
|
"https://indieauth.net/source/#user-profile-url",
|
|
|
|
"",
|
|
|
|
)
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
if me.User != nil {
|
2022-02-01 18:38:08 +00:00
|
|
|
return nil, NewError(
|
|
|
|
ErrorCodeInvalidRequest,
|
2023-03-16 15:30:00 +00:00
|
|
|
"profile URL MUST NOT contain a username or password component, got '"+me.User.String()+"'",
|
2022-02-01 18:38:08 +00:00
|
|
|
"https://indieauth.net/source/#user-profile-url",
|
|
|
|
"",
|
|
|
|
)
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
if me.Host == "" {
|
2022-02-01 18:38:08 +00:00
|
|
|
return nil, NewError(
|
|
|
|
ErrorCodeInvalidRequest,
|
2023-03-16 15:30:00 +00:00
|
|
|
"profile host name MUST be a domain name, got '"+me.Host+"'",
|
2022-02-01 18:38:08 +00:00
|
|
|
"https://indieauth.net/source/#user-profile-url",
|
|
|
|
"",
|
|
|
|
)
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
if _, port, _ := net.SplitHostPort(me.Host); port != "" {
|
2022-02-01 18:38:08 +00:00
|
|
|
return nil, NewError(
|
|
|
|
ErrorCodeInvalidRequest,
|
2023-01-14 21:27:37 +00:00
|
|
|
"profile MUST NOT contain a port, got '"+port+"'",
|
2022-02-01 18:38:08 +00:00
|
|
|
"https://indieauth.net/source/#user-profile-url",
|
|
|
|
"",
|
|
|
|
)
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
if out := net.ParseIP(me.Host); out != nil {
|
2022-02-01 18:38:08 +00:00
|
|
|
return nil, NewError(
|
|
|
|
ErrorCodeInvalidRequest,
|
2023-01-14 21:27:37 +00:00
|
|
|
"profile MUST NOT be ipv4 or ipv6 addresses, got '"+out.String()+"'",
|
2022-02-01 18:38:08 +00:00
|
|
|
"https://indieauth.net/source/#user-profile-url",
|
|
|
|
"",
|
|
|
|
)
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
return &Me{me: me}, nil
|
2021-12-25 18:53:49 +00:00
|
|
|
}
|
2021-12-03 01:58:37 +00:00
|
|
|
|
2022-01-29 17:50:45 +00:00
|
|
|
// TestMe returns valid random generated me for tests.
|
2022-01-29 13:49:27 +00:00
|
|
|
func TestMe(tb testing.TB, src string) *Me {
|
2021-12-25 18:53:49 +00:00
|
|
|
tb.Helper()
|
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
u, err := url.Parse(src)
|
2022-01-30 17:49:25 +00:00
|
|
|
if err != nil {
|
2022-02-01 17:27:48 +00:00
|
|
|
tb.Fatal(err)
|
2022-01-30 17:49:25 +00:00
|
|
|
}
|
2021-12-25 18:53:49 +00:00
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
return &Me{me: u}
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:50:45 +00:00
|
|
|
// UnmarshalForm implements custom unmarshler for form values.
|
2021-12-29 20:08:30 +00:00
|
|
|
func (m *Me) UnmarshalForm(v []byte) error {
|
2022-01-29 14:54:37 +00:00
|
|
|
me, err := ParseMe(string(v))
|
2021-12-25 18:53:49 +00:00
|
|
|
if err != nil {
|
2022-02-15 20:07:39 +00:00
|
|
|
return fmt.Errorf("Me: UnmarshalForm: %w", err)
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 20:08:30 +00:00
|
|
|
*m = *me
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-29 17:50:45 +00:00
|
|
|
// UnmarshalJSON implements custom unmarshler for JSON.
|
2021-12-29 20:08:30 +00:00
|
|
|
func (m *Me) UnmarshalJSON(v []byte) error {
|
|
|
|
src, err := strconv.Unquote(string(v))
|
|
|
|
if err != nil {
|
2022-02-15 20:07:39 +00:00
|
|
|
return fmt.Errorf("Me: UnmarshalJSON: %w", err)
|
2021-12-29 20:08:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 14:54:37 +00:00
|
|
|
me, err := ParseMe(src)
|
2021-12-29 20:08:30 +00:00
|
|
|
if err != nil {
|
2022-02-15 20:07:39 +00:00
|
|
|
return fmt.Errorf("Me: UnmarshalJSON: %w", err)
|
2021-12-29 20:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*m = *me
|
2021-12-25 18:53:49 +00:00
|
|
|
|
|
|
|
return nil
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:50:45 +00:00
|
|
|
// MarshalJSON implements custom marshler for JSON.
|
2022-01-12 18:04:40 +00:00
|
|
|
func (m Me) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(strconv.Quote(m.String())), nil
|
|
|
|
}
|
|
|
|
|
2022-01-29 17:50:45 +00:00
|
|
|
// URL returns copy of parsed me in *url.URL representation.
|
2022-01-12 18:04:40 +00:00
|
|
|
func (m Me) URL() *url.URL {
|
2023-03-16 15:30:00 +00:00
|
|
|
if m.me == nil {
|
2021-12-29 20:08:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-16 15:30:00 +00:00
|
|
|
out, _ := url.Parse(m.me.String())
|
2023-01-02 01:15:11 +00:00
|
|
|
|
|
|
|
return out
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:50:45 +00:00
|
|
|
// String returns string representation of me.
|
2022-01-12 18:04:40 +00:00
|
|
|
func (m Me) String() string {
|
2023-03-16 15:30:00 +00:00
|
|
|
if m.me != nil {
|
|
|
|
return m.me.String()
|
2021-12-29 20:08:30 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 18:07:37 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m Me) GoString() string {
|
|
|
|
return "domain.Me(" + m.String() + ")"
|
2021-12-03 01:58:37 +00:00
|
|
|
}
|