1.Authentication vs authorization: who is the caller, what can they do
Authentication proves who is making a request — a login, a token, a certificate. Authorization decides what that already-proven identity is allowed to do. A system can get the first right and still be broken if it never checks the second.
Key features
- Authentication answers "who are you" — login, session, token, certificate, API key are all authentication mechanisms
- Authorization answers "what are you allowed to do" — a role, a permission, an ownership check, evaluated after identity is already known
- Authorization always runs after authentication logically, even when both happen inside one middleware call — you cannot decide what an unknown caller may do
- A request can be fully authenticated (a valid, unexpired token from a real user) and still be correctly rejected by authorization (that real user has no permission for this specific action)
- Mixing the two up is a common source of security bugs: code that checks "is this token valid" and treats that as equivalent to "is this action allowed" skips the authorization step entirely
Authentication and authorization compared
- Client → API: Request + credentials/token
- API → Auth check: Who is this? (authentication)
- Auth check → API: user_id 482 (identity confirmed)
- API → Authz check: Can 482 delete invoice 900? (authorization)
- Authz check → API: No — not the owner
- API → Client: 403 Forbidden
Command
# Authentication: establishes identity
user = authenticate(request.token) # -> User(id=482) or reject (401)
# Authorization: checks what that identity may do
if not authorize(user, action='delete', resource=invoice):
return 403 # known caller, disallowed actionAuthentication proves identity (who); authorization decides permission (what they can do with that identity). They are two separate checks, not one — a valid token only ever answers the first question.