Class: PetstoreApiClient::Authentication::ApiKey
- 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
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
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Class Method Summary collapse
-
.from_env ⇒ ApiKey
Create authenticator from environment variable.
Instance Method Summary collapse
-
#apply(env) ⇒ void
Apply API key authentication to request Adds api_key header to the request.
-
#configured? ⇒ Boolean
Check if API key is configured.
-
#initialize(api_key = nil) ⇒ ApiKey
constructor
Initialize API key authenticator.
-
#inspect ⇒ String
(also: #to_s)
String representation (masks API key for security).
Methods inherited from Base
Constructor Details
#initialize(api_key = nil) ⇒ ApiKey
Initialize API key authenticator
rubocop:disable Lint/MissingSuper
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_key ⇒ Object (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_env ⇒ ApiKey
Create authenticator from environment variable
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
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
73 74 75 |
# File 'lib/petstore_api_client/authentication/api_key.rb', line 73 def configured? !@api_key.nil? && !@api_key.empty? end |
#inspect ⇒ String Also known as: to_s
String representation (masks API key for security)
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 |