github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/credentials/anonymous.go (about)

     1  package credentials
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
     9  )
    10  
    11  var (
    12  	_ Credentials                = (*Anonymous)(nil)
    13  	_ fmt.Stringer               = (*Anonymous)(nil)
    14  	_ AnonymousCredentialsOption = SourceInfoOption("")
    15  )
    16  
    17  type AnonymousCredentialsOption interface {
    18  	ApplyAnonymousCredentialsOption(c *Anonymous)
    19  }
    20  
    21  // Anonymous implements Credentials interface with Anonymous access
    22  type Anonymous struct {
    23  	sourceInfo string
    24  }
    25  
    26  func NewAnonymousCredentials(opts ...AnonymousCredentialsOption) *Anonymous {
    27  	c := &Anonymous{
    28  		sourceInfo: stack.Record(1),
    29  	}
    30  	for _, opt := range opts {
    31  		opt.ApplyAnonymousCredentialsOption(c)
    32  	}
    33  
    34  	return c
    35  }
    36  
    37  // Token implements Credentials.
    38  func (c Anonymous) Token(_ context.Context) (string, error) {
    39  	return "", nil
    40  }
    41  
    42  // Token implements Credentials.
    43  func (c Anonymous) String() string {
    44  	buffer := xstring.Buffer()
    45  	defer buffer.Free()
    46  	buffer.WriteString("Anonymous{")
    47  	if c.sourceInfo != "" {
    48  		buffer.WriteString("From:")
    49  		fmt.Fprintf(buffer, "%q", c.sourceInfo)
    50  	}
    51  	buffer.WriteByte('}')
    52  
    53  	return buffer.String()
    54  }