Repository navigation
Expand file tree
/
Copy pathauthMsg.go
More file actions
64 lines (56 loc) · 1.47 KB
/
Copy pathauthMsg.go
File metadata and controls
64 lines (56 loc) · 1.47 KB
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
package client
import (
"strings"
)
// authMsg models a message in the Haystack authorization header format.
// They follow the form: "[scheme] <name1>=<val1>, <name2>=<val2>, ..."
type authMsg struct {
scheme string
attrs map[string]string
}
func authMsgFromString(str string) authMsg {
attrs := make(map[string]string)
attributeStrs := strings.Split(str, ",")
scheme := ""
// The first one MAY include the scheme but not necessarily. Handle both situations
firstAttr := attributeStrs[0]
if strings.Contains(attributeStrs[0], " ") {
schemeSplit := strings.Split(firstAttr, " ")
scheme = strings.TrimSpace(schemeSplit[0])
attributeStrs[0] = schemeSplit[1]
}
for _, attributeStr := range attributeStrs {
attributeSplit := strings.Split(attributeStr, "=")
name := strings.TrimSpace(attributeSplit[0])
val := strings.TrimSpace(attributeSplit[1])
attrs[name] = val
}
return authMsg{
scheme: scheme,
attrs: attrs,
}
}
func (authMsg *authMsg) get(attrName string) string {
return authMsg.attrs[attrName]
}
func (authMsg *authMsg) toString() string {
builder := new(strings.Builder)
if authMsg.scheme != "" {
builder.WriteString(strings.ToUpper(authMsg.scheme))
builder.WriteRune(' ')
}
firstVal := true
for name, val := range authMsg.attrs {
if firstVal {
firstVal = false
} else {
builder.WriteString(", ")
}
builder.WriteString(name)
if val != "" {
builder.WriteRune('=')
builder.WriteString(val)
}
}
return builder.String()
}