auth

package
v1.0.65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package auth provides authentication utilities and helper functions for AI provider implementations

Index

Constants

View Source
const (
	// ContextKeyOAuthToken is the context key for injecting an OAuth token
	//nolint:gosec // G101: This is a context key name, not a credential
	ContextKeyOAuthToken contextKey = "oauth_token"

	// ContextKeyAPIKey is the context key for injecting an API key
	//nolint:gosec // G101: This is a context key name, not a credential
	ContextKeyAPIKey contextKey = "api_key"

	// ContextKeyAuthType is the context key for specifying auth type ("bearer", "api_key")
	ContextKeyAuthType contextKey = "auth_type"
)

Variables

This section is empty.

Functions

func GetAPIKey

func GetAPIKey(ctx context.Context) string

GetAPIKey extracts API key from context, returns empty string if not present

func GetAuthType

func GetAuthType(ctx context.Context) string

GetAuthType extracts auth type from context, defaults to "bearer"

func GetOAuthToken

func GetOAuthToken(ctx context.Context) string

GetOAuthToken extracts OAuth token from context, returns empty string if not present

func WithAPIKey

func WithAPIKey(ctx context.Context, apiKey string) context.Context

WithAPIKey returns a context with the API key set

func WithAuthType

func WithAuthType(ctx context.Context, authType string) context.Context

WithAuthType returns a context with the auth type set

func WithOAuthToken

func WithOAuthToken(ctx context.Context, token string) context.Context

WithOAuthToken returns a context with the OAuth token set

Types

type AuthHelper

type AuthHelper struct {
	ProviderName string
	KeyManager   *auth.APIKeyManagerImpl
	OAuthManager *oauthmanager.OAuthKeyManager
	HTTPClient   *http.Client
	Config       types.ProviderConfig
}

AuthHelper provides shared authentication functionality for providers

func NewAuthHelper

func NewAuthHelper(providerName string, config types.ProviderConfig, client *http.Client) *AuthHelper

NewAuthHelper creates a new authentication helper

func (*AuthHelper) ClearAuthentication

func (h *AuthHelper) ClearAuthentication()

ClearAuthentication clears all authentication credentials

func (*AuthHelper) CreateJSONRequest

func (h *AuthHelper) CreateJSONRequest(
	ctx context.Context,
	method, url string,
	body interface{},
	credential string,
	authType string,
) (*http.Request, error)

CreateJSONRequest creates an HTTP request with JSON body and standard headers This is a convenience method that combines request creation, JSON marshaling, and header setup commonly used across providers.

func (*AuthHelper) ExecuteWithAuth

func (h *AuthHelper) ExecuteWithAuth(
	ctx context.Context,
	options types.GenerateOptions,
	oauthOperation func(context.Context, *types.OAuthCredentialSet) (string, *types.Usage, error),
	apiKeyOperation func(context.Context, string) (string, *types.Usage, error),
) (string, *types.Usage, error)

ExecuteWithAuth executes an operation using available authentication methods Automatically chooses OAuth over API key, with failover support Returns string content only - use ExecuteWithAuthMessage for full message support

func (*AuthHelper) ExecuteWithAuthMessage

func (h *AuthHelper) ExecuteWithAuthMessage(
	ctx context.Context,
	options types.GenerateOptions,
	oauthOperation func(context.Context, *types.OAuthCredentialSet) (types.ChatMessage, *types.Usage, error),
	apiKeyOperation func(context.Context, string) (types.ChatMessage, *types.Usage, error),
) (types.ChatMessage, *types.Usage, error)

ExecuteWithAuthMessage executes an operation using available authentication methods Returns full ChatMessage to preserve tool calls. Use this for chat completions.

func (*AuthHelper) GetAuthMethod

func (h *AuthHelper) GetAuthMethod() string

GetAuthMethod returns the currently available authentication method

func (*AuthHelper) GetAuthStatus

func (h *AuthHelper) GetAuthStatus() map[string]interface{}

GetAuthStatus returns a detailed authentication status

func (*AuthHelper) HandleAuthError

func (h *AuthHelper) HandleAuthError(err error, statusCode int) error

HandleAuthError handles authentication-specific errors and provides user-friendly messages

func (*AuthHelper) IsAPIKeyConfigured

func (h *AuthHelper) IsAPIKeyConfigured() bool

IsAPIKeyConfigured checks if API keys are configured and available

func (*AuthHelper) IsAuthenticated

func (h *AuthHelper) IsAuthenticated() bool

IsAuthenticated checks if any authentication method is configured

func (*AuthHelper) IsOAuthConfigured

func (h *AuthHelper) IsOAuthConfigured() bool

IsOAuthConfigured checks if OAuth credentials are configured and available

func (*AuthHelper) MakeAuthenticatedRequest

