What if you could stop guessing who someone is and what they can do, and instead have a clear, secure way to check both every time?
Authentication vs authorization in HLD - When to Use Which
Imagine you run a busy office where everyone needs to enter a building and access different rooms. Without a system, you have to check each person's ID and then remember which rooms they can enter. This manual checking slows everything down and causes confusion.
Manually verifying who someone is and what they can do is slow and error-prone. People might get access to places they shouldn't, or get denied entry by mistake. It's hard to keep track of permissions and identities separately, leading to security risks and frustration.
Authentication and authorization split the job into two clear steps: first, confirm who the person is (authentication), then decide what they are allowed to do (authorization). This separation makes systems faster, safer, and easier to manage.
if user_id == known_id and room in allowed_rooms: allow_entry()
if authenticate(user_credentials): if authorize(user, resource): allow_entry()
This clear separation lets systems securely control access at scale, ensuring only the right people do the right things.
When you log into your email, the system first checks your password (authentication). Then, it decides if you can read emails, send messages, or change settings (authorization).
Authentication verifies identity; authorization controls access.
Separating these improves security and clarity.
It helps build scalable, safe systems for many users.
Practice
authentication in system design?Solution
Step 1: Understand authentication purpose
Authentication is the process of confirming who the user is by checking credentials like username and password.Step 2: Differentiate from authorization
Authorization controls what the user can do after identity is confirmed, not the identity check itself.Final Answer:
Verifying the identity of a user -> Option DQuick Check:
Authentication = Verifying identity [OK]
- Confusing authentication with authorization
- Thinking authentication controls permissions
- Mixing identity verification with data encryption
Solution
Step 1: Identify authorization logic
Authorization checks what actions a user can perform, often by checking roles or permissions likeuser.role == 'admin'.Step 2: Differentiate from authentication checks
Authentication checks credentials like passwords or tokens, not roles.Final Answer:
if user.role == 'admin': -> Option CQuick Check:
Authorization checks roles/permissions [OK]
- Using password checks as authorization
- Confusing session token presence with authorization
- Checking email verification as authorization
def access_resource(user):
if not user.is_authenticated:
return "Access Denied"
if user.permission != 'read':
return "Permission Denied"
return "Resource Content"What will be the output if
user.is_authenticated is True but user.permission is 'write'?Solution
Step 1: Check authentication condition
The user is authenticated (True), so the firstifcondition fails and does not return "Access Denied".Step 2: Check authorization condition
The user's permission is 'write', which is not equal to 'read', so the secondifreturns "Permission Denied".Final Answer:
"Permission Denied" -> Option AQuick Check:
Authenticated but wrong permission = Permission Denied [OK]
- Ignoring permission check and returning resource
- Confusing authentication failure with permission failure
- Assuming 'write' permission equals 'read'
if user.is_authorized:
print("Access granted")
else:
print("Access denied")But the system always denies access even for valid users. What is the likely issue?
Solution
Step 1: Analyze authentication and authorization order
Authorization should only be checked after confirming the user is authenticated. Skipping authentication can cause authorization to fail.Step 2: Understand why access is always denied
Ifis_authorizeddepends on authentication, but authentication is missing, it will always be false or unset, denying access.Final Answer:
The code checks authorization without authenticating the user first -> Option BQuick Check:
Authenticate before authorizing [OK]
- Checking authorization without authentication
- Assuming authorization implies authentication
- Ignoring user object initialization
Solution
Step 1: Understand correct security flow
First, confirm user identity by authentication (e.g., passwords). Then, check what the user is allowed to do by authorization (e.g., roles).Step 2: Evaluate options for security best practice
Authenticate users with passwords, then authorize access based on roles before serving data follows the correct order: authenticate then authorize. Other options either reverse order, skip authorization, or skip authentication improperly.Final Answer:
Authenticate users with passwords, then authorize access based on roles before serving data -> Option AQuick Check:
Authenticate first, then authorize [OK]
- Reversing authentication and authorization order
- Skipping authorization after authentication
- Trusting session tokens without authentication
Ready to prove it?
Pass the test and this pattern moves unknown
