-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathheader.go
88 lines (73 loc) · 1.93 KB
/
header.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
package basicauth
import (
"encoding/base64"
"strings"
)
const (
spaceChar = ' '
colonChar = ':'
colonLiteral = string(colonChar)
basicLiteral = "Basic"
basicSpaceLiteral = "Basic "
basicSpaceLiteralLen = len(basicSpaceLiteral)
)
// The username and password are combined with a single colon (:).
// This means that the username itself cannot contain a colon.
// URL encoding (e.g. https://Aladdin:[email protected]/index.html)
// has been deprecated by rfc3986.
func encodeHeader(username, password string) (string, bool) {
if strings.Contains(username, colonLiteral) || strings.Contains(password, colonLiteral) {
return "", false
}
fullUser := []byte(username + colonLiteral + password)
header := basicSpaceLiteral + base64.StdEncoding.EncodeToString(fullUser)
return header, true
}
// Like net/http.parseBasicAuth
func decodeHeader(header string) (fullUser, username, password string, ok bool) {
if len(header) < basicSpaceLiteralLen || !strings.EqualFold(header[:basicSpaceLiteralLen], basicSpaceLiteral) {
return
}
c, err := base64.StdEncoding.DecodeString(header[basicSpaceLiteralLen:])
if err != nil {
return
}
cs := string(c)
s := strings.IndexByte(cs, colonChar)
if s < 0 {
return
}
return cs, cs[:s], cs[s+1:], true
/*
for i := 0; i < n; i++ {
if header[i] == spaceChar {
prefix := header[:i]
if prefix != basicLiteral {
return
}
if n <= i+1 {
return
}
decodedFullUser, err := base64.RawStdEncoding.DecodeString(header[i+1:])
if err != nil {
return
}
fullUser = string(decodedFullUser)
break
}
}
n = len(fullUser)
for i := n - 1; i > -1; i-- {
if fullUser[i] == colonChar {
username = fullUser[:i]
password = fullUser[i+1:]
if strings.TrimSpace(username) == "" || strings.TrimSpace(password) == "" {
ok = false
} else {
ok = true
}
return
}
}
return*/
}