11class Api ::V0 ::BaseController < ApplicationController
2+ before_action :generate_api_request_id
23 before_action :verify_client_name_and_signature
3- rescue_from MefService :: RetryableError , with : :retryable_mef_error
4+
45 rescue_from ActionController ::ParameterMissing , with : :showable_error
56 rescue_from Aws ::SecretsManager ::Errors ::ServiceError , with : :aws_error
67 rescue_from JWT ::VerificationError , with : :jwt_error
78
8- def retryable_mef_error ( exception )
9- Rails . logger . error ( "Encountered retryable error while contacting MeF: #{ exception } " )
10- render json : "Error contacting MeF, please try again" , status : :bad_gateway
9+ # A random API Request ID is returned to the client synchronously and also included in all webhook requests.
10+ # This is so that the client can correspond a webhook callback to the originating API request.
11+ # This pattern requires that clients persist records of all API calls that so that they can look them up by request ID
12+ # upon receiving a webhook callback and take appropriate steps to resolve whatever action initiated the API request.
13+ attr_reader :api_request_id
14+ def generate_api_request_id
15+ @api_request_id = SecureRandom . uuid
1116 end
1217
1318 def showable_error ( exception )
@@ -29,7 +34,7 @@ def verify_client_name_and_signature
2934 authorization_header = request . headers [ "HTTP_AUTHORIZATION" ]
3035 token = JWT ::EncodedToken . new ( authorization_header . delete_prefix ( "Bearer " ) )
3136
32- client_credentials = get_api_client_mef_credentials
37+ client_credentials = MefService . get_mef_credentials ( api_client_name )
3338 client_public_key_base64 = client_credentials [ :efiler_api_public_key ]
3439 client_public_key = OpenSSL ::PKey ::RSA . new ( Base64 . decode64 ( client_public_key_base64 ) )
3540 token . verify_signature! ( algorithm : "RS256" , key : client_public_key )
@@ -40,11 +45,4 @@ def api_client_name
4045 token = JWT ::EncodedToken . new ( authorization_header . delete_prefix ( "Bearer " ) )
4146 token . unverified_payload [ "iss" ]
4247 end
43-
44- def get_api_client_mef_credentials
45- aws_client = Aws ::SecretsManager ::Client . new
46- environment = Rails . env . production? ? "production" : "demo"
47- response = aws_client . get_secret_value ( secret_id : "efiler-api/#{ environment } /efiler-api-client-credentials/#{ api_client_name } " )
48- JSON . parse ( response . secret_string ) . transform_keys { |k | k . to_sym }
49- end
5048end
0 commit comments