Module: Common::Client::Concerns::MHVSessionBasedClient

Extended by:
ActiveSupport::Concern
Includes:
MhvLockedSessionClient, SentryLogging
Included in:
BB::Client, BBInternal::Client, MHVLogging::Client, Rx::Client, Rx::MedicationsClient, SM::Client
Defined in:
lib/common/client/concerns/mhv_session_based_client.rb

Overview

Module mixin for overriding session logic when making MHV client connections

Constant Summary

Constants included from MhvLockedSessionClient

Common::Client::Concerns::MhvLockedSessionClient::LOCK_RETRY_DELAY, Common::Client::Concerns::MhvLockedSessionClient::RETRY_ATTEMPTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Methods included from MhvLockedSessionClient

#authenticate, #initialize, #lock_and_get_session, #obtain_redis_lock, #refresh_session, #release_redis_lock

Instance Attribute Details

#sessionHash (readonly)

Returns a hash containing session information.

Returns:

  • (Hash)

    a hash containing session information



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 19

module MHVSessionBasedClient
  extend ActiveSupport::Concern
  include MhvLockedSessionClient
  include SentryLogging

  attr_reader :session

  def user_key
    session.user_id
  end

  def invalid?(session)
    session.expired?
  end

  def session_config_key
    :mhv_session_lock
  end

  ##
  # Creates a session from the request headers
  #
  # @return [Rx::ClientSession] if an Rx (Prescription) client session
  # @return [SM::ClientSession] if a SM (Secure Messaging) client session
  #
  def get_session
    env = get_session_tagged
    req_headers = env.request_headers
    res_headers = env.response_headers
    new_session = @session.class.new(user_id: req_headers['mhvCorrelationId'],
                                     expires_at: res_headers['expires'],
                                     token: res_headers['token'])
    new_session.save
    new_session
  end

  private

  def get_session_tagged
    Sentry.set_tags(error: 'mhv_session')
    env = perform(:get, 'session', nil, auth_headers)
    Sentry.get_current_scope.tags.delete(:error)
    env
  end

  def token_headers
    config.base_request_headers.merge('Token' => session.token)
  end

  def auth_headers
    config.base_request_headers.merge('appToken' => config.app_token, 'mhvCorrelationId' => session.user_id.to_s)
  end
end

Instance Method Details

#auth_headersObject (private)



68
69
70
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 68

def auth_headers
  config.base_request_headers.merge('appToken' => config.app_token, 'mhvCorrelationId' => session.user_id.to_s)
end

#get_sessionRx::ClientSession, SM::ClientSession

Creates a session from the request headers

Returns:



44
45
46
47
48
49
50
51
52
53
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 44

def get_session
  env = get_session_tagged
  req_headers = env.request_headers
  res_headers = env.response_headers
  new_session = @session.class.new(user_id: req_headers['mhvCorrelationId'],
                                   expires_at: res_headers['expires'],
                                   token: res_headers['token'])
  new_session.save
  new_session
end

#get_session_taggedObject (private)



57
58
59
60
61
62
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 57

def get_session_tagged
  Sentry.set_tags(error: 'mhv_session')
  env = perform(:get, 'session', nil, auth_headers)
  Sentry.get_current_scope.tags.delete(:error)
  env
end

#invalid?(session) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 30

def invalid?(session)
  session.expired?
end

#session_config_keyObject



34
35
36
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 34

def session_config_key
  :mhv_session_lock
end

#token_headersObject (private)



64
65
66
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 64

def token_headers
  config.base_request_headers.merge('Token' => session.token)
end

#user_keyObject



26
27
28
# File 'lib/common/client/concerns/mhv_session_based_client.rb', line 26

def user_key
  session.user_id
end