-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.go
67 lines (60 loc) · 1.58 KB
/
utils.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
package cbauth
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"net"
"strconv"
"strings"
"github.com/couchbase/cbauth/httpreq"
)
// SplitHostPort separates hostport into string host and numeric port.
func SplitHostPort(hostport string) (host string, port int, err error) {
host, portS, err := net.SplitHostPort(hostport)
if err != nil {
return "", 0, err
}
port64, err := strconv.ParseUint(portS, 10, 16)
if err != nil {
return "", 0, fmt.Errorf("port of `%s' is expected to be numeric but isn't: %v", hostport, err)
}
port = int(port64)
return
}
func extractBase64Pair(s string) (user, extra string, err error) {
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return
}
idx := bytes.IndexByte(decoded, ':')
if idx < 0 {
err = errors.New("Malformed header")
return
}
user = string(decoded[0:idx])
extra = string(decoded[(idx + 1):])
return
}
// ExtractCredsGeneric extracts Basic auth creds from header.
func ExtractCredsGeneric(hdr httpreq.HttpHeader) (user string, pwd string, err error) {
auth := hdr.Get("Authorization")
if auth == "" {
return "", "", nil
}
basicPrefix := "Basic "
if !strings.HasPrefix(auth, basicPrefix) {
err = errors.New("Non-basic auth is not supported")
return
}
return extractBase64Pair(auth[len(basicPrefix):])
}
// ExtractOnBehalfIdentityGeneric extracts 'on behalf' identity from header.
func ExtractOnBehalfIdentityGeneric(hdr httpreq.HttpHeader) (user string,
domain string, err error) {
onBehalf := hdr.Get("cb-on-behalf-of")
if onBehalf == "" {
return "", "", nil
}
return extractBase64Pair(onBehalf)
}