Performance: Why authorization matters
Authorization affects server response time and user experience by controlling access to resources, impacting perceived speed and security.
from flask import Flask, request, abort app = Flask(__name__) @app.route('/dashboard') def dashboard(): user = request.args.get('user') if user != 'admin': abort(403) # expensive data processing here return 'Welcome to admin dashboard'
from flask import Flask, request app = Flask(__name__) @app.route('/dashboard') def dashboard(): # expensive data processing here user = request.args.get('user') if user != 'admin': return 'Access Denied', 403 return 'Welcome to admin dashboard'
| Pattern | Server Processing | Response Delay | Network Impact | Verdict |
|---|---|---|---|---|
| Late authorization check after processing | High (full processing done) | High (blocks response) | No change | [X] Bad |
| Early authorization check with abort | Low (processing skipped if unauthorized) | Low (fast response) | No change | [OK] Good |
from flask import abort
@app.route('/admin')
def admin_panel():
if current_user.role != 'admin':
abort(403)
return 'Welcome, admin!'@app.route('/dashboard')
def dashboard():
if current_user.role = 'admin':
return 'Admin dashboard'
else:
return 'User dashboard'@app.route('/reports')
def reports():
if current_user.role in ['admin', 'manager']:
return 'Reports page'
else:
abort(403)Pass the test and this pattern moves unknown