Documentation
¶
Overview ¶
Package oauthcommon provides shared OAuth server, error types, and HTML templates used by the Claude (Anthropic) and Codex (OpenAI) authentication flows.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidState represents an error for invalid OAuth state parameter. ErrInvalidState = &AuthenticationError{ Type: "invalid_state", Message: "OAuth state parameter is invalid", Code: http.StatusBadRequest, } // ErrCodeExchangeFailed represents an error when exchanging authorization code for tokens fails. ErrCodeExchangeFailed = &AuthenticationError{ Type: "code_exchange_failed", Message: "Failed to exchange authorization code for tokens", Code: http.StatusBadRequest, } // ErrServerStartFailed represents an error when starting the OAuth callback server fails. ErrServerStartFailed = &AuthenticationError{ Type: "server_start_failed", Message: "Failed to start OAuth callback server", Code: http.StatusInternalServerError, } // ErrPortInUse represents an error when the OAuth callback port is already in use. ErrPortInUse = &AuthenticationError{ Type: "port_in_use", Message: "OAuth callback port is already in use", Code: 13, } // ErrCallbackTimeout represents an error when waiting for OAuth callback times out. ErrCallbackTimeout = &AuthenticationError{ Type: "callback_timeout", Message: "Timeout waiting for OAuth callback", Code: http.StatusRequestTimeout, } // ErrBrowserOpenFailed represents an error when opening the browser for authentication fails. ErrBrowserOpenFailed = &AuthenticationError{ Type: "browser_open_failed", Message: "Failed to open browser for authentication", Code: http.StatusInternalServerError, } )
Common authentication error types.
Functions ¶
func GetUserFriendlyMessage ¶
GetUserFriendlyMessage returns a user-friendly error message based on the error type.
Types ¶
type AuthenticationError ¶
type AuthenticationError struct {
// Type is the type of authentication error.
Type string `json:"type"`
// Message is a human-readable message describing the error.
Message string `json:"message"`
// Code is the HTTP status code associated with the error.
Code int `json:"code"`
// Cause is the underlying error that caused this authentication error.
Cause error `json:"-"`
}
AuthenticationError represents authentication-related errors.
func NewAuthenticationError ¶
func NewAuthenticationError(baseErr *AuthenticationError, cause error) *AuthenticationError
NewAuthenticationError creates a new authentication error with a cause based on a base error.
func (*AuthenticationError) Error ¶
func (e *AuthenticationError) Error() string
Error returns a string representation of the authentication error.
type OAuthError ¶
type OAuthError struct {
// Code is the OAuth error code.
Code string `json:"error"`
// Description is a human-readable description of the error.
Description string `json:"error_description,omitempty"`
// URI is a URI identifying a human-readable web page with information about the error.
URI string `json:"error_uri,omitempty"`
// StatusCode is the HTTP status code associated with the error.
StatusCode int `json:"-"`
}
OAuthError represents an OAuth-specific error.
func NewOAuthError ¶
func NewOAuthError(code, description string, statusCode int) *OAuthError
NewOAuthError creates a new OAuth error with the specified code, description, and status code.
func (*OAuthError) Error ¶
func (e *OAuthError) Error() string
Error returns a string representation of the OAuth error.
type OAuthResult ¶
type OAuthResult struct {
// Code is the authorization code received from the OAuth provider.
Code string
// State is the state parameter used to prevent CSRF attacks.
State string
// Error contains any error message if the OAuth flow failed.
Error string
}
OAuthResult contains the result of the OAuth callback. It holds either the authorization code and state for successful authentication or an error message if the authentication failed.
type OAuthServer ¶
type OAuthServer struct {
// contains filtered or unexported fields
}
OAuthServer handles the local HTTP server for OAuth callbacks. It listens for the authorization code response from the OAuth provider and captures the necessary parameters to complete the authentication flow.
func NewOAuthServer ¶
func NewOAuthServer(port int, cfg ServerConfig) *OAuthServer
NewOAuthServer creates a new OAuth callback server with the given port and configuration.
func (*OAuthServer) IsRunning ¶
func (s *OAuthServer) IsRunning() bool
IsRunning returns whether the server is currently running.
func (*OAuthServer) Start ¶
func (s *OAuthServer) Start() error
Start starts the OAuth callback server.
func (*OAuthServer) Stop ¶
func (s *OAuthServer) Stop(ctx context.Context) error
Stop gracefully stops the OAuth callback server.
func (*OAuthServer) WaitForCallback ¶
func (s *OAuthServer) WaitForCallback(timeout time.Duration) (*OAuthResult, error)
WaitForCallback waits for the OAuth callback with a timeout.
type ServerConfig ¶
type ServerConfig struct {
// CallbackPath is the HTTP path for the OAuth callback endpoint (e.g. "/callback").
CallbackPath string
// DefaultPlatformURL is the fallback URL shown on the success page (e.g. "https://console.anthropic.com/").
DefaultPlatformURL string
// ProviderName is the display name used in HTML templates (e.g. "Claude", "Codex").
ProviderName string
}
ServerConfig holds provider-specific settings for the OAuth callback server.