Authentication vs. Authorization
As a Technical Lead, it's vital to distinguish between the two: Authentication (AuthN) is the process of verifying who a user is (e.g., checking a password). Authorization (AuthZ) is verifying what they are allowed to do. In modern Python architecture, Authentication is the first gate a request must pass through before hitting your business logic.
- Session-Based Authentication
Traditional web applications use stateful sessions. The server creates a session in a database or cache (like Redis) and sends a unique Session ID to the user's browser via a cookie. This is the battle-tested default for frameworks like Django.
- Pros: Simple to implement for web, easy to revoke sessions immediately from the server.
- Cons: Harder to scale horizontally (requires shared session storage), not native to mobile platforms like Flutter.
- Token-Based Authentication (JWT)
JSON Web Tokens (JWT) are the standard for modern APIs and mobile backends. They are stateless, meaning the server doesn't need to store session data. The 'identity' is encoded and signed within the token itself.
- OAuth2 and OpenID Connect (OIDC)
For enterprise applications or 'Login with Social' features, OAuth2 is the industry-standard protocol. Python libraries like Authlib or PyOIDC handle the complex handshakes between your app and providers like Google, GitHub, or Microsoft.
Authentication Strategy Comparison
| Method | State | Storage | Best For |
|---|---|---|---|
| Basic Auth | Stateless | Header (Base64) | Simple internal scripts/CLI |
| Sessions | Stateful | Server DB / Cookies | Standard Web Apps (Monoliths) |
| JWT / Tokens | Stateless | Client Storage / Headers | Mobile Apps (Flutter) & SPAs |
| OAuth2 / OIDC | Delegated | External Provider | SSO & Third-party integrations |