Course contentsShow
← Security
Lesson 1023 of 3,05221. Web Security - API SecurityFree lesson

Resource-Based Authorization Patterns

Implementing proper authorization checks based on resource ownership and relationship context.

Resource-Based Authorization Patterns

What you'll learn: How to properly authorize API requests by validating that users can only access resources they own or have legitimate relationships with.

The Core Problem

Many APIs check if a user is authenticated, but fail to verify which resources that user should access. Resource-based authorization ensures that even authenticated users can only interact with their own data or resources they're explicitly permitted to access.

Understanding Resource Ownership

Think of resource-based authorization like a hotel key card system. Your card proves you're a guest (authentication), but it only opens your room, not every room in the hotel. The system checks both who you are AND which specific room you're trying to access.

In APIs, this means validating the relationship between:

  • The authenticated user making the request
  • The specific resource being accessed (by ID)
  • The ownership or permission context

Common Authorization Patterns

Direct Ownership

The simplest pattern: verify the resource belongs to the requesting user.

GET /api/orders/12345
→ Check: Does order 12345 belong to current user?

Relationship-Based Access

More complex: validate indirect relationships through organizational hierarchies or team memberships.

GET /api/documents/789
→ Check: Is user in the same team/project as document 789?

Role-Scoped Resources

Combine role checks with resource context—admins might access all resources, while users only access their own.

Implementation Strategy

Always perform authorization checks after retrieving the resource from the database, comparing resource attributes (like owner_id) against the authenticated user's identity. Never trust client-provided data to filter resources.

Key Takeaway: Resource-based authorization validates not just WHO the user is, but WHETHER they should access THIS SPECIFIC resource through ownership, relationships, or explicit permissions.