Class: Booqable::Middleware::Auth::ApiKey

Inherits:
Base
  • Object
show all
Defined in:
lib/booqable/middleware/auth/api_key.rb

Overview

Faraday middleware for API key authentication

This middleware adds Bearer token authentication to HTTP requests using a pre-configured API key. The API key is added to the Authorization header for each request unless already present.

For more info see: developers.booqable.com/#authentication-access-token

Examples:

Adding to Faraday middleware stack

builder.use Booqable::Middleware::Auth::ApiKey, api_key: "your_api_key"

Constant Summary collapse

TOKEN_ENDPOINT =

OAuth token endpoint (legacy reference)

"/api/boomerang/oauth/token"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ApiKey

Initialize the API key authentication middleware

Parameters:

  • app (#call)

    The next middleware in the Faraday stack

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

    Configuration options

Options Hash (options):

  • :api_key (String)

    The API key for authentication

Raises:

  • (KeyError)

    If :api_key option is not provided



24
25
26
27
28
# File 'lib/booqable/middleware/auth/api_key.rb', line 24

def initialize(app, options = {})
  super(app)

  @api_key = options.fetch(:api_key)
end

Instance Method Details

#call(env) ⇒ Faraday::Response

Process the HTTP request and add API key authentication

Adds the API key as a Bearer token in the Authorization header if no authorization header is already present, then passes the request to the next middleware in the stack.

Parameters:

  • env (Faraday::Env)

    The request environment

Returns:

  • (Faraday::Response)

    The response from the next middleware



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

def call(env)
  env.request_headers["Authorization"] ||= "Bearer #{@api_key}"

  @app.call(env)
end