-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthdigest.go
296 lines (235 loc) · 6.82 KB
/
authdigest.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package httpbulb
import (
"bytes"
"crypto/md5"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"crypto/subtle"
"encoding/hex"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-chi/chi/v5"
)
type digestCredentials struct {
username string
realm string
nonce string
uri string
response string
qop string
nc string
cnonce string
algorithm string
}
func (d *digestCredentials) fromMap(m map[string]string) *digestCredentials {
for k, v := range m {
switch k {
case "username":
d.username = v
case "realm":
d.realm = v
case "nonce":
d.nonce = v
case "uri":
d.uri = v
case "response":
d.response = v
case "qop":
d.qop = v
case "nc":
d.nc = v
case "cnonce":
d.cnonce = v
case "algorithm":
d.algorithm = v
}
}
return d
}
// DigestAuthHandle prompts the user for authorization using HTTP Digest Auth.
func DigestAuthHandle(w http.ResponseWriter, r *http.Request) {
user := chi.URLParam(r, "user")
passwd := chi.URLParam(r, "passwd")
algorithm := chi.URLParam(r, "algorithm")
qop := chi.URLParam(r, "qop")
staleAfter := chi.URLParam(r, "stale_after")
requireCookieParam := strings.ToLower(r.URL.Query().Get("require-cookie"))
secureCookie := getURLScheme(r) == schemeHttps
var requireCookie bool
switch requireCookieParam {
case "true", "1", "t":
requireCookie = true
}
// Actually `algorithm` path parameter is not relevant,
// because algorithm will be taken from Authorization header.
// It will be relevant only if the Authorization header is not set or is not Digest.
switch algorithm {
case "MD5", "SHA-256", "SHA-512":
default:
algorithm = "MD5"
}
if staleAfter == "" {
staleAfter = "never"
}
_, hasCookie := r.Header["Cookie"]
authorization := r.Header.Get("Authorization")
credentials, err := parseDigestAuth(authorization)
if err != nil || (requireCookie && !hasCookie) {
// 401 response
setCookie(w, "stale_after", staleAfter, secureCookie)
setCookie(w, "fake", "fake_value", secureCookie)
writeDigestChallengeResponse(w, r, "httpbulb", qop, algorithm, false)
return
}
if requireCookie && getCookie(r, "fake") != "fake_value" {
// 403 response
setCookie(w, "fake", "fake_value", secureCookie)
RenderError(w, "missing cookie set on challenge", http.StatusForbidden)
return
}
currentNonce := credentials.nonce
staleAfterValue := getCookie(r, "stale_after")
lastNonce := getCookie(r, "last_nonce")
if (lastNonce != "" && currentNonce == lastNonce) || staleAfterValue == "0" {
setCookie(w, "stale_after", staleAfter, secureCookie)
setCookie(w, "fake", "fake_value", secureCookie)
setCookie(w, "last_nonce", currentNonce, secureCookie)
writeDigestChallengeResponse(w, r, "httpbulb", qop, algorithm, true)
return
}
if !checkDigestAuth(r, credentials, user, passwd) {
setCookie(w, "stale_after", staleAfter, secureCookie)
setCookie(w, "fake", "fake_value", secureCookie)
setCookie(w, "last_nonce", currentNonce, secureCookie)
writeDigestChallengeResponse(w, r, "httpbulb", qop, algorithm, false)
return
}
if staleAfterValue != "" {
setCookie(w, "stale_after", nextStaleAfterValue(staleAfterValue), secureCookie)
}
setCookie(w, "fake", "fake_value", secureCookie)
RenderResponse(w, http.StatusOK, AuthResponse{Authenticated: true, User: user})
}
func parseDigestAuth(authHeader string) (dig *digestCredentials, err error) {
if authHeader == "" {
err = fmt.Errorf("missing Authorization header")
return
}
parts := strings.SplitN(authHeader, " ", 2)
authType := parts[0]
authInfo := parts[1]
if strings.ToLower(authType) != "digest" {
err = fmt.Errorf("supported authorization type is Digest")
return
}
cm := parseHeaderValues(authInfo)
requiredCredentials := []string{"username", "realm", "nonce", "uri", "response"}
for _, cred := range requiredCredentials {
if _, ok := cm[cred]; !ok {
err = fmt.Errorf("missing required credential %q", cred)
return
}
}
dig = (&digestCredentials{}).fromMap(cm)
if dig.qop != "" {
if dig.nc == "" || dig.cnonce == "" {
err = fmt.Errorf("missing required credentials 'nc' and 'cnonce'")
return
}
}
return
}
func parseHeaderValues(value string) map[string]string {
parts := strings.Split(value, ",")
m := make(map[string]string)
for _, part := range parts {
part := strings.TrimSpace(part)
p := strings.SplitN(part, "=", 2)
key := p[0]
value := strings.Trim(p[1], `"`)
m[key] = value
}
return m
}
func writeDigestChallengeResponse(w http.ResponseWriter, r *http.Request, realm, qop, algorithm string, stale bool) {
ts := time.Now().Unix()
b := make([]byte, 10)
rand.Read(b)
opaqueB := make([]byte, 10)
rand.Read(opaqueB)
nonceBuf := new(bytes.Buffer)
nonceBuf.WriteString(r.RemoteAddr)
nonceBuf.WriteString(":")
nonceBuf.WriteString(fmt.Sprintf("%d", ts))
nonceBuf.WriteString(":")
nonceBuf.Write(b)
nonce := hexDigest(nonceBuf.Bytes(), algorithm)
opaque := hexDigest(opaqueB, algorithm)
if qop == "" {
qop = "auth"
}
value := fmt.Sprintf(
"Digest qop=%s, realm=%s, algorithm=%s, nonce=%s, opaque=%s stale=%t",
qop, realm, algorithm, nonce, opaque, stale)
w.Header().Set("WWW-Authenticate", value)
w.WriteHeader(http.StatusUnauthorized)
}
func hexDigest(data []byte, algorithm string) string {
var h []byte
switch algorithm {
case "SHA-256":
checksumB := sha256.Sum256(data)
h = checksumB[:]
case "SHA-512":
checksumB := sha512.Sum512(data)
h = checksumB[:]
default:
checksumB := md5.Sum(data)
h = checksumB[:]
}
return hex.EncodeToString(h)
}
func ha1(realm, username, password, algorithm string) string {
a1 := []byte(fmt.Sprintf("%s:%s:%s", username, realm, password))
return hexDigest(a1, algorithm)
}
func ha2(method, uri, algorithm string) string {
a2 := []byte(fmt.Sprintf("%s:%s", method, uri))
return hexDigest(a2, algorithm)
}
func compileDigestResponse(dig *digestCredentials, password, method, uri string) string {
ha1Value := ha1(dig.realm, dig.username, password, dig.algorithm)
ha2Value := ha2(method, uri, dig.algorithm)
var resp string
switch dig.qop {
case "auth", "auth-int":
resp = fmt.Sprintf("%s:%s:%s:%s:%s:%s", ha1Value, dig.nonce, dig.nc, dig.cnonce, dig.qop, ha2Value)
default:
resp = fmt.Sprintf("%s:%s:%s", ha1Value, dig.nonce, ha2Value)
}
return hexDigest([]byte(resp), dig.algorithm)
}
func checkDigestAuth(r *http.Request, dig *digestCredentials, username, password string) (ok bool) {
if dig == nil {
return
}
if dig.username != username {
return
}
responseHexDigest := compileDigestResponse(dig, password, r.Method, r.RequestURI)
expectedHexDigest := dig.response
if subtle.ConstantTimeCompare([]byte(responseHexDigest), []byte(expectedHexDigest)) == 1 {
ok = true
}
return
}
func nextStaleAfterValue(staleAfter string) string {
if num, err := strconv.Atoi(staleAfter); err == nil {
return fmt.Sprintf("%d", num-1)
}
return "never"
}