0
0
Flaskframework~20 mins

Why authorization matters in Flask - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Authorization Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is authorization important in a Flask app?

Imagine a Flask web app where users can view and edit their profiles. Why is authorization important here?

ATo encrypt user passwords before storing them.
BTo speed up the app by caching user data.
CTo ensure users can only edit their own profiles, preventing unauthorized changes.
DTo allow users to register new accounts.
Attempts:
2 left
💡 Hint

Think about what could happen if anyone could edit any profile.

component_behavior
intermediate
2:00remaining
What happens if a Flask route lacks authorization checks?

Consider this Flask route that shows user details without authorization checks. What risk does this pose?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/user/<int:user_id>')
def user_detail(user_id):
    # No authorization check here
    return f"User details for user {user_id}"
AAny user can view details of any other user, risking privacy breaches.
BThe app will crash due to missing authorization code.
CUsers will be logged out automatically.
DThe route will only work for admin users.
Attempts:
2 left
💡 Hint

What if a user tries to see another user's info?

state_output
advanced
2:00remaining
What is the output of this Flask authorization check?

Given this Flask route with a simple authorization check, what will be the output if a user with role 'guest' accesses it?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/admin')
def admin_panel():
    user_role = request.args.get('role', 'guest')
    if user_role != 'admin':
        return 'Access denied', 403
    return 'Welcome to admin panel', 200
AAccess denied with status code 403
BServer error 500
CWelcome to admin panel with status code 200
DRedirect to login page
Attempts:
2 left
💡 Hint

Check the role value and the if condition.

📝 Syntax
advanced
2:00remaining
Which Flask code snippet correctly implements authorization?

Choose the code snippet that correctly checks if a user is authorized before accessing a route.

A
if current_user.is_authenticated and current_user.role == 'admin'
    return 'Admin access granted'
else:
    return 'Access denied', 403
B
if current_user.is_authenticated and current_user.role == 'admin':
    return 'Admin access granted'
else:
    return 'Access denied', 403
C
if current_user.is_authenticated and current_user.role = 'admin':
    return 'Admin access granted'
else:
    return 'Access denied', 403
D
if current_user.is_authenticated or current_user.role == 'admin':
    return 'Admin access granted'
else:
    return 'Access denied', 403
Attempts:
2 left
💡 Hint

Look for correct syntax and logic operators.

🔧 Debug
expert
3:00remaining
Why does this Flask authorization code fail to restrict access?

Examine this Flask route. Why does it fail to restrict access properly?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/dashboard')
def dashboard():
    user_role = request.args.get('role')
    if user_role == 'admin':
        return 'Welcome admin'
    return 'Access denied', 403
AThe route lacks a decorator to enforce login, so unauthorized users can access it.
BThe function returns a string instead of a JSON response, causing a runtime error.
CThe code uses '==' instead of 'is' for string comparison, causing a logic error.
DIf 'role' parameter is missing, user_role is None, so access is denied even for admins without role in URL.
Attempts:
2 left
💡 Hint

What happens if the URL does not include the 'role' parameter?