Skip to content

Commit

Permalink
Unify Faraday config under local module instead of global prepend (#9757
Browse files Browse the repository at this point in the history
)

* Unify Faraday config under local module instead of global prepend

* Let Vault rely on Faraday's comfortable JSON parsing
  • Loading branch information
gregorbg authored Aug 8, 2024
1 parent a6fbd18 commit 0c40fb3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 48 deletions.
10 changes: 4 additions & 6 deletions app/controllers/api/internal/v1/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ def validate_wca_token
# Renew our token if it has expired or is close to expiring
Vault.auth_token.renew_self if vault_token_data[:ttl] < 300

client = Faraday.new(url: EnvConfig.VAULT_ADDR)
client = Faraday.new(url: EnvConfig.VAULT_ADDR, &FaradayConfig)

# Make the POST request to the introspect endpoint
response = client.post do |req|
req.url '/v1/identity/oidc/introspect'
response = client.post('/v1/identity/oidc/introspect') do |req|
req.headers['X-Vault-Token'] = vault_token_data[:id]
req.body = { token: service_token }.to_json
req.body = { token: service_token }
end
if response.success?
result = JSON.parse(response.body)
render json: { error: "Authentication Expired or Token Invalid" }, status: :forbidden unless result["active"]
render json: { error: "Authentication Expired or Token Invalid" }, status: :forbidden unless response.body["active"]
else
raise "Introspection failed with the following error: #{response.status}, #{response.body}"
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def staging_oauth_login
'Authorization' => "Bearer #{access_token}",
'Content-Type' => 'application/json',
},
&FaradayConfig
)

results = connection.get("/api/v0/me").body
Expand Down
20 changes: 0 additions & 20 deletions config/initializers/faraday_default_options.rb

This file was deleted.

22 changes: 22 additions & 0 deletions lib/faraday_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module FaradayConfig
def self.setup_connection(builder)
# Sets headers and parses jsons automatically
builder.request :json
builder.response :json

# Raises an error on 4xx and 5xx responses.
builder.response :raise_error

# Logs requests and responses.
# By default, it only logs the request method and URL, and the request/response headers.
builder.response :logger, ::Logger.new($stdout), bodies: true if Rails.env.development?
end

# This makes it so that we can just pass &FaradayConfig
# as an arg directly to the last parameter of the Faraday constructor
def self.to_proc
self.method(:setup_connection).to_proc
end
end
12 changes: 2 additions & 10 deletions lib/microservices/registrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,8 @@ def self.registration_connection
Faraday.new(
url: base_url,
headers: { Microservices::Auth::MICROSERVICE_AUTH_HEADER => Microservices::Auth.get_wca_token },
) do |builder|
# Sets headers and parses jsons automatically
builder.request :json
builder.response :json
# Raises an error on 4xx and 5xx responses.
builder.response :raise_error
# Logs requests and responses.
# By default, it only logs the request method and URL, and the request/response headers.
builder.response :logger
end
&FaradayConfig
)
end

def self.registrations_by_user(user_id, cache: true)
Expand Down
14 changes: 2 additions & 12 deletions lib/paypal_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,8 @@ def self.issue_refund(merchant_id, capture_id, amount_iso, currency_code)
'Content-Type' => 'application/json',
'Prefer' => 'return=representation', # forces PayPal to return everything they known on every request
},
) do |builder|
# Sets headers and parses jsons automatically
builder.request :json
builder.response :json

# Raises an error on 4xx and 5xx responses.
builder.response :raise_error

# Logs requests and responses.
# By default, it only logs the request method and URL, and the request/response headers.
builder.response :logger, ::Logger.new($stdout), bodies: true if Rails.env.development?
end
&FaradayConfig
)
end

private_class_method def self.generate_access_token
Expand Down

0 comments on commit 0c40fb3

Please sign in to comment.