l-ai-chat-client-go/auth.go
Kaien 9ac1206f2e Add reusable live chat Go SDK
Provide a standalone agent client for l-ai-chat with service-token auth, safe at-least-once subscriptions, ordered cumulative ACK tracking, and self-contained gRPC fake tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-09 21:17:31 +03:00

43 lines
1012 B
Go

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{}