oauth

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeSessionIDFromState

func DecodeSessionIDFromState(state string) (string, error)

DecodeSessionIDFromState extracts the session ID from an OAuth state parameter.

This function is used by OAuth callback handlers to decode session IDs. When the OAuth provider redirects back to our callback endpoint, they include the state parameter we originally sent. This function reverses the encoding done by GenerateStateWithSessionID to extract the session ID, allowing us to route the authorization code back to the correct agent session that initiated the OAuth flow.

This is the critical piece that bridges the browser-based OAuth callback back to the specific runtime session that needs the authorization.

func GenerateStateWithSessionID

func GenerateStateWithSessionID(sessionID string) (string, error)

GenerateStateWithSessionID generates an OAuth state parameter that encodes the session ID.

OAuth State Parameter Design:

When an agent needs OAuth authorization, the flow works like this: 1. Agent runtime detects OAuth is needed and pauses execution 2. We generate authorization URL with state parameter containing the session ID 3. User's browser is redirected to the OAuth provider for authorization 4. OAuth provider redirects back to our callback URL with the authorization code AND the state 5. Our callback handler receives the state, extracts the session ID from it 6. We can then resume the correct agent session with the authorization code

Without encoding session ID in state, we couldn't match the OAuth callback to the specific agent session that requested authorization, especially in multi-session scenarios.

The state parameter combines: - Session ID: To route the callback back to the correct session - Random bytes: For CSRF protection (traditional OAuth security requirement)

func MayBeOAuthError added in v1.3.6

func MayBeOAuthError(err error) (error, bool)

MayBeOAuthError checks if the given error is an OAuth authorization error and wraps it with server information if available.

This function examines error chains to identify OAuth authorization errors wrapped within agent ToolSetError (or any other future error that might contain a possible OAuth error). When found, it extracts server information and returns a properly wrapped AuthorizationRequiredError.

Returns: - wrapped error and true if this is an OAuth authorization error - original error and false otherwise

func OpenBrowser

func OpenBrowser(url string) error

OpenBrowser opens the default browser to the specified URL

Types

type AuthorizationRequiredError

type AuthorizationRequiredError struct {
	Err        error
	ServerURL  string
	ServerType string
}

AuthorizationRequiredError wraps an OAuth authorization error with server information

func (*AuthorizationRequiredError) Error

func (*AuthorizationRequiredError) Unwrap

func (e *AuthorizationRequiredError) Unwrap() error

type Manager

type Manager interface {
	// HandleAuthorizationFlow handles a single OAuth authorization flow
	HandleAuthorizationFlow(ctx context.Context, sessionID string, oauthErr *AuthorizationRequiredError) error

	// StartAuthorizationFlow signals that user confirmation has been given to start the OAuth flow
	StartAuthorizationFlow(confirmation bool)

	// SendAuthorizationCode sends the OAuth authorization code after user has completed the OAuth flow
	SendAuthorizationCode(code string) error
}

Manager defines the contract for OAuth flow management

func NewManager

func NewManager(emitAuthRequired func(serverURL, serverType, status string)) Manager

NewManager creates a new OAuth manager

type ServerInfoProvider

type ServerInfoProvider interface {
	GetServerInfo() (serverURL, serverType string)
}

ServerInfoProvider interface for toolsets that can provide server information

type StateData

type StateData struct {
	SessionID string `json:"session_id"` // The session ID that initiated the OAuth flow
	Random    string `json:"random"`     // Random component for CSRF protection
}

StateData represents the data encoded in the OAuth state parameter.

In OAuth flows, the state parameter serves dual purposes:

  1. Security: CSRF protection by including random data
  2. Session tracking: When the browser returns from authorization, we need to know which session triggered the OAuth flow to route the callback correctly.

Since OAuth authorization happens in a browser (different context from our runtime), we embed the session ID in the state parameter so we can retrieve it when the authorization server redirects back to us with the authorization code.

Jump to

Keyboard shortcuts

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