Introduction to Authorization & Role-Based Access Control (RBAC)

Introduction to Authorization & Role-Based Access Control (RBAC)

Last updated: 3/7/2025

1 hour
Medium

Introduction to Authorization & Role-Based Access Control (RBAC)

🌍 Introduction

In the previous lessons, we implemented authentication, which verifies who a user is.
Now, we need authorization, which controls what a user can do.

In this lesson, you'll learn: βœ… What authorization is and why it’s important.
βœ… The difference between authentication and authorization.
βœ… How Role-Based Access Control (RBAC) works.
βœ… Different authorization strategies for APIs.


πŸ“Œ 1. What is Authorization?

Authorization determines what actions a user is allowed to perform after being authenticated.

πŸ”Ή Example:

  • A regular user can only access their own todos.
  • An admin can manage all users' todos.

Without proper authorization, a user could delete another user's data, leading to security issues.


πŸ“Œ 2. Authentication vs. Authorization

πŸ”Ή Authentication β†’ Verifies who you are (Login)
πŸ”Ή Authorization β†’ Verifies what you can do (Permissions)

FeatureAuthenticationAuthorization
PurposeIdentifies a userControls user actions
Happens When?Before authorizationAfter authentication
ExampleLogging in with a passwordChecking if a user can access an admin page
TechnologiesPasswords, JWT, OAuthRBAC, permissions

πŸ“Œ 3. Authorization Strategies in APIs

There are different ways to control user access:

βœ… 1️⃣ Role-Based Access Control (RBAC)

πŸ”Ή Users have predefined roles (e.g., admin, user).
πŸ”Ή Permissions are assigned based on roles.

Example:

RoleActions Allowed
AdminManage all users & todos
UserManage only their own todos

βœ… 2️⃣ Attribute-Based Access Control (ABAC)

πŸ”Ή Access is granted based on attributes (e.g., department, location).
πŸ”Ή More flexible than RBAC.

Example:

  • A user from HR can access employee records.
  • A manager can approve requests in their department.

βœ… 3️⃣ Permission-Based Authorization

πŸ”Ή Instead of assigning roles, users get specific permissions.
πŸ”Ή More fine-grained control than RBAC.

Example:

UserPermissions
Alicecan_create_todos, can_edit_own_todos
Bob (Admin)can_create_todos, can_edit_all_todos, can_delete_users

πŸ“Œ 4. Role-Based Access Control (RBAC) in a TodoList API

We'll assign roles and protect routes in the next lesson.

πŸ”Ή Users Table (MongoDB)

{ "_id": "656a7f3bc4d91b0012a8d849", "name": "Alice", "email": "alice@example.com", "password": "hashedpassword", "role": "user" }

πŸ”Ή Admins will have access to more routes:

EndpointUserAdmin
GET /todos (own todos)βœ… Allowedβœ… Allowed
DELETE /users/:id❌ Deniedβœ… Allowed

🎯 Summary

βœ… Authorization controls what users can do in an API.
βœ… RBAC assigns permissions based on roles (Admin/User).
βœ… Fine-grained control is possible using ABAC or permission-based access.


βœ… Next Lesson: Implementing Role-Based Authorization in APIs

In the next lesson, we’ll implement role-based authorization in our TodoList API! πŸš€