Class: Strava::OAuth::Client

Inherits:
Web::Client
  • Object
show all
Defined in:
lib/strava/oauth/client.rb

Overview

OAuth client for Strava authentication.

This client handles the OAuth 2.0 authentication flow with Strava, including:

  • Generating authorization URLs
  • Exchanging authorization codes for access tokens
  • Refreshing expired access tokens

Examples:

Basic OAuth flow

client = Strava::OAuth::Client.new(
  client_id: "your_client_id",
  client_secret: "your_client_secret"
)

# Step 1: Get authorization URL
url = client.authorize_url(
  redirect_uri: 'http://localhost:3000/callback',
  scope: 'read,activity:read_all'
)
# Redirect user to this URL

# Step 2: Exchange code for token
token = client.oauth_token(code: params[:code])
# Store token.access_token and token.refresh_token

# Step 3: Refresh token when expired
new_token = client.oauth_token(
  refresh_token: saved_refresh_token,
  grant_type: 'refresh_token'
)

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Initialize a new OAuth client.

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :client_id (String)

    Strava application client ID (required)

  • :client_secret (String)

    Strava application client secret (required)

  • :endpoint (String)

    OAuth endpoint URL (defaults to https://www.strava.com/oauth)



49
50
51
52
53
54
# File 'lib/strava/oauth/client.rb', line 49

def initialize(options = {})
  Strava::OAuth::Config::ATTRIBUTES.each do |key|
    send("#{key}=", options[key] || Strava::OAuth.config.send(key))
  end
  super
end

Class Method Details

.configModule

Returns the current OAuth client configuration.

Returns:

  • (Module)

    The Config module



154
155
156
# File 'lib/strava/oauth/client.rb', line 154

def config
  Config
end

.configure {|Config| ... } ⇒ Module

Configure the OAuth client with a block.

Examples:

Strava::OAuth::Client.configure do |config|
  config.client_id = ENV['STRAVA_CLIENT_ID']
  config.client_secret = ENV['STRAVA_CLIENT_SECRET']
end

Yields:

  • (Config)

    Yields the configuration module for setup

Returns:

  • (Module)

    The Config module



145
146
147
# File 'lib/strava/oauth/client.rb', line 145

def configure
  block_given? ? yield(Config) : Config
end

Instance Method Details

#authorize_url(options = {}) ⇒ String

Generate the authorization URL for OAuth flow.

Creates a URL to redirect users to for Strava authorization. After the user authorizes your application, they will be redirected back to your redirect_uri with an authorization code.

Examples:

Generate authorization URL

url = client.authorize_url(
  redirect_uri: 'https://myapp.com/callback',
  scope: 'read,activity:read_all,activity:write',
  state: 'random_state_value'
)

Parameters:

  • options (Hash) (defaults to: {})

    Authorization parameters

Options Hash (options):

  • :redirect_uri (String)

    URL where user is redirected after authorization (default: http://localhost)

  • :response_type (String)

    Must be 'code' (default: 'code')

  • :approval_prompt (String)

    'force' to always show approval page, 'auto' to auto-approve (default: 'auto')

  • :scope (String)

    Comma-delimited string of permissions (default: 'read') Available scopes: read, read_all, profile:read_all, profile:write, activity:read, activity:read_all, activity:write

  • :state (String)

    Optional value returned in redirect URI for CSRF protection

Returns:

  • (String)

    Full authorization URL

See Also:



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/strava/oauth/client.rb', line 82

def authorize_url(options = {})
  query = options.merge(
    client_id: client_id || raise(ArgumentError, 'Missing Strava client id.'),
    response_type: options[:response_type] || 'code',
    redirect_uri: options[:redirect_uri] || 'http://localhost',
    approval_prompt: options[:approval_prompt] || 'auto',
    scope: options[:scope] || 'read'
  )

  [endpoint, "authorize?#{query.to_query}"].join('/')
end

#oauth_token(options = {}) ⇒ Strava::Models::Token

Exchange authorization code for access token or refresh an expired token.

This method handles two OAuth flows:

  1. Initial token exchange: Exchange authorization code for access/refresh tokens
  2. Token refresh: Use refresh_token to get new access/refresh tokens

Examples:

Exchange authorization code for token

token = client.oauth_token(code: 'authorization_code_from_redirect')
access_token = token.access_token
refresh_token = token.refresh_token
expires_at = token.expires_at

Refresh an expired token

new_token = client.oauth_token(
  refresh_token: 'saved_refresh_token',
  grant_type: 'refresh_token'
)

Parameters:

  • options (Hash) (defaults to: {})

    Token request parameters

Options Hash (options):

  • :code (String)

    Authorization code from redirect (for initial exchange)

  • :refresh_token (String)

    Refresh token (for token refresh)

  • :grant_type (String)

    Grant type: 'authorization_code' (default) or 'refresh_token'

Returns:

See Also:



122
123
124
125
126
127
128
129
130
# File 'lib/strava/oauth/client.rb', line 122

def oauth_token(options = {})
  query = options.merge(
    client_id: client_id || raise(ArgumentError, 'Missing Strava client id.'),
    client_secret: client_secret || raise(ArgumentError, 'Missing Strava client secret.'),
    grant_type: options[:grant_type] || 'authorization_code'
  )

  Strava::Models::Token.new(post('token', query))
end