Understanding Authentication vs Authorization
# CHAPTER 2
Understanding Authentication vs Authorization
1. Introduction
The terms "Authentication" and "Authorization" are often used interchangeably by beginners, but in backend engineering, confusing the two will lead to catastrophic security breaches. They represent two completely distinct phases of security. In this chapter, we will definitively separate these concepts, define access control, and introduce the concept of User Roles and Permissions.2. Learning Objectives
By the end of this chapter, you will be able to:- Clearly articulate the difference between Authentication (AuthN) and Authorization (AuthZ).
- Understand the chronological flow of a secure request.
- Define what a "Permission" is.
- Understand the concept of "User Roles" and Access Control.
3. Beginner-Friendly Explanation
Let's return to the hotel analogy. Authentication (AuthN): You walk up to the front desk. You hand the receptionist your ID. The receptionist verifies your face matches the ID. You have been Authenticated. The receptionist hands you a plastic keycard. *You have proven WHO you are.*Authorization (AuthZ): You take your keycard and walk to the 5th floor. You try to swipe your keycard on the door to the gym. The light flashes green; the door opens. You try to swipe your keycard on the door to another guest's room. The light flashes red; the door stays locked. *The system has checked WHAT you are allowed to do.*
If you are Authenticated, you are allowed inside the building. If you are Authorized, you are allowed inside a specific room.
4. Authentication (AuthN)
- Goal: Verify identity.
- Question Answered: "Who are you?"
- Mechanism: Passwords, Biometrics, OTPs (One-Time Passwords).
- Result: The system generates a Session or a Token indicating you are a valid user.
5. Authorization (AuthZ)
- Goal: Enforce permissions and access control.
- Question Answered: "Are you allowed to do this specific action?"
- Mechanism: Checking User Roles (Admin vs. User), checking resource ownership (Did you write this post?), reading Access Control Lists (ACLs).
- Result: The system executes the requested action OR throws a "403 Forbidden" error.
6. The Chronological Security Flow
When a user attempts to delete a blog post, the backend server must execute these steps in order:- 1. AuthN Check: Is the user logged in? (If no -> Return 401 Unauthorized).
- 2. AuthZ Check: Is the user an Admin, OR did the user write this specific post? (If no -> Return 403 Forbidden).
- 3. Execution: Delete the post from the database.
*Critical Failure:* If a developer forgets Step 2, a logged-in user could delete another user's post. This is called a Broken Access Control vulnerability, and it is the #1 security flaw on the internet.
7. Roles and Permissions
To manage Authorization at scale, systems group permissions into Roles.-
Permission: A specific action. (e.g.,
can_delete_users,can_publish_articles,can_view_dashboard).
-
Role: A container of permissions. (e.g., The
Moderatorrole containscan_publish_articlesandcan_view_dashboard, but NOTcan_delete_users).
When a user is created, they are assigned a Role. The software checks the Role before granting access.
8. Real-World Code Example (Express.js)
Here is how AuthN and AuthZ look when written as middleware in an Express.js API.9. Best Practices
- Principle of Least Privilege: A user should only be given the absolute minimum permissions necessary to perform their job. If an employee's job is just to write blog posts, they should not be given an "Admin" role just because it's easier to configure.
10. Common Mistakes
- Confusing 401 and 403 HTTP Codes:
-
401 Unauthorizedactually means "Unauthenticated." The user is not logged in.
-
403 Forbiddenmeans "Unauthorized." The user IS logged in, but they lack the permissions to view this specific page.
11. Exercises
- 1. Define the terms AuthN and AuthZ. Which one must always occur first chronologically?
12. Coding Challenges
-
Challenge: Write a PHP or Python pseudo-code function named
delete_comment(user_object, comment_object). Write theifstatements required to implement authorization. The user should only be allowed to delete the comment IFuser_object.role == "admin"ORuser_object.id == comment_object.author_id.
13. MCQs with Answers
Which of the following scenarios is a failure of Authorization (AuthZ) rather than Authentication (AuthN)?
Which HTTP status code should a backend server return if a user is successfully logged in, but attempts to access a resource reserved strictly for Administrators?
14. Interview Questions
- Q: Differentiate between Authentication and Authorization. Provide a real-world software example illustrating how a failure in Authorization could lead to a data breach even if Authentication is perfectly secure.
- Q: Explain the "Principle of Least Privilege" in the context of designing an Access Control system.