Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MHV-58591 MHV-57739 Authentication for MHV Classic radiology endpoint #17381

Merged
merged 15 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
MHV-57739 Add radiology controller and backend connection
  • Loading branch information
mmoyer-va committed May 20, 2024
commit cf74aeecde3735048b64ce8ab9fe4701cf8c87eb
15 changes: 15 additions & 0 deletions config/locales/exceptions.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,21 @@ en:
code: "MEDICALRECORDS_404"
detail: "The resource could not be found"
status: 404
BBINTERNAL_401:
<<: *external_defaults
code: "BBINTERNAL_401"
detail: "Authentication failed on the upstream server"
status: 400
BBINTERNAL_403:
<<: *external_defaults
code: "BBINTERNAL_403"
detail: "This user is forbidden from performing requests on the upstream server"
status: 403
BBINTERNAL_404:
<<: *external_defaults
code: "BBINTERNAL_404"
detail: "The resource could not be found"
status: 404
HCA422:
<<: *external_defaults
code: 'HCA422'
Expand Down
3 changes: 3 additions & 0 deletions config/redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ development: &defaults
medical_records_store:
namespace: mr-service
each_ttl: 1200
bb_internal_store:
namespace: bb-internal-service
each_ttl: 1200
mhv_mr_fhir_session_lock:
namespace: mhv-mr-fhir-session-lock
each_ttl: 10
Expand Down
59 changes: 59 additions & 0 deletions lib/medical_records/bb_internal/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require 'common/client/base'
require 'common/client/concerns/mhv_session_based_client'
require 'medical_records/bb_internal/client_session'
require 'medical_records/bb_internal/configuration'

module BBInternal
##
# Core class responsible for PHR Manager API interface operations
#
class Client < Common::Client::Base
include Common::Client::Concerns::MHVSessionBasedClient

configuration BBInternal::Configuration
client_session BBInternal::ClientSession

def get_radiology
response = perform(:get, "bluebutton/radiology/phr/#{session.patient_id}", nil, token_headers)
response.body
end

private

##
# Override MHVSessionBasedClient's method so we can get the patientId and store it as well.
#
def get_session
new_session = @session.class.new(user_id: 11_383_839,
patient_id: 11_383_893,
expires_at: 'Wed, 15 Jan 2025 00:00:00 GMT',
token: 'ENC(MA0ECJh1RjEgZFMhAgEQC4nF8QKGKGmZuYg7kVN8CGTImSTyeRVyXIeUOtSUP4PoUkdGKwuDnAAn)')

# # Call MHVSessionBasedClient.get_session
# session = super

# # Supplement session with patientId
# patient_id = get_patient_id
# session.patient_id = patient_id

new_session.save
new_session
end

def get_patient_id
response = perform(:get, "usermgmt/patient/uid/#{@session.user_id}", nil, token_headers)
response.body
11_383_893
end

##
# Override MHVSessionBasedClient's method, because we need more control over the path.
#
def get_session_tagged
response = perform(:get, 'usermgmt/auth/session', nil, auth_headers)
response.body
end
end
end
14 changes: 14 additions & 0 deletions lib/medical_records/bb_internal/client_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'common/client/session'

module BBInternal
class ClientSession < Common::Client::Session
# attribute :icn, String
attribute :patient_id, String

redis_store REDIS_CONFIG[:bb_internal_store][:namespace]
redis_ttl 3600
redis_key :user_id
end
end
65 changes: 65 additions & 0 deletions lib/medical_records/bb_internal/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require 'common/client/configuration/rest'
require 'common/client/middleware/request/camelcase'
require 'common/client/middleware/request/multipart_request'
require 'common/client/middleware/response/json_parser'
require 'common/client/middleware/response/raise_error'
require 'common/client/middleware/response/mhv_errors'
require 'common/client/middleware/response/snakecase'
require 'faraday/multipart'
require 'sm/middleware/response/sm_parser'

module BBInternal
##
# HTTP client configuration for {PHRMgr::Client}
#
class Configuration < Common::Client::Configuration::REST
##
# BB Internal uses the same app token as Rx.
# @return [String] Client token set in `settings.yml` via credstash
#
def app_token
Settings.mhv.rx.app_token
end

##
# BB Internal uses the same domain as Medical Records FHIR.
# @return [String] Base path for dependent URLs
#
def base_path
"#{Settings.mhv.medical_records.host}/mhvapi/v1/"
end

##
# @return [String] Service name to use in breakers and metrics
#
def service_name
'BBInternal'
end

##
# Creates a connection
#
# @return [Faraday::Connection] a Faraday connection instance
#
def connection
Faraday.new(base_path, headers: base_request_headers, request: request_options) do |conn|
conn.use :breakers
conn.request :multipart_request
conn.request :multipart
conn.request :json

# Uncomment this if you want curl command equivalent or response output to log
# conn.request(:curl, ::Logger.new(STDOUT), :warn) unless Rails.env.production?
# conn.response(:logger, ::Logger.new(STDOUT), bodies: true) unless Rails.env.production?

conn.response :raise_error, error_prefix: service_name
conn.response :mhv_errors
conn.response :mhv_xml_html_errors
conn.response :json_parser
conn.adapter Faraday.default_adapter
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MrController < ApplicationController
service_tag 'mhv-medical-records'

# skip_before_action :authenticate
before_action :authenticate_bb

rescue_from ::MedicalRecords::PatientNotFound do |_exception|
render body: nil, status: :accepted
Expand All @@ -26,6 +27,14 @@ def phrmgr_client
@phrmgr_client ||= PHRMgr::Client.new(current_user.icn)
end

def bb_client
@bb_client ||= BBInternal::Client.new(current_user.mhv_correlation_id)
end

def authenticate_bb
bb_client.authenticate
end

def authorize
raise_access_denied unless current_user.authorize(:mhv_medical_records, :access?)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module MyHealth
module V1
module MedicalRecords
class RadiologyController < MrController
def index
resource = bb_client.get_radiology
render json: resource.to_json
end
end
end
end
end
11 changes: 7 additions & 4 deletions modules/my_health/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
MyHealth::Engine.routes.draw do
namespace :v1 do
scope :medical_records do
resources :session, only: %i[create], controller: 'medical_records/mr_session',
defaults: { format: :json } do
get :status, on: :collection
end
resources :vaccines, only: %i[index show], defaults: { format: :json } do
get :pdf, on: :collection
end
Expand All @@ -17,6 +13,13 @@
resources :conditions, only: %i[index show], defaults: { format: :json }
end

namespace :medical_records do
resources :session, only: %i[create], controller: 'mr_session', defaults: { format: :json } do
get :status, on: :collection
end
resources :radiology, only: %i[index], defaults: { format: :json }
end

scope :messaging do
resources :triage_teams, only: [:index], defaults: { format: :json }, path: 'recipients'
resources :all_triage_teams, only: [:index], defaults: { format: :json }, path: 'allrecipients'
Expand Down
29 changes: 29 additions & 0 deletions spec/lib/medical_records/bb_internal/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'rails_helper'
require 'medical_records/bb_internal/client'
require 'stringio'

describe BBInternal::Client do
before(:all) do
@client ||= begin
client = BBInternal::Client.new(session: { user_id: '11383893' })
client.authenticate
client
end
end

let(:client) { @client }

describe 'Getting radiology records' do
it 'gets the records' do
VCR.use_cassette 'mr_client/bb_internal/get_radiology' do
radiology_results = client.get_radiology
expect(radiology_results).to be_an(Array)
result = radiology_results[0]
expect(result).to be_a(Hash)
expect(result).to have_key('procedureName')
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading