forked from hacdias/indielib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverification.go
116 lines (93 loc) · 2.56 KB
/
verification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package indieauth
import (
"errors"
"net"
urlpkg "net/url"
"strings"
)
var (
ErrInvalidScheme error = errors.New("scheme must be either http or https")
ErrEmptyPath error = errors.New("path must not be empty")
ErrInvalidPath error = errors.New("path cannot contain single or double dots")
ErrInvalidFragment error = errors.New("fragment must be empty")
ErrUserIsSet error = errors.New("user and or password must not be set")
ErrPortIsSet error = errors.New("port must not be set")
ErrIsIP error = errors.New("profile cannot be ip address")
ErrIsNonLoopback error = errors.New("client id cannot be non-loopback ip")
)
// IsValidProfileURL validates the profile URL according to the specification.
// https://indieauth.spec.indieweb.org/#user-profile-url
func IsValidProfileURL(profile string) error {
url, err := urlpkg.Parse(profile)
if err != nil {
return err
}
if url.Scheme != "http" && url.Scheme != "https" {
return ErrInvalidScheme
}
if url.Path == "" {
return ErrEmptyPath
}
if strings.Contains(url.Path, ".") || strings.Contains(url.Path, "..") {
return ErrInvalidPath
}
if url.Fragment != "" {
return ErrInvalidFragment
}
if url.User.String() != "" {
return ErrUserIsSet
}
if url.Port() != "" {
return ErrPortIsSet
}
if net.ParseIP(url.Host) != nil {
return ErrIsIP
}
return nil
}
// IsValidClientIdentifier validates a client identifier according to the specification.
// https://indieauth.spec.indieweb.org/#client-identifier
func IsValidClientIdentifier(identifier string) error {
url, err := urlpkg.Parse(identifier)
if err != nil {
return err
}
if url.Scheme != "http" && url.Scheme != "https" {
return ErrInvalidScheme
}
if url.Path == "" {
return ErrEmptyPath
}
if strings.Contains(url.Path, ".") || strings.Contains(url.Path, "..") {
return ErrInvalidPath
}
if url.Fragment != "" {
return ErrInvalidFragment
}
if url.User.String() != "" {
return ErrUserIsSet
}
if v := net.ParseIP(url.Host); v != nil {
if !v.IsLoopback() {
return ErrIsNonLoopback
}
}
return nil
}
// CanonicalizeURL checks if a URL has a path, and appends a path "/""
// if it has no path.
func CanonicalizeURL(urlStr string) string {
// NOTE: parsing a URL without scheme will most likely put the host as path.
// That's why I add it first.
if !strings.HasPrefix(urlStr, "http://") && !strings.HasPrefix(urlStr, "https://") {
urlStr = "https://" + urlStr
}
url, err := urlpkg.Parse(urlStr)
if err != nil {
return urlStr
}
if url.Path == "" {
url.Path = "/"
}
return url.String()
}