Class: Basecamp::Oauth::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/basecamp/oauth/exchange.rb

Overview

Handles OAuth 2 token exchange and refresh operations.

Instance Method Summary collapse

Constructor Details

#initialize(http_client: nil, timeout: 30) ⇒ Exchange

Returns a new instance of Exchange.

Parameters:

  • http_client (Faraday::Connection, nil) (defaults to: nil)

    HTTP client (uses default if nil)

  • timeout (Integer) (defaults to: 30)

    Request timeout in seconds (default: 30)



13
14
15
# File 'lib/basecamp/oauth/exchange.rb', line 13

def initialize(http_client: nil, timeout: 30)
  @http_client = http_client || build_default_client(timeout)
end

Instance Method Details

#exchange(request) ⇒ Token

Exchanges an authorization code for access and refresh tokens.

Supports both standard OAuth 2 and Basecamp's Launchpad legacy format. Use use_legacy_format: true for Launchpad compatibility.

Examples:

Standard OAuth 2

token = exchange.exchange(ExchangeRequest.new(
  token_endpoint: config.token_endpoint,
  code: "auth_code_from_callback",
  redirect_uri: "https://myapp.com/callback",
  client_id: "my_client_id",
  client_secret: "my_client_secret"
))

Launchpad legacy format

token = exchange.exchange(ExchangeRequest.new(
  token_endpoint: config.token_endpoint,
  code: "auth_code",
  redirect_uri: "https://myapp.com/callback",
  client_id: "my_client_id",
  client_secret: "my_client_secret",
  use_legacy_format: true
))

Parameters:

Returns:

  • (Token)

    The token response

Raises:

  • (OauthError)

    on validation, network, or authentication errors



44
45
46
47
48
49
# File 'lib/basecamp/oauth/exchange.rb', line 44

def exchange(request)
  validate_exchange_request!(request)

  params = build_exchange_params(request)
  do_token_request(request.token_endpoint, params)
end

#refresh(request) ⇒ Token

Refreshes an access token using a refresh token.

Supports both standard OAuth 2 and Basecamp's Launchpad legacy format. Use use_legacy_format: true for Launchpad compatibility.

Examples:

Standard OAuth 2

new_token = exchange.refresh(RefreshRequest.new(
  token_endpoint: config.token_endpoint,
  refresh_token: old_token.refresh_token,
  client_id: "my_client_id",
  client_secret: "my_client_secret"
))

Launchpad legacy format

new_token = exchange.refresh(RefreshRequest.new(
  token_endpoint: config.token_endpoint,
  refresh_token: old_token.refresh_token,
  use_legacy_format: true
))

Parameters:

Returns:

  • (Token)

    The new token response

Raises:

  • (OauthError)

    on validation, network, or authentication errors



74
75
76
77
78
79
# File 'lib/basecamp/oauth/exchange.rb', line 74

def refresh(request)
  validate_refresh_request!(request)

  params = build_refresh_params(request)
  do_token_request(request.token_endpoint, params)
end