Class: PetstoreApiClient::Authentication::ApiKey

Inherits:
Base
  • Object
show all
Defined in:
lib/petstore_api_client/authentication/api_key.rb

Overview

API Key authentication strategy Adds api_key header to requests

The Swagger Petstore API accepts api_key in the request header. According to official docs, the special key is “special-key”

Security best practices:

  • API keys are validated before use

  • Warnings issued for insecure (HTTP) connections

  • Supports loading from environment variables

Examples:

Direct instantiation

auth = ApiKey.new("special-key")

From environment variable

ENV['PETSTORE_API_KEY'] = "special-key"
auth = ApiKey.from_env

Constant Summary collapse

HEADER_NAME =

Header name for API key According to Swagger Petstore spec: petstore.swagger.io

"api_key"
ENV_VAR_NAME =

Environment variable name for API key

"PETSTORE_API_KEY"
MIN_KEY_LENGTH =

Minimum length for API key (security validation)

3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#type

Constructor Details

#initialize(api_key = nil) ⇒ ApiKey

Initialize API key authenticator

rubocop:disable Lint/MissingSuper

Parameters:

  • api_key (String, nil) (defaults to: nil)

    The API key to use

Raises:



42
43
44
45
# File 'lib/petstore_api_client/authentication/api_key.rb', line 42

def initialize(api_key = nil)
  @api_key = api_key&.to_s&.strip
  validate! if configured?
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



35
36
37
# File 'lib/petstore_api_client/authentication/api_key.rb', line 35

def api_key
  @api_key
end

Class Method Details

.from_envApiKey

Create authenticator from environment variable

Returns:

  • (ApiKey)

    New authenticator instance



51
52
53
# File 'lib/petstore_api_client/authentication/api_key.rb', line 51

def self.from_env
  new(ENV.fetch(ENV_VAR_NAME, nil))
end

Instance Method Details

#apply(env) ⇒ void

This method returns an undefined value.

Apply API key authentication to request Adds api_key header to the request

Parameters:

  • env (Faraday::Env)

    The request environment



60
61
62
63
64
65
66
67
68
# File 'lib/petstore_api_client/authentication/api_key.rb', line 60

def apply(env)
  return unless configured?

  # Warn if sending API key over insecure connection (HTTP)
  warn_if_insecure!(env)

  # Add API key header
  env.request_headers[HEADER_NAME] = @api_key
end

#configured?Boolean

Check if API key is configured

Returns:

  • (Boolean)


73
74
75
# File 'lib/petstore_api_client/authentication/api_key.rb', line 73

def configured?
  !@api_key.nil? && !@api_key.empty?
end

#inspectString Also known as: to_s

String representation (masks API key for security)

Returns:

  • (String)


80
81
82
83
84
85
86
87
# File 'lib/petstore_api_client/authentication/api_key.rb', line 80

def inspect
  return unconfigured_inspect unless configured?

  # Use base class method to mask credential (show first 4 chars)
  masked = mask_credential(@api_key, 4)

  "#<#{self.class.name} api_key=#{masked}>"
end