github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/chunk/cassandra/authenticator.go (about)

     1  package cassandra
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gocql/gocql"
     7  )
     8  
     9  // CustomPasswordAuthenticator provides the default behaviour for Username/Password authentication with
    10  // Cassandra while allowing users to specify a non-default Authenticator to accept.
    11  type CustomPasswordAuthenticator struct {
    12  	ApprovedAuthenticators []string
    13  	Username               string
    14  	Password               string
    15  }
    16  
    17  func (p CustomPasswordAuthenticator) approve(authenticator string) bool {
    18  	for _, s := range p.ApprovedAuthenticators {
    19  		if authenticator == s {
    20  			return true
    21  		}
    22  	}
    23  	return false
    24  }
    25  
    26  // Challenge verifies the name of the authenticator and formats the provided username and password
    27  // into a response
    28  func (p CustomPasswordAuthenticator) Challenge(req []byte) ([]byte, gocql.Authenticator, error) {
    29  	if !p.approve(string(req)) {
    30  		return nil, nil, fmt.Errorf("unexpected authenticator %q", req)
    31  	}
    32  	resp := make([]byte, 2+len(p.Username)+len(p.Password))
    33  	resp[0] = 0
    34  	copy(resp[1:], p.Username)
    35  	resp[len(p.Username)+1] = 0
    36  	copy(resp[2+len(p.Username):], p.Password)
    37  	return resp, nil, nil
    38  }
    39  
    40  // Success returns nil by default, identical to the default PasswordAuthenticator
    41  func (p CustomPasswordAuthenticator) Success(data []byte) error {
    42  	return nil
    43  }