package chatclient import ( "context" "fmt" "strings" "google.golang.org/grpc/credentials" ) type TokenProvider func(ctx context.Context) (string, error) func StaticToken(token string) TokenProvider { return func(context.Context) (string, error) { if strings.TrimSpace(token) == "" { return "", fmt.Errorf("%w: empty service token", ErrInvalidConfig) } return token, nil } } type tokenCredentials struct { provider TokenProvider requireTransportSecurity bool } func (c tokenCredentials) GetRequestMetadata(ctx context.Context, _ ...string) (map[string]string, error) { token, err := c.provider(ctx) if err != nil { return nil, err } if strings.TrimSpace(token) == "" { return nil, fmt.Errorf("%w: empty service token", ErrInvalidConfig) } return map[string]string{"authorization": "Bearer " + token}, nil } func (c tokenCredentials) RequireTransportSecurity() bool { return c.requireTransportSecurity } var _ credentials.PerRPCCredentials = tokenCredentials{}