Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,135 +1,135 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from flask import Flask, render_template, request, jsonify, send_from_directory
|
| 3 |
-
from flask_cors import CORS
|
| 4 |
-
from werkzeug.utils import secure_filename
|
| 5 |
-
from pdf_logic import process_comparison
|
| 6 |
-
|
| 7 |
-
app = Flask(__name__)
|
| 8 |
-
CORS(app)
|
| 9 |
-
|
| 10 |
-
# Configuration
|
| 11 |
-
UPLOAD_FOLDER = 'static/uploads'
|
| 12 |
-
OUTPUT_FOLDER = 'static/output'
|
| 13 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 14 |
-
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
@app.route('/compare', methods=['POST'])
|
| 18 |
-
def compare():
|
| 19 |
-
if 'source' not in request.files or 'destination' not in request.files:
|
| 20 |
-
return jsonify({'error': 'Missing files'}), 400
|
| 21 |
-
|
| 22 |
-
source_file = request.files['source']
|
| 23 |
-
dest_file = request.files['destination']
|
| 24 |
-
|
| 25 |
-
s_path = os.path.join(UPLOAD_FOLDER, secure_filename(source_file.filename))
|
| 26 |
-
d_path = os.path.join(UPLOAD_FOLDER, secure_filename(dest_file.filename))
|
| 27 |
-
|
| 28 |
-
source_file.save(s_path)
|
| 29 |
-
dest_file.save(d_path)
|
| 30 |
-
|
| 31 |
-
try:
|
| 32 |
-
# Run the comparison logic
|
| 33 |
-
src_out, dest_out, changes, total_pages = process_comparison(s_path, d_path, OUTPUT_FOLDER)
|
| 34 |
-
|
| 35 |
-
return jsonify({
|
| 36 |
-
'status': 'success',
|
| 37 |
-
'source_pdf': f"/static/output/{src_out}",
|
| 38 |
-
'dest_pdf': f"/static/output/{dest_out}",
|
| 39 |
-
'changes': changes,
|
| 40 |
-
'total_pages': total_pages
|
| 41 |
-
})
|
| 42 |
-
except Exception as e:
|
| 43 |
-
print(e)
|
| 44 |
-
return jsonify({'error': str(e)}), 500
|
| 45 |
-
|
| 46 |
-
# WorkOS Configuration
|
| 47 |
-
import workos
|
| 48 |
-
from workos import WorkOSClient
|
| 49 |
-
|
| 50 |
-
# Initialize WorkOS Client
|
| 51 |
-
# In production, use environment variables: os.environ.get('WORKOS_API_KEY')
|
| 52 |
-
# For now we will check if they exist, otherwise these endpoints will fail gracefully or you can hardcode for testing if needed
|
| 53 |
-
workos.api_key = os.environ.get('WORKOS_API_KEY', 'placeholder_key')
|
| 54 |
-
workos.client_id = os.environ.get('WORKOS_CLIENT_ID', 'placeholder_id')
|
| 55 |
-
|
| 56 |
-
@app.route('/auth/verify-session', methods=['POST'])
|
| 57 |
-
def verify_session():
|
| 58 |
-
"""
|
| 59 |
-
Enforces single-session policy.
|
| 60 |
-
Expects JSON: { "userId": "user_...", "currentSessionId": "session_..." }
|
| 61 |
-
"""
|
| 62 |
-
data = request.json
|
| 63 |
-
user_id = data.get('userId')
|
| 64 |
-
current_session_id = data.get('currentSessionId')
|
| 65 |
-
|
| 66 |
-
if not user_id or not current_session_id:
|
| 67 |
-
return jsonify({'error': 'Missing userId or currentSessionId'}), 400
|
| 68 |
-
|
| 69 |
-
try:
|
| 70 |
-
# 1. List all active sessions for the user
|
| 71 |
-
# Note: WorkOS Python SDK ListSessions usage might vary by version;
|
| 72 |
-
# checking User Management > List Sessions or similar via SDK.
|
| 73 |
-
# As of recent versions, it's workos.user_management.list_sessions(user_id=...)
|
| 74 |
-
# or verify standard usage. We'll use the generic client resources if specific helper isn't found,
|
| 75 |
-
# but workos.user_management is the standard path.
|
| 76 |
-
|
| 77 |
-
sessions_list = workos.user_management.list_sessions(
|
| 78 |
-
user_id=user_id,
|
| 79 |
-
type='user_session' # Ensure we get user sessions
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
-
active_sessions = sessions_list.data
|
| 83 |
-
revoked_count = 0
|
| 84 |
-
|
| 85 |
-
# 2. Iterate and revoke any session that is NOT the current one
|
| 86 |
-
for session in active_sessions:
|
| 87 |
-
if session.id != current_session_id:
|
| 88 |
-
# Revoke this old/extra session
|
| 89 |
-
workos.user_management.revoke_session(session.id)
|
| 90 |
-
revoked_count += 1
|
| 91 |
-
|
| 92 |
-
return jsonify({
|
| 93 |
-
'status': 'success',
|
| 94 |
-
'revoked_count': revoked_count,
|
| 95 |
-
'message': f'Session verified. {revoked_count} other sessions revoked.'
|
| 96 |
-
})
|
| 97 |
-
|
| 98 |
-
except Exception as e:
|
| 99 |
-
print(f"WorkOS Error: {e}")
|
| 100 |
-
# If API key is invalid or not set, this will error.
|
| 101 |
-
# We return 500 but frontend should handle it (maybe allow login if strict mode is off)
|
| 102 |
-
return jsonify({'error': str(e)}), 500
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
@app.route('/auth/logout-everywhere', methods=['POST'])
|
| 106 |
-
def logout_everywhere():
|
| 107 |
-
"""
|
| 108 |
-
Revokes ALL sessions for the user.
|
| 109 |
-
Expects JSON: { "userId": "user_..." }
|
| 110 |
-
"""
|
| 111 |
-
data = request.json
|
| 112 |
-
user_id = data.get('userId')
|
| 113 |
-
|
| 114 |
-
if not user_id:
|
| 115 |
-
return jsonify({'error': 'Missing userId'}), 400
|
| 116 |
-
|
| 117 |
-
try:
|
| 118 |
-
# List all sessions
|
| 119 |
-
sessions_list = workos.user_management.list_sessions(user_id=user_id)
|
| 120 |
-
|
| 121 |
-
# Revoke all
|
| 122 |
-
for session in sessions_list.data:
|
| 123 |
-
workos.user_management.revoke_session(session.id)
|
| 124 |
-
|
| 125 |
-
return jsonify({'status': 'success', 'message': 'All sessions revoked.'})
|
| 126 |
-
|
| 127 |
-
except Exception as e:
|
| 128 |
-
print(f"WorkOS Error: {e}")
|
| 129 |
-
return jsonify({'error': str(e)}), 500
|
| 130 |
-
|
| 131 |
-
if __name__ == '__main__':
|
| 132 |
-
app.run(port=5000)
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, render_template, request, jsonify, send_from_directory
|
| 3 |
+
from flask_cors import CORS
|
| 4 |
+
from werkzeug.utils import secure_filename
|
| 5 |
+
from pdf_logic import process_comparison
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
CORS(app)
|
| 9 |
+
|
| 10 |
+
# Configuration
|
| 11 |
+
UPLOAD_FOLDER = 'static/uploads'
|
| 12 |
+
OUTPUT_FOLDER = 'static/output'
|
| 13 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 14 |
+
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@app.route('/compare', methods=['POST'])
|
| 18 |
+
def compare():
|
| 19 |
+
if 'source' not in request.files or 'destination' not in request.files:
|
| 20 |
+
return jsonify({'error': 'Missing files'}), 400
|
| 21 |
+
|
| 22 |
+
source_file = request.files['source']
|
| 23 |
+
dest_file = request.files['destination']
|
| 24 |
+
|
| 25 |
+
s_path = os.path.join(UPLOAD_FOLDER, secure_filename(source_file.filename))
|
| 26 |
+
d_path = os.path.join(UPLOAD_FOLDER, secure_filename(dest_file.filename))
|
| 27 |
+
|
| 28 |
+
source_file.save(s_path)
|
| 29 |
+
dest_file.save(d_path)
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
# Run the comparison logic
|
| 33 |
+
src_out, dest_out, changes, total_pages = process_comparison(s_path, d_path, OUTPUT_FOLDER)
|
| 34 |
+
|
| 35 |
+
return jsonify({
|
| 36 |
+
'status': 'success',
|
| 37 |
+
'source_pdf': f"/static/output/{src_out}",
|
| 38 |
+
'dest_pdf': f"/static/output/{dest_out}",
|
| 39 |
+
'changes': changes,
|
| 40 |
+
'total_pages': total_pages
|
| 41 |
+
})
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(e)
|
| 44 |
+
return jsonify({'error': str(e)}), 500
|
| 45 |
+
|
| 46 |
+
# WorkOS Configuration
|
| 47 |
+
import workos
|
| 48 |
+
from workos import WorkOSClient
|
| 49 |
+
|
| 50 |
+
# Initialize WorkOS Client
|
| 51 |
+
# In production, use environment variables: os.environ.get('WORKOS_API_KEY')
|
| 52 |
+
# For now we will check if they exist, otherwise these endpoints will fail gracefully or you can hardcode for testing if needed
|
| 53 |
+
workos.api_key = os.environ.get('WORKOS_API_KEY', 'placeholder_key')
|
| 54 |
+
workos.client_id = os.environ.get('WORKOS_CLIENT_ID', 'placeholder_id')
|
| 55 |
+
|
| 56 |
+
@app.route('/auth/verify-session', methods=['POST'])
|
| 57 |
+
def verify_session():
|
| 58 |
+
"""
|
| 59 |
+
Enforces single-session policy.
|
| 60 |
+
Expects JSON: { "userId": "user_...", "currentSessionId": "session_..." }
|
| 61 |
+
"""
|
| 62 |
+
data = request.json
|
| 63 |
+
user_id = data.get('userId')
|
| 64 |
+
current_session_id = data.get('currentSessionId')
|
| 65 |
+
|
| 66 |
+
if not user_id or not current_session_id:
|
| 67 |
+
return jsonify({'error': 'Missing userId or currentSessionId'}), 400
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
# 1. List all active sessions for the user
|
| 71 |
+
# Note: WorkOS Python SDK ListSessions usage might vary by version;
|
| 72 |
+
# checking User Management > List Sessions or similar via SDK.
|
| 73 |
+
# As of recent versions, it's workos.user_management.list_sessions(user_id=...)
|
| 74 |
+
# or verify standard usage. We'll use the generic client resources if specific helper isn't found,
|
| 75 |
+
# but workos.user_management is the standard path.
|
| 76 |
+
|
| 77 |
+
sessions_list = workos.user_management.list_sessions(
|
| 78 |
+
user_id=user_id,
|
| 79 |
+
type='user_session' # Ensure we get user sessions
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
active_sessions = sessions_list.data
|
| 83 |
+
revoked_count = 0
|
| 84 |
+
|
| 85 |
+
# 2. Iterate and revoke any session that is NOT the current one
|
| 86 |
+
for session in active_sessions:
|
| 87 |
+
if session.id != current_session_id:
|
| 88 |
+
# Revoke this old/extra session
|
| 89 |
+
workos.user_management.revoke_session(session.id)
|
| 90 |
+
revoked_count += 1
|
| 91 |
+
|
| 92 |
+
return jsonify({
|
| 93 |
+
'status': 'success',
|
| 94 |
+
'revoked_count': revoked_count,
|
| 95 |
+
'message': f'Session verified. {revoked_count} other sessions revoked.'
|
| 96 |
+
})
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"WorkOS Error: {e}")
|
| 100 |
+
# If API key is invalid or not set, this will error.
|
| 101 |
+
# We return 500 but frontend should handle it (maybe allow login if strict mode is off)
|
| 102 |
+
return jsonify({'error': str(e)}), 500
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
@app.route('/auth/logout-everywhere', methods=['POST'])
|
| 106 |
+
def logout_everywhere():
|
| 107 |
+
"""
|
| 108 |
+
Revokes ALL sessions for the user.
|
| 109 |
+
Expects JSON: { "userId": "user_..." }
|
| 110 |
+
"""
|
| 111 |
+
data = request.json
|
| 112 |
+
user_id = data.get('userId')
|
| 113 |
+
|
| 114 |
+
if not user_id:
|
| 115 |
+
return jsonify({'error': 'Missing userId'}), 400
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
# List all sessions
|
| 119 |
+
sessions_list = workos.user_management.list_sessions(user_id=user_id)
|
| 120 |
+
|
| 121 |
+
# Revoke all
|
| 122 |
+
for session in sessions_list.data:
|
| 123 |
+
workos.user_management.revoke_session(session.id)
|
| 124 |
+
|
| 125 |
+
return jsonify({'status': 'success', 'message': 'All sessions revoked.'})
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
print(f"WorkOS Error: {e}")
|
| 129 |
+
return jsonify({'error': str(e)}), 500
|
| 130 |
+
|
| 131 |
+
if __name__ == '__main__':
|
| 132 |
+
# app.run(port=5000)
|
| 133 |
+
from waitress import serve
|
| 134 |
+
|
| 135 |
+
serve(app, host="0.0.0.0", port=7860)
|