forked from colinmarc/hdfs
/
challenge.go
66 lines (57 loc) · 1.7 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
package rpc
import (
"fmt"
"regexp"
"strings"
hadoop "github.com/stubey/hdfs/v2/internal/protocol/hadoop_common"
)
const (
// qopAuthenication is how the namenode refers to authentication mode, which
// only establishes mutual authentication without encryption (the default).
qopAuthentication = "auth"
// qopIntegrity is how the namenode refers to integrity mode, which, in
// in addition to authentication, verifies the signature of RPC messages.
qopIntegrity = "auth-int"
// qopPrivacy is how the namenode refers to privacy mode, which, in addition
// to authentication and integrity, provides full end-to-end encryption for
// RPC messages.
qopPrivacy = "auth-conf"
)
var challengeRegexp = regexp.MustCompile(",?([a-zA-Z0-9]+)=(\"([^\"]+)\"|([^,]+)),?")
type tokenChallenge struct {
realm string
nonce string
qop string
charset string
cipher []string
algorithm string
}
// parseChallenge returns a tokenChallenge parsed from a challenge response from
// the namenode.
func parseChallenge(auth *hadoop.RpcSaslProto_SaslAuth) (*tokenChallenge, error) {
tokenChallenge := tokenChallenge{}
matched := challengeRegexp.FindAllSubmatch(auth.Challenge, -1)
if matched == nil {
return nil, fmt.Errorf("invalid token challenge: %s", auth.Challenge)
}
for _, m := range matched {
key := string(m[1])
val := string(m[3])
switch key {
case "realm":
tokenChallenge.realm = val
case "nonce":
tokenChallenge.nonce = val
case "qop":
tokenChallenge.qop = val
case "charset":
tokenChallenge.charset = val
case "cipher":
tokenChallenge.cipher = strings.Split(val, ",")
case "algorithm":
tokenChallenge.algorithm = val
default:
}
}
return &tokenChallenge, nil
}