-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials.go
143 lines (122 loc) · 3.18 KB
/
credentials.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package httpclient
import (
"encoding/json"
"errors"
"io"
"net/url"
"sort"
"strings"
)
var NoCredentialsErr = errors.New("Matching login credentials not found")
type Credentials interface {
Login(uri *url.URL, realm string) (username, password string, err error)
}
type Credential struct {
Domain string
Path string
Username string
Password string
}
func NewCredential(domain, path, username, password string) Credential {
return Credential{
Domain: strings.ToLower(domain),
Path: path,
Username: username,
Password: password,
}
}
func (c Credential) Matches(uri *url.URL) bool {
return c.domainMatch(uri.Host) && c.pathMatch(uri.Path)
}
func (c Credential) domainMatch(domain string) bool {
s := strings.ToLower(domain)
if c.Domain == "" || c.Domain == s {
return true
}
if strings.HasSuffix(s, c.Domain) && strings.Count(c.Domain, ".") >= 1 {
if s[len(s)-len(c.Domain)-1] == '.' {
return true
}
}
return strings.HasPrefix(c.Domain, ".") && strings.HasSuffix(s, c.Domain)
}
func (c Credential) pathMatch(path string) bool {
if c.Path == "" || c.Path == path {
return true
}
if strings.HasPrefix(path, c.Path) {
if strings.HasSuffix(c.Path, "/") {
return true
}
return path[len(c.Path)] == '/'
}
return false
}
func NewCredentialsJSON(r io.Reader) (c Credentials, err error) {
if r == nil {
err = errors.New("nil io.Reader")
return nil, err
}
oc := &OrderedCredentials{}
v := make([]Credential, 0)
dec := json.NewDecoder(r)
err = dec.Decode(&v)
if err == nil {
for i := range v {
v[i].Domain = strings.ToLower(v[i].Domain)
}
oc.v = v
sort.Sort(oc)
}
return oc, err
}
type OrderedCredentials struct {
v []Credential
}
func (c *OrderedCredentials) Login(uri *url.URL, realm string) (username, password string, err error) {
for _, v := range c.v {
if v.Matches(uri) {
return v.Username, v.Password, nil
}
}
// prompt for usernamse/password for realm could be made here
return "", "", NoCredentialsErr
}
func (c *OrderedCredentials) Len() int { return len(c.v) }
func (c *OrderedCredentials) Swap(i, j int) { c.v[i], c.v[j] = c.v[j], c.v[i] }
func (c *OrderedCredentials) Less(i, j int) bool {
// sort non-empty domains before empty ones
if c.v[i].Domain == "" && c.v[j].Domain != "" {
return false
} else if c.v[i].Domain != "" && c.v[j].Domain == "" {
return true
}
// sort fully qualified domains before partial ones
if !strings.HasPrefix(c.v[i].Domain, ".") && strings.HasPrefix(c.v[j].Domain, ".") {
return true
} else if strings.HasPrefix(c.v[i].Domain, ".") && !strings.HasPrefix(c.v[j].Domain, ".") {
return false
}
// sort by number of domain components, longest to shortest
a, b := strings.Count(c.v[i].Domain, "."), strings.Count(c.v[j].Domain, ".")
if a > b {
return true
} else if a < b {
return false
}
// sort by domain name
if c.v[i].Domain < c.v[j].Domain {
return true
} else if c.v[i].Domain > c.v[j].Domain {
return false
}
// sort by number of path components, longest to shortest
a, b = strings.Count(c.v[i].Path, "/"), strings.Count(c.v[j].Path, "/")
if a > b {
return true
} else if a < b {
return false
}
// sort by path string
return c.v[i].Path < c.v[j].Path
}