...

Source file src/github.com/twmb/franz-go/pkg/sasl/plain/plain.go

Documentation: github.com/twmb/franz-go/pkg/sasl/plain

     1  // Package plain provides PLAIN sasl authentication as specified in RFC4616.
     2  package plain
     3  
     4  import (
     5  	"context"
     6  	"errors"
     7  
     8  	"github.com/twmb/franz-go/pkg/sasl"
     9  )
    10  
    11  // Auth contains information for authentication.
    12  type Auth struct {
    13  	// Zid is an optional authorization ID to use in authenticating.
    14  	Zid string
    15  
    16  	// User is username to use for authentication.
    17  	User string
    18  
    19  	// Pass is the password to use for authentication.
    20  	Pass string
    21  
    22  	_ struct{} // require explicit field initialization
    23  }
    24  
    25  // AsMechanism returns a sasl mechanism that will use 'a' as credentials for
    26  // all sasl sessions.
    27  //
    28  // This is a shortcut for using the Plain function and is useful when you do
    29  // not need to live-rotate credentials.
    30  func (a Auth) AsMechanism() sasl.Mechanism {
    31  	return Plain(func(context.Context) (Auth, error) {
    32  		return a, nil
    33  	})
    34  }
    35  
    36  // Plain returns a sasl mechanism that will call authFn whenever sasl
    37  // authentication is needed. The returned Auth is used for a single session.
    38  func Plain(authFn func(context.Context) (Auth, error)) sasl.Mechanism {
    39  	return plain(authFn)
    40  }
    41  
    42  type plain func(context.Context) (Auth, error)
    43  
    44  func (plain) Name() string { return "PLAIN" }
    45  func (fn plain) Authenticate(ctx context.Context, _ string) (sasl.Session, []byte, error) {
    46  	auth, err := fn(ctx)
    47  	if err != nil {
    48  		return nil, nil, err
    49  	}
    50  	if auth.User == "" || auth.Pass == "" {
    51  		return nil, nil, errors.New("PLAIN user and pass must be non-empty")
    52  	}
    53  	return session{}, []byte(auth.Zid + "\x00" + auth.User + "\x00" + auth.Pass), nil
    54  }
    55  
    56  type session struct{}
    57  
    58  func (session) Challenge([]byte) (bool, []byte, error) {
    59  	return true, nil, nil
    60  }
    61  

View as plain text