Class: Payabli::Internal::RoutingAuthProvider
- Inherits:
-
Object
- Object
- Payabli::Internal::RoutingAuthProvider
- Defined in:
- lib/payabli/internal/routing_auth_provider.rb
Instance Method Summary collapse
-
#auth_headers ⇒ Hash[String, String]
Endpoint-security applies auth per-endpoint, so no auth headers are added to every request.
-
#auth_headers_for_endpoint(security:) ⇒ Hash[String, String]
Returns the auth headers for a single endpoint given its static security requirements.
- #initialize(api_key: nil, oauth_provider: nil) ⇒ void constructor
Constructor Details
#initialize(api_key: nil, oauth_provider: nil) ⇒ void
10 11 12 13 |
# File 'lib/payabli/internal/routing_auth_provider.rb', line 10 def initialize(api_key: nil, oauth_provider: nil) @api_key = api_key @oauth_provider = oauth_provider end |
Instance Method Details
#auth_headers ⇒ Hash[String, String]
Endpoint-security applies auth per-endpoint, so no auth headers are added to every request.
18 19 20 |
# File 'lib/payabli/internal/routing_auth_provider.rb', line 18 def auth_headers {} end |
#auth_headers_for_endpoint(security:) ⇒ Hash[String, String]
Returns the auth headers for a single endpoint given its static security requirements. Builds only the headers for the first requirement whose schemes all have credentials available (OR across the list, AND within a requirement). Raises when none is satisfiable.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/payabli/internal/routing_auth_provider.rb', line 29 def auth_headers_for_endpoint(security:) return {} if security.nil? || security.empty? available_auth_headers = {} available_auth_headers["APIKeyAuth"] = { "requestToken" => @api_key.to_s } unless @api_key.nil? available_auth_headers["BearerAuth"] = @oauth_provider.auth_headers unless @oauth_provider.nil? security.each do |requirement| next unless requirement.keys.all? { |scheme_key| available_auth_headers.key?(scheme_key) } combined_headers = {} requirement.each_key { |scheme_key| combined_headers.merge!(available_auth_headers[scheme_key]) } return combined_headers end missing_schemes = security.map do |requirement| requirement.keys.reject { |scheme_key| available_auth_headers.key?(scheme_key) }.join(" AND ") end raise ArgumentError, "No authentication credentials provided that satisfy the endpoint's security requirements. Please provide credentials for: #{missing_schemes.join(" OR ")}" end |