liveBook · Manning

Authorization decision

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

Authorization is the process of determining which actions or resources an authenticated user or system is permitted to access.

Authentication answers “Who are you?” Authorization decides “What can you do?” It uses a user’s role or permissions to control access to actions, resources, and application routes. section 9.6 section 9.9

A role-based authorization design can assign capabilities such as:

  • Administrators can update, activate, or deactivate any content and can activate or deactivate users. section 9.6
  • Editors can update any content but cannot deactivate users or content. section 9.6
  • Registered users can create, activate, or deactivate their own content without changing another user’s content or active state. section 9.6

Protecting an administrator route

Stack route decorators so a user must be logged in and possess the administrator permission. The authorization check aborts with HTTP 403 Forbidden when the permission is missing; otherwise, the route continues normally. section 9.6.2

from functools import wraps
from flask import abort
from flask_login import current_user
 
def authorization_required(permissions):             #1
    def wrapper(func):                               #2
        @wraps(func)                                 #3
        def wrapped_function(*args, **kwargs):       #4
            if not current_user.role.permissions
             & permissions:                        #5
                abort(403)                           #6
            return func(*args, **kwargs)             #6
        return wrapped_function
    return wrapper

The decorator accepts a permission, compares it with the current user’s permissions using a bitwise check, aborts unauthorized requests, and invokes the protected function when allowed. section 9.6.2

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains role capabilities, route decorators, permission checks, forbidden responses, and configuration-based administrator assignment; defers stronger security measures.
Examples
Uses administrator, editor, and registered-user roles, an administrator-only route, a 403 response, and configured administrator email addresses.
What it assumes
Assumes authentication and user identity, then adds roles and permissions to govern capabilities.
What you can do after
Apply it via Flask route decorators to restrict pages and actions according to authenticated users’ roles or permissions.
How it connects
Connects authentication with roles, permissions, decorators, protected routes, users, and content ownership.

Also explained in

Compare how 7 books explain authorization decision →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.