-
Notifications
You must be signed in to change notification settings - Fork 16
/
challenge.go
155 lines (147 loc) · 3.06 KB
/
challenge.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
144
145
146
147
148
149
150
151
152
153
154
155
package digest
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/icholy/digest/internal/param"
)
// Challenge is a challenge sent in the WWW-Authenticate header
type Challenge struct {
Realm string
Domain []string
Nonce string
Opaque string
Stale bool
Algorithm string
QOP []string
Charset string
Userhash bool
}
// SupportsQOP returns true if the challenge advertises support
// for the provided qop value
func (c *Challenge) SupportsQOP(qop string) bool {
for _, v := range c.QOP {
if v == qop {
return true
}
}
return false
}
// ParseChallenge parses the WWW-Authenticate header challenge
func ParseChallenge(s string) (*Challenge, error) {
s, ok := strings.CutPrefix(s, Prefix)
if !ok {
return nil, errors.New("digest: invalid challenge prefix")
}
pp, err := param.Parse(s)
if err != nil {
return nil, fmt.Errorf("digest: invalid challenge: %w", err)
}
var c Challenge
for _, p := range pp {
switch p.Key {
case "realm":
c.Realm = p.Value
case "domain":
c.Domain = strings.Fields(p.Value)
case "nonce":
c.Nonce = p.Value
case "algorithm":
c.Algorithm = p.Value
case "stale":
c.Stale = strings.ToLower(p.Value) == "true"
case "opaque":
c.Opaque = p.Value
case "qop":
c.QOP = strings.Split(p.Value, ",")
case "charset":
c.Charset = p.Value
case "userhash":
c.Userhash = strings.ToLower(p.Value) == "true"
}
}
return &c, nil
}
// String returns the foramtted header value
func (c *Challenge) String() string {
var pp []param.Param
pp = append(pp, param.Param{
Key: "realm",
Value: c.Realm,
Quote: true,
})
if len(c.Domain) != 0 {
pp = append(pp, param.Param{
Key: "domain",
Value: strings.Join(c.Domain, " "),
Quote: true,
})
}
pp = append(pp, param.Param{
Key: "nonce",
Value: c.Nonce,
Quote: true,
})
if c.Opaque != "" {
pp = append(pp, param.Param{
Key: "opaque",
Value: c.Opaque,
Quote: true,
})
}
if c.Stale {
pp = append(pp, param.Param{
Key: "stale",
Value: "true",
})
}
if c.Algorithm != "" {
pp = append(pp, param.Param{
Key: "algorithm",
Value: c.Algorithm,
})
}
if len(c.QOP) != 0 {
pp = append(pp, param.Param{
Key: "qop",
Value: strings.Join(c.QOP, ","),
Quote: true,
})
}
if c.Charset != "" {
pp = append(pp, param.Param{
Key: "charset",
Value: c.Charset,
})
}
if c.Userhash {
pp = append(pp, param.Param{
Key: "userhash",
Value: "true",
})
}
return Prefix + param.Format(pp...)
}
// ErrNoChallenge indicates that no WWW-Authenticate headers were found.
var ErrNoChallenge = errors.New("digest: no challenge found")
// FindChallenge returns the first supported challenge in the headers
func FindChallenge(h http.Header) (*Challenge, error) {
var last error
for _, header := range h.Values("WWW-Authenticate") {
if !IsDigest(header) {
continue
}
chal, err := ParseChallenge(header)
if err == nil && CanDigest(chal) {
return chal, nil
}
if err != nil {
last = err
}
}
if last != nil {
return nil, last
}
return nil, ErrNoChallenge
}