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.
Instance Method Summary collapse
-
#api_key_authenticated? ⇒ Boolean
Check if API key authentication is configured.
-
#authenticate_with_code(code) ⇒ void
Complete OAuth authentication flow with authorization code.
-
#inject_auth_middleware(builder) ⇒ void
private
Inject appropriate authentication middleware into the request stack.
-
#oauth_authenticated? ⇒ Boolean
Check if OAuth authentication is configured.
-
#oauth_client ⇒ OAuthClient?
Get or create the OAuth client.
-
#single_use_token_authenticated? ⇒ Boolean
Check if single-use token authentication is configured.
Instance Method Details
#api_key_authenticated? ⇒ Boolean
Check if API key authentication is configured
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.
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.
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
96 97 98 |
# File 'lib/booqable/auth.rb', line 96 def oauth_authenticated? !!@client_id && !!@client_secret end |
#oauth_client ⇒ OAuthClient?
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.
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
110 111 112 |
# File 'lib/booqable/auth.rb', line 110 def single_use_token_authenticated? !!@single_use_token end |