Class: Booqable::OAuthClient

Inherits:
Object
  • Object
show all
Defined in:
lib/booqable/oauth_client.rb

Overview

OAuth2 client for Booqable API authentication

Provides OAuth2 authentication flow support for the Booqable API. Handles authorization code exchange for access tokens using the standard OAuth2 authorization code flow.

Examples:

Basic OAuth flow

oauth_client = Booqable::OAuthClient.new(
  base_url: "https://demo.booqable.com",
  client_id: "your_client_id",
  client_secret: "your_client_secret",
  redirect_uri: "https://yourapp.com/callback"
)

# After user authorizes and returns with code
token = oauth_client.get_token_from_code(params[:code])
access_token = token.token

Constant Summary collapse

TOKEN_ENDPOINT =

OAuth2 token endpoint path

"/oauth/token"

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint:, client_id:, client_secret:, redirect_uri: nil) ⇒ OAuthClient

Initialize a new OAuth client

Parameters:

  • base_url (String)

    Base URL of the Booqable instance (e.g., “demo.booqable.com”)

  • client_id (String)

    OAuth2 client identifier

  • client_secret (String)

    OAuth2 client secret

  • redirect_uri (String) (defaults to: nil)

    OAuth2 redirect URI for authorization callback



33
34
35
36
37
38
39
40
41
# File 'lib/booqable/oauth_client.rb', line 33

def initialize(api_endpoint:, client_id:, client_secret:, redirect_uri: nil)
  @client = OAuth2::Client.new(
    client_id,
    client_secret,
    site: api_endpoint,
    token_url: api_endpoint + TOKEN_ENDPOINT,
  )
  @redirect_uri = redirect_uri
end

Instance Method Details

#get_access_token_from_hash(hash) ⇒ OAuth2::AccessToken

Create an access token from a hash.

Parameters:

  • hash (Hash)

    Hash containing access token data.

Returns:

  • (OAuth2::AccessToken)

    Access token object created from the hash.



68
69
70
# File 'lib/booqable/oauth_client.rb', line 68

def get_access_token_from_hash(hash)
  OAuth2::AccessToken.from_hash(@client, hash)
end

#get_token_from_code(code, scope: "full_access") ⇒ OAuth2::AccessToken

Exchange an authorization code for an access token

Exchanges the authorization code received from the OAuth callback for an access token that can be used to make API requests.

Examples:

token = oauth_client.get_token_from_code("auth_code_123")
access_token = token.token
refresh_token = token.refresh_token

Parameters:

  • code (String)

    Authorization code from OAuth callback

  • scope (String) (defaults to: "full_access")

    OAuth scope to request (default: “full_access”)

Returns:

  • (OAuth2::AccessToken)

    Access token object with token and refresh token

Raises:

  • (OAuth2::Error)

    If the authorization code is invalid or expired



57
58
59
60
61
62
# File 'lib/booqable/oauth_client.rb', line 57

def get_token_from_code(code, scope: "full_access")
  @client.auth_code.get_token(code,
                              redirect_uri: @redirect_uri,
                              scope: scope,
                              grant_type: "authorization_code")
end