Module: Common::Client::Concerns::MHVJwtSessionClient
Overview
Module mixin for overriding session logic when making MHV JWT-based client connections
Constant Summary
Common::Client::Concerns::MhvLockedSessionClient::LOCK_RETRY_DELAY, Common::Client::Concerns::MhvLockedSessionClient::RETRY_ATTEMPTS
Instance Attribute Summary collapse
Instance Method Summary
collapse
#authenticate, #initialize, #invalid?, #lock_and_get_session, #obtain_redis_lock, #refresh_session, #release_redis_lock
#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata
Instance Attribute Details
#session ⇒ Hash
Returns a hash containing session information.
16
17
18
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 16
module MHVJwtSessionClient
extend ActiveSupport::Concern
include MhvLockedSessionClient
protected
def user_key
session.icn
end
def session_config_key
:mhv_mr_fhir_session_lock
end
def get_session
validate_session_params
env = get_session_tagged
jwt = (env.)
decoded_token = decode_jwt_token(jwt)
session.expires_at = (decoded_token)
@session.class.new(user_id: session.user_id.to_s,
icn: session.icn,
expires_at: session.expires_at,
token: jwt)
end
private
def ()
= ['authorization']
if .nil? || !.start_with?('Bearer ')
raise Common::Exceptions::Unauthorized, detail: 'Invalid or missing authorization header'
end
.sub('Bearer ', '')
end
def decode_jwt_token(jwt_token)
JWT.decode jwt_token, nil, false
rescue JWT::DecodeError
raise Common::Exceptions::Unauthorized, detail: 'Invalid JWT token'
end
def (decoded_token)
if decoded_token[0]['exp']
Time.zone.at(decoded_token[0]['exp']).to_datetime.rfc2822
else
1.hour.from_now.rfc2822
end
end
def validate_session_params
raise Common::Exceptions::ParameterMissing, 'ICN' if session.icn.blank?
raise Common::Exceptions::ParameterMissing, 'MHV MR App Token' if config.app_token.blank?
end
def get_session_tagged
Sentry.set_tags(error: 'mhv_session')
env = perform(:post, '/mhvapi/security/v1/login', auth_body, )
Sentry.get_current_scope.tags.delete(:error)
env
end
def jwt_bearer_token
session.token
end
def patient_fhir_id
session.patient_fhir_id
end
def
config..merge('Content-Type' => 'application/json')
end
def auth_body
{
'appId' => '103',
'appToken' => config.app_token,
'subject' => session.icn,
'userType' => 'PATIENT',
'authParams' => {
'PATIENT_SUBJECT_ID_TYPE' => 'ICN'
}
}
end
end
|
Instance Method Details
#auth_body ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 98
def auth_body
{
'appId' => '103',
'appToken' => config.app_token,
'subject' => session.icn,
'userType' => 'PATIENT',
'authParams' => {
'PATIENT_SUBJECT_ID_TYPE' => 'ICN'
}
}
end
|
94
95
96
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 94
def
config..merge('Content-Type' => 'application/json')
end
|
#decode_jwt_token(jwt_token) ⇒ Object
60
61
62
63
64
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 60
def decode_jwt_token(jwt_token)
JWT.decode jwt_token, nil, false
rescue JWT::DecodeError
raise Common::Exceptions::Unauthorized, detail: 'Invalid JWT token'
end
|
66
67
68
69
70
71
72
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 66
def (decoded_token)
if decoded_token[0]['exp']
Time.zone.at(decoded_token[0]['exp']).to_datetime.rfc2822
else
1.hour.from_now.rfc2822
end
end
|
50
51
52
53
54
55
56
57
58
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 50
def ()
= ['authorization']
if .nil? || !.start_with?('Bearer ')
raise Common::Exceptions::Unauthorized, detail: 'Invalid or missing authorization header'
end
.sub('Bearer ', '')
end
|
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 35
def get_session
validate_session_params
env = get_session_tagged
jwt = (env.)
decoded_token = decode_jwt_token(jwt)
session.expires_at = (decoded_token)
@session.class.new(user_id: session.user_id.to_s,
icn: session.icn,
expires_at: session.expires_at,
token: jwt)
end
|
#get_session_tagged ⇒ Object
79
80
81
82
83
84
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 79
def get_session_tagged
Sentry.set_tags(error: 'mhv_session')
env = perform(:post, '/mhvapi/security/v1/login', auth_body, )
Sentry.get_current_scope.tags.delete(:error)
env
end
|
#jwt_bearer_token ⇒ Object
86
87
88
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 86
def jwt_bearer_token
session.token
end
|
#patient_fhir_id ⇒ Object
90
91
92
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 90
def patient_fhir_id
session.patient_fhir_id
end
|
#session_config_key ⇒ Object
26
27
28
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 26
def session_config_key
:mhv_mr_fhir_session_lock
end
|
#user_key ⇒ Object
22
23
24
|
# File 'lib/common/client/concerns/mhv_jwt_session_client.rb', line 22
def user_key
session.icn
end
|
#validate_session_params ⇒ Object