Documentation
¶
Overview ¶
Package auth provides authentication utilities and helper functions for AI provider implementations
Index ¶
- Constants
- func GetAuthType(ctx context.Context) string
- func GetOAuthToken(ctx context.Context) string
- func WithAuthType(ctx context.Context, authType string) context.Context
- func WithOAuthToken(ctx context.Context, token string) context.Context
- type AuthHelper
- func (h *AuthHelper) ClearAuthentication()
- func (h *AuthHelper) CreateJSONRequest(ctx context.Context, method, url string, body interface{}, credential string, ...) (*http.Request, error)
- func (h *AuthHelper) ExecuteWithAuth(ctx context.Context, options types.GenerateOptions, ...) (string, *types.Usage, error)
- func (h *AuthHelper) ExecuteWithAuthMessage(ctx context.Context, options types.GenerateOptions, ...) (types.ChatMessage, *types.Usage, error)
- func (h *AuthHelper) GetAuthMethod() string
- func (h *AuthHelper) GetAuthStatus() map[string]interface{}
- func (h *AuthHelper) HandleAuthError(err error, statusCode int) error
- func (h *AuthHelper) IsAuthenticated() bool
- func (h *AuthHelper) MakeAuthenticatedRequest(ctx context.Context, method, url string, headers map[string]string, ...) (*http.Response, error)
- func (h *AuthHelper) RefreshAllOAuthTokens(ctx context.Context) error
- func (h *AuthHelper) SetAuthHeaders(req *http.Request, authToken string, authType string)
- func (h *AuthHelper) SetAuthHeadersFromContext(ctx context.Context, req *http.Request) bool
- func (h *AuthHelper) SetProviderSpecificHeaders(req *http.Request)
- func (h *AuthHelper) SetupAPIKeys()
- func (h *AuthHelper) SetupOAuth(refreshFunc oauthmanager.RefreshFunc)
- func (h *AuthHelper) ValidateAuthConfig(authConfig types.AuthConfig) error
- type OAuthRefreshHelper
- func (h *OAuthRefreshHelper) AnthropicOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
- func (h *OAuthRefreshHelper) GeminiOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
- func (h *OAuthRefreshHelper) GenericOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet, tokenURL string) (*types.OAuthCredentialSet, error)
- func (h *OAuthRefreshHelper) OpenAIOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
- func (h *OAuthRefreshHelper) QwenOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
- type RefreshFuncFactory
- func (f *RefreshFuncFactory) CreateAnthropicRefreshFunc() oauthmanager.RefreshFunc
- func (f *RefreshFuncFactory) CreateGeminiRefreshFunc() oauthmanager.RefreshFunc
- func (f *RefreshFuncFactory) CreateGenericRefreshFunc(tokenURL string) oauthmanager.RefreshFunc
- func (f *RefreshFuncFactory) CreateOpenAIRefreshFunc() oauthmanager.RefreshFunc
- func (f *RefreshFuncFactory) CreateQwenRefreshFunc() oauthmanager.RefreshFunc
Constants ¶
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" // ContextKeyAuthType is the context key for specifying auth type ("bearer", "api_key") ContextKeyAuthType contextKey = "auth_type" )
Variables ¶
This section is empty.
Functions ¶
func GetAuthType ¶
GetAuthType extracts auth type from context, defaults to "bearer"
func GetOAuthToken ¶
GetOAuthToken extracts OAuth token from context, returns empty string if not present
func WithAuthType ¶
WithAuthType returns a context with the auth type set
Types ¶
type AuthHelper ¶
type AuthHelper struct {
ProviderName string
KeyManager *keymanager.KeyManager
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) IsAuthenticated ¶
func (h *AuthHelper) IsAuthenticated() bool
IsAuthenticated checks if any authentication method is configured
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 ¶
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 ¶
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 ¶
func (h *OAuthRefreshHelper) GeminiOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
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 ¶
func (h *OAuthRefreshHelper) OpenAIOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
OpenAIOAuthRefresh implements OpenAI's OAuth 2.0 token refresh
func (*OAuthRefreshHelper) QwenOAuthRefresh ¶
func (h *OAuthRefreshHelper) QwenOAuthRefresh(ctx context.Context, cred *types.OAuthCredentialSet) (*types.OAuthCredentialSet, error)
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