Skip to content

Commit e92d701

Browse files
author
JAMES FUQIAN
committed
sync with PR for review changes, add access denied message and other errors handling
1 parent 87a9aaf commit e92d701

1 file changed

Lines changed: 43 additions & 11 deletions

File tree

server/app.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
from cms_bluebutton.cms_bluebutton import BlueButton
55

66

7+
BENE_DENIED_ACCESS = "access_denied"
8+
FE_MSG_ACCESS_DENIED = "Beneficiary denied app access to their data"
9+
ERR_QUERY_EOB = "Error when querying the patient's EOB!"
10+
ERR_MISSING_AUTH_CODE = "Response was missing access code!"
11+
ERR_MISSING_STATE = "State is required when using PKCE"
12+
713
app = Flask(__name__)
814
bb = BlueButton()
915

1016
# This is where medicare.gov beneficiary associated
1117
# with the current logged in app user,
1218
# in real app, this could be the app specific
13-
# accoount management system
14-
loggedInUser = {
19+
# account management system
20+
logged_in_user = {
1521
'authToken': None,
1622
'eobData': None
1723
}
@@ -35,25 +41,40 @@ def get_auth_url():
3541
@app.route('/api/bluebutton/callback/', methods=['GET'])
3642
def authorization_callback():
3743
request_query = request.args
44+
45+
if (request_query.get('error') == BENE_DENIED_ACCESS):
46+
# clear all cached claims eob data since the bene has denied access
47+
# for the application
48+
clear_bb2_data()
49+
logged_in_user.update({'eobData': {'message': FE_MSG_ACCESS_DENIED}})
50+
print(FE_MSG_ACCESS_DENIED)
51+
return redirect(get_fe_redirect_url())
52+
3853
code = request_query.get('code')
54+
55+
if code is None:
56+
print(ERR_MISSING_AUTH_CODE)
57+
return redirect(get_fe_redirect_url())
58+
3959
state = request_query.get('state')
4060

61+
if state is None:
62+
print(ERR_MISSING_STATE)
63+
return redirect(get_fe_redirect_url())
64+
4165
auth_token = bb.get_authorization_token(auth_data, code, state)
4266

4367
# correlate app user with medicare bene
44-
loggedInUser['authToken'] = auth_token
68+
logged_in_user['authToken'] = auth_token
4569

4670
config = {
4771
"auth_token": auth_token,
4872
"params": {},
4973
"url": "to be overriden"
5074
}
5175

52-
# result = {}
53-
5476
try:
55-
# search eob
56-
77+
# search eob (or other fhir resources: patient, coverage, etc.)
5778
eob_data = bb.get_explaination_of_benefit_data(config)
5879

5980
# fhir search response could contain large number of resources,
@@ -65,9 +86,12 @@ def authorization_callback():
6586
# Use bb.get_pages(data, config) to get all the pages
6687

6788
auth_token = eob_data['auth_token']
68-
loggedInUser['authToken'] = auth_token
69-
loggedInUser['eobData'] = eob_data['response'].json()
89+
logged_in_user['authToken'] = auth_token
90+
logged_in_user['eobData'] = eob_data['response'].json()
7091
except Exception as ex:
92+
clear_bb2_data()
93+
logged_in_user.update({'eobData': {'message': ERR_QUERY_EOB}})
94+
print(ERR_QUERY_EOB)
7195
print(ex)
7296

7397
return redirect(get_fe_redirect_url())
@@ -81,8 +105,8 @@ def get_patient_eob():
81105
* This would be replaced by a persistence service layer for whatever
82106
* DB you would choose to use
83107
"""
84-
if loggedInUser and loggedInUser.get('eobData'):
85-
return loggedInUser.get('eobData')
108+
if logged_in_user and logged_in_user.get('eobData'):
109+
return logged_in_user.get('eobData')
86110
else:
87111
return {}
88112

@@ -95,5 +119,13 @@ def get_fe_redirect_url():
95119
return 'http://client:3000' if is_selenium else 'http://localhost:3000'
96120

97121

122+
def clear_bb2_data():
123+
'''
124+
helper to clean up cached result
125+
'''
126+
logged_in_user.update({'authToken': None})
127+
logged_in_user.update({'eobData': {}})
128+
129+
98130
if __name__ == '__main__':
99131
app.run(debug=True, host='0.0.0.0', port=3001)

0 commit comments

Comments
 (0)