func (h *AuthHelper) MakeAuthenticatedRequest(
	ctx context.Context,
	method, url string,
	headers map[string]string,
	body interface{},
) (*http.Response, error)

MakeAuthenticatedRequest makes an HTTP request with proper authentication

func (*AuthHelper) RefreshAllOAuthTokens

func (h *AuthHelper) RefreshAllOAuthTokens(ctx context.Context) error

RefreshAllOAuthTokens attempts to refresh all OAuth tokens

func (*AuthHelper) SetAuthHeaders

func (h *AuthHelper) SetAuthHeaders(req *http.Request, authToken string, authType string)

SetAuthHeaders sets appropriate authentication headers on HTTP requests

func (*AuthHelper) SetAuthHeadersFromContext

func (h *AuthHelper) SetAuthHeadersFromContext(ctx context.Context, req *http.Request) bool

SetAuthHeadersFromContext sets auth headers using context-provided credentials

func (*AuthHelper) SetProviderSpecificHeaders

func (h *AuthHelper) SetProviderSpecificHeaders(req *http.Request)

SetProviderSpecificHeaders sets provider-specific headers beyond auth

func (*AuthHelper) SetupAPIKeys

func (h *AuthHelper) SetupAPIKeys()

SetupAPIKeys configures API key management with support for multiple keys Extracts keys from both config.APIKey and config.ProviderConfig["api_keys"]

func (*AuthHelper) SetupOAuth

func (h *AuthHelper) SetupOAuth(refreshFunc oauthmanager.RefreshFunc)

SetupOAuth configures OAuth management with multiple credential support

func (*AuthHelper) ValidateAuthConfig

func (h *AuthHelper) ValidateAuthConfig(authConfig types.AuthConfig) error

ValidateAuthConfig validates authentication configuration

type OAuthRefreshHelper

type OAuthRefreshHelper struct {
	ProviderName string
	HTTPClient   *http.Client
}

OAuthRefreshHelper provides common OAuth token refresh implementations

func NewOAuthRefreshHelper

func NewOAuthRefreshHelper(providerName string, client *http.Client) *OAuthRefreshHelper

NewOAuthRefreshHelper creates a new OAuth refresh helper

func (*OAuthRefreshHelper) AnthropicOAuthRefresh

func (h *OAuthRefreshHelper) AnthropicOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)

AnthropicOAuthRefresh implements Anthropic's OAuth 2.0 token refresh

func (*OAuthRefreshHelper) GeminiOAuthRefresh

GeminiOAuthRefresh implements Google's OAuth 2.0 token refresh using the oauth2 library

func (*OAuthRefreshHelper) GenericOAuthRefresh

func (h *OAuthRefreshHelper) GenericOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet, tokenURL string) (*types.OAuthCredentialSet, error)

GenericOAuthRefresh provides a generic OAuth 2.0 token refresh implementation

func (*OAuthRefreshHelper) OpenAIOAuthRefresh

OpenAIOAuthRefresh implements OpenAI's OAuth 2.0 token refresh

func (*OAuthRefreshHelper) QwenOAuthRefresh

QwenOAuthRefresh implements Qwen's OAuth 2.0 token refresh

type RefreshFuncFactory

type RefreshFuncFactory struct {
	Helper *OAuthRefreshHelper
}

RefreshFuncFactory creates refresh functions for different providers

func NewRefreshFuncFactory

func NewRefreshFuncFactory(providerName string, client *http.Client) *RefreshFuncFactory

NewRefreshFuncFactory creates a new refresh function factory

func (*RefreshFuncFactory) CreateAnthropicRefreshFunc

func (f *RefreshFuncFactory) CreateAnthropicRefreshFunc() oauthmanager.RefreshFunc

CreateAnthropicRefreshFunc creates an Anthropic refresh function

func (*RefreshFuncFactory) CreateGeminiRefreshFunc

func (f *RefreshFuncFactory) CreateGeminiRefreshFunc() oauthmanager.RefreshFunc

CreateGeminiRefreshFunc creates a Gemini refresh function

func (*RefreshFuncFactory) CreateGenericRefreshFunc

func (f *RefreshFuncFactory) CreateGenericRefreshFunc(tokenURL string) oauthmanager.RefreshFunc

CreateGenericRefreshFunc creates a generic refresh function for custom providers

func (*RefreshFuncFactory) CreateOpenAIRefreshFunc

func (f *RefreshFuncFactory) CreateOpenAIRefreshFunc() oauthmanager.RefreshFunc

CreateOpenAIRefreshFunc creates an OpenAI refresh function

func (*RefreshFuncFactory) CreateQwenRefreshFunc

func (f *RefreshFuncFactory) CreateQwenRefreshFunc() oauthmanager.RefreshFunc

CreateQwenRefreshFunc creates a Qwen refresh function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL