forked from grafana/loki
/
authenticator.go
43 lines (37 loc) · 1.22 KB
/
authenticator.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
package cassandra
import (
"fmt"
"github.com/gocql/gocql"
)
// CustomPasswordAuthenticator provides the default behaviour for Username/Password authentication with
// Cassandra while allowing users to specify a non-default Authenticator to accept.
type CustomPasswordAuthenticator struct {
ApprovedAuthenticators []string
Username string
Password string
}
func (p CustomPasswordAuthenticator) approve(authenticator string) bool {
for _, s := range p.ApprovedAuthenticators {
if authenticator == s {
return true
}
}
return false
}
// Challenge verifies the name of the authenticator and formats the provided username and password
// into a response
func (p CustomPasswordAuthenticator) Challenge(req []byte) ([]byte, gocql.Authenticator, error) {
if !p.approve(string(req)) {
return nil, nil, fmt.Errorf("unexpected authenticator %q", req)
}
resp := make([]byte, 2+len(p.Username)+len(p.Password))
resp[0] = 0
copy(resp[1:], p.Username)
resp[len(p.Username)+1] = 0
copy(resp[2+len(p.Username):], p.Password)
return resp, nil, nil
}
// Success returns nil by default, identical to the default PasswordAuthenticator
func (p CustomPasswordAuthenticator) Success(_ []byte) error {
return nil
}