Module: Booqable::Auth

Included in:
Client
Defined in:
lib/booqable/auth.rb

Overview

Authentication methods for Client

Provides authentication support for multiple methods including OAuth2, API keys, and single-use tokens. The module automatically detects which authentication method to use based on the configured credentials.

Examples:

OAuth authentication

client = Booqable::Client.new(
  client_id: "your_client_id",
  client_secret: "your_client_secret"
)
client.authenticate_with_code("auth_code_from_callback")

API key authentication

client = Booqable::Client.new(api_key: "your_api_key")
# Authentication is automatic with API requests

Single-use token authentication

client = Booqable::Client.new(
  single_use_token: "your_token",
  single_use_token_algorithm: "HS256",
  single_use_token_secret: "your_secret"
)

Instance Method Summary collapse

Instance Method Details

#api_key_authenticated?Boolean

Check if API key authentication is configured

Returns:

  • (Boolean)

    true if api_key is present



103
104
105
# File 'lib/booqable/auth.rb', line 103

def api_key_authenticated?
  !!@api_key
end

#authenticate_with_code(code) ⇒ void

This method returns an undefined value.

Complete OAuth authentication flow with authorization code

Exchanges an authorization code for an access token and stores it using the configured write_token proc. This method should be called after the user has been redirected back from the OAuth provider.

Examples:

# In your OAuth callback handler
client.authenticate_with_code(params[:code])

Parameters:

  • code (String)

    Authorization code from OAuth callback

Raises:

  • (OAuth2::Error)

    If the authorization code is invalid or expired



39
40
41
42
# File 'lib/booqable/auth.rb', line 39

def authenticate_with_code(code)
  token = oauth_client.get_token_from_code(code)
  @write_token.call(token.to_hash)
end

#inject_auth_middleware(builder) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Inject appropriate authentication middleware into the request stack

Automatically detects which authentication method is configured and injects the corresponding middleware. Multiple authentication methods can be configured, with OAuth taking precedence over API keys.

Parameters:

  • builder (Faraday::Builder)

    The middleware builder to configure



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/booqable/auth.rb', line 53

def inject_auth_middleware(builder)
  builder.use Booqable::Middleware::Auth::OAuth, {
    client_id: client_id,
    client_secret: client_secret,
    api_endpoint: api_endpoint,
    redirect_uri: redirect_uri,
    read_token: read_token,
    write_token: write_token
  } if oauth_authenticated?

  builder.use Booqable::Middleware::Auth::ApiKey, {
    api_key: api_key
  } if api_key_authenticated?

  builder.use Booqable::Middleware::Auth::SingleUse, {
    single_use_token: single_use_token,
    single_use_token_algorithm: single_use_token_algorithm,
    single_use_token_private_key: single_use_token_private_key || single_use_token_secret,
    single_use_token_expiration_period: single_use_token_expiration_period,
    single_use_token_company_id: single_use_token_company_id,
    single_use_token_user_id: single_use_token_user_id,
    api_endpoint: api_endpoint
  } if single_use_token_authenticated?
end

#oauth_authenticated?Boolean

Check if OAuth authentication is configured

Returns:

  • (Boolean)

    true if both client_id and client_secret are present



96
97
98
# File 'lib/booqable/auth.rb', line 96

def oauth_authenticated?
  !!@client_id && !!@client_secret
end

#oauth_clientOAuthClient?

Get or create the OAuth client

Returns a memoized OAuth client instance configured with the current client credentials. Returns nil if OAuth is not configured.

Returns:

  • (OAuthClient, nil)

    OAuth client instance or nil if not configured



84
85
86
87
88
89
90
91
# File 'lib/booqable/auth.rb', line 84

def oauth_client
  @oauth_client ||= OAuthClient.new(
    api_endpoint: api_endpoint,
    client_id: @client_id,
    client_secret: @client_secret,
    redirect_uri: @redirect_uri
  ) if oauth_authenticated?
end

#single_use_token_authenticated?Boolean

Check if single-use token authentication is configured

Returns:

  • (Boolean)

    true if single_use_token is present



110
111
112
# File 'lib/booqable/auth.rb', line 110

def single_use_token_authenticated?
  !!@single_use_token
end