github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/auth/identity.go (about)

     1  package auth
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  // IdentityTokenFile reads a JWT from a file on each call, supporting
    11  // automatic rotation (e.g. Kubernetes projected service account tokens).
    12  type IdentityTokenFile struct {
    13  	Path string
    14  }
    15  
    16  func (p *IdentityTokenFile) GetIdentityToken(_ context.Context) (string, error) {
    17  	if p.Path == "" {
    18  		return "", fmt.Errorf("identity token file path is empty")
    19  	}
    20  	data, err := os.ReadFile(p.Path)
    21  	if err != nil {
    22  		return "", fmt.Errorf("failed to read identity token file %q: %w", p.Path, err)
    23  	}
    24  	token := strings.TrimSpace(string(data))
    25  	if token == "" {
    26  		return "", fmt.Errorf("identity token file %q is empty", p.Path)
    27  	}
    28  	return token, nil
    29  }
    30  
    31  // IdentityTokenValue wraps a static JWT string as an [IdentityTokenProvider].
    32  type IdentityTokenValue struct {
    33  	Token string
    34  }
    35  
    36  func (p *IdentityTokenValue) GetIdentityToken(_ context.Context) (string, error) {
    37  	if p.Token == "" {
    38  		return "", fmt.Errorf("identity token value is empty")
    39  	}
    40  	return p.Token, nil
    41  }