add dedicated auth/callback add dashboard auth
Browse files- app.py +29 -9
- utils/callbackmanager.py +9 -3
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, send_file, jsonify, Response
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import logging
|
|
@@ -68,6 +68,26 @@ def auth():
|
|
| 68 |
return render_template('auth.html', auth_url=auth_url, auth_result=result)
|
| 69 |
return render_template('auth.html', auth_url=auth_url)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
@app.route('/auth/patient-data', methods=['GET'])
|
| 72 |
def patient_data():
|
| 73 |
data = CALLBACK_MANAGER.get_patient_data()
|
|
@@ -79,14 +99,14 @@ def generate_meldrx_pdf():
|
|
| 79 |
pdf_path = generate_pdf_from_meldrx(patient_data)
|
| 80 |
return send_file(pdf_path, as_attachment=True, download_name="meldrx_patient_data.pdf")
|
| 81 |
|
| 82 |
-
@app.route('/dashboard', methods=['GET'])
|
| 83 |
-
def dashboard():
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
@app.route('/form', methods=['GET', 'POST'])
|
| 92 |
def discharge_form():
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, send_file, jsonify, Response, redirect, url_for
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import logging
|
|
|
|
| 68 |
return render_template('auth.html', auth_url=auth_url, auth_result=result)
|
| 69 |
return render_template('auth.html', auth_url=auth_url)
|
| 70 |
|
| 71 |
+
@app.route('/auth/callback', methods=['GET'])
|
| 72 |
+
def auth_callback():
|
| 73 |
+
redirected_url = request.url
|
| 74 |
+
result = CALLBACK_MANAGER.handle_callback(redirected_url)
|
| 75 |
+
if "Authentication successful" in result:
|
| 76 |
+
return redirect(url_for('dashboard'))
|
| 77 |
+
return render_template('auth.html', auth_url=CALLBACK_MANAGER.get_auth_url(), auth_result=result)
|
| 78 |
+
|
| 79 |
+
@app.route('/dashboard', methods=['GET'])
|
| 80 |
+
def dashboard():
|
| 81 |
+
if not CALLBACK_MANAGER.access_token:
|
| 82 |
+
return render_template('auth.html', auth_url=CALLBACK_MANAGER.get_auth_url(), auth_result="<span style='color:#FF8C00;'>Please authenticate first.</span>")
|
| 83 |
+
|
| 84 |
+
data = CALLBACK_MANAGER.get_patient_data()
|
| 85 |
+
if data.startswith('<span'):
|
| 86 |
+
return render_template('dashboard.html', error=data)
|
| 87 |
+
patients_data = json.loads(data)
|
| 88 |
+
patients = [entry['resource'] for entry in patients_data.get('entry', []) if entry['resource'].get('resourceType') == 'Patient']
|
| 89 |
+
return render_template('dashboard.html', patients=patients, authenticated=True)
|
| 90 |
+
|
| 91 |
@app.route('/auth/patient-data', methods=['GET'])
|
| 92 |
def patient_data():
|
| 93 |
data = CALLBACK_MANAGER.get_patient_data()
|
|
|
|
| 99 |
pdf_path = generate_pdf_from_meldrx(patient_data)
|
| 100 |
return send_file(pdf_path, as_attachment=True, download_name="meldrx_patient_data.pdf")
|
| 101 |
|
| 102 |
+
# @app.route('/dashboard', methods=['GET'])
|
| 103 |
+
# def dashboard():
|
| 104 |
+
# data = CALLBACK_MANAGER.get_patient_data()
|
| 105 |
+
# if data.startswith('<span'): # Indicates an error or unauthenticated state
|
| 106 |
+
# return render_template('dashboard.html', error=data)
|
| 107 |
+
# patients_data = json.loads(data)
|
| 108 |
+
# patients = [entry['resource'] for entry in patients_data.get('entry', []) if entry['resource'].get('resourceType') == 'Patient']
|
| 109 |
+
# return render_template('dashboard.html', patients=patients, authenticated=True)
|
| 110 |
|
| 111 |
@app.route('/form', methods=['GET', 'POST'])
|
| 112 |
def discharge_form():
|
utils/callbackmanager.py
CHANGED
|
@@ -25,17 +25,23 @@ class CallbackManager:
|
|
| 25 |
self.api = MeldRxAPI(client_id, client_secret, workspace_id, redirect_uri)
|
| 26 |
self.auth_code = None
|
| 27 |
self.access_token = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def handle_callback(self, callback_url: str) -> str:
|
| 30 |
-
"""Handles the callback URL and extracts the code automatically."""
|
| 31 |
self.auth_code = extract_code_from_url(callback_url)
|
| 32 |
if not self.auth_code:
|
| 33 |
return "No authentication code found in URL."
|
| 34 |
-
|
| 35 |
if self.api.authenticate_with_code(self.auth_code):
|
| 36 |
self.access_token = self.api.access_token
|
|
|
|
|
|
|
| 37 |
return f"Authentication successful! Access Token: {self.access_token[:10]}... (truncated)"
|
| 38 |
-
|
| 39 |
|
| 40 |
def get_auth_url(self) -> str:
|
| 41 |
return self.api.get_authorization_url()
|
|
|
|
| 25 |
self.api = MeldRxAPI(client_id, client_secret, workspace_id, redirect_uri)
|
| 26 |
self.auth_code = None
|
| 27 |
self.access_token = None
|
| 28 |
+
# Load token from file if it exists
|
| 29 |
+
token_path = '/tmp/access_token.txt'
|
| 30 |
+
if os.path.exists(token_path):
|
| 31 |
+
with open(token_path, 'r') as f:
|
| 32 |
+
self.access_token = f.read().strip()
|
| 33 |
+
self.api.access_token = self.access_token
|
| 34 |
|
| 35 |
def handle_callback(self, callback_url: str) -> str:
|
|
|
|
| 36 |
self.auth_code = extract_code_from_url(callback_url)
|
| 37 |
if not self.auth_code:
|
| 38 |
return "No authentication code found in URL."
|
|
|
|
| 39 |
if self.api.authenticate_with_code(self.auth_code):
|
| 40 |
self.access_token = self.api.access_token
|
| 41 |
+
with open('/tmp/access_token.txt', 'w') as f:
|
| 42 |
+
f.write(self.access_token)
|
| 43 |
return f"Authentication successful! Access Token: {self.access_token[:10]}... (truncated)"
|
| 44 |
+
return "Authentication failed. Please check the authorization code."
|
| 45 |
|
| 46 |
def get_auth_url(self) -> str:
|
| 47 |
return self.api.get_authorization_url()
|