Sathsara K.
  • Home
  • About
  • Projects
  • Case Studies
  • Blog
  • Contact
Hire me
  • Home
  • About
  • Projects
  • Case Studies
  • Blog
  • Contact
Sathsara K.
© 2026 Sathsara. Built with intent.
Back to Blog
Authentication vs Authorization: Security Best Practices
Software EngineeringJuly 11, 20266 min read

Authentication vs Authorization: Security Best Practices

Learn the key differences between authentication and authorization, plus security best practices for passwords, sessions, JWTs, and access control.

Table of Contents

  • • Authentication vs Authorization: The Core Difference
  • • Authentication: Best Practices
  • ↳ 1. Never store passwords in plaintext or with fast hashes
  • ↳ 2. Enforce multi-factor authentication (MFA)
  • ↳ 3. Get session management right
  • ↳ 4. If you're using JWTs, know the common pitfalls
  • • Authorization: Best Practices
  • ↳ 1. Apply the principle of least privilege
  • ↳ 2. Choose the right access control model
  • ↳ 3. Enforce authorization on every layer, not just the UI
  • ↳ 4. Watch for Insecure Direct Object References (IDOR)
  • • Practices That Apply to Both
  • • Closing Thought

Authentication vs Authorization: Security Best Practices

Every backend engineer eventually gets this wrong at least once: confusing who you are with what you're allowed to do. Authentication and authorization solve two different problems, and mixing them up is one of the most common sources of security vulnerabilities in production systems.

This guide breaks down both concepts and covers the practices that actually hold up in real-world, high-traffic systems, not just tutorial code.

Authentication vs Authorization: The Core Difference

Authentication (AuthN) answers: Is this really you?

It verifies identity, usually through a password, token, biometric, or certificate.

Authorization (AuthZ) answers: What are you allowed to do?

It governs access to resources once identity is confirmed.

A login form is authentication. A 403 Forbidden response is authorization. Systems that conflate the two often end up with logic like "if the user is logged in, they can access anything," which is how privilege escalation bugs happen.

Authentication: Best Practices

1. Never store passwords in plaintext or with fast hashes

MD5 and SHA-256 are fast by design, which makes them terrible for password storage; attackers can brute-force billions of hashes per second on modern GPUs. Use a slow, memory-hard algorithm instead:

  • Argon2id (recommended by OWASP as the current standard)
  • bcrypt (still solid, widely supported)
  • scrypt (good alternative, memory-hard)
  • Always use a unique salt per user (these libraries handle this automatically) and tune the work factor to your hardware, not a default from 2015.

    2. Enforce multi-factor authentication (MFA)

    Passwords alone are compromised constantly through phishing, credential stuffing, and data breaches. MFA should be:

  • Mandatory for admin and privileged accounts, optional-but-encouraged for regular users
  • Based on TOTP (Google Authenticator, Authy) or WebAuthn/FIDO2 (hardware keys, platform biometrics) rather than SMS, since SMS is vulnerable to SIM-swapping
  • Backed by recovery codes generated at enrollment, stored by the user, not emailed on request
  • 3. Get session management right

    If you're using session-based auth:

  • Regenerate the session ID after login to prevent session fixation attacks
  • Set HttpOnly, Secure, and SameSite=Strict (or Lax) on session cookies
  • Enforce idle timeouts and absolute session expiration, not just "logged in forever until they clear cookies"
  • Invalidate sessions server-side on logout, don't just delete the client-side cookie
  • 4. If you're using JWTs, know the common pitfalls

    JWTs are popular for stateless auth in microservice architectures, but they introduce their own risk surface:

  • Never accept `alg: none` in the token header. This was a real vulnerability class where attackers stripped the signature and servers accepted it.
  • Pin the expected algorithm server-side. Don't trust the alg field from the token itself; if you expect RS256, reject anything else, including HS256, to prevent algorithm confusion attacks.
  • Keep expiration short. Access tokens should live minutes, not days. Use short-lived access tokens with a separate, securely stored refresh token for renewal.
  • Have a revocation strategy. JWTs are stateless by design, which means you can't easily invalidate one before expiry. Maintain a blocklist (in Redis, for example) for tokens that must be revoked immediately, such as after a password reset or account compromise.
  • Don't put sensitive data in the payload. JWT payloads are base64-encoded, not encrypted. Anyone can decode and read them.
  • Authorization: Best Practices

    1. Apply the principle of least privilege

    Every user, service account, and API key should have the minimum permissions needed to do its job, nothing more. This limits blast radius when credentials are compromised. In event-driven systems with something like RabbitMQ, this also means scoping queue and exchange permissions per service rather than giving every consumer blanket access to the broker.

    2. Choose the right access control model

  • RBAC (Role-Based Access Control): Good for systems with clear, stable roles (admin, editor, viewer). Simple to reason about and audit.
  • ABAC (Attribute-Based Access Control): Better when permissions depend on context, like resource ownership, time of day, or department. More flexible, but harder to audit at scale.
  • ReBAC (Relationship-Based Access Control): Used when permissions depend on relationships between entities, such as "can edit if owner or shared collaborator." Google Zanzibar-style systems use this model.
  • Pick based on actual complexity. Don't reach for ABAC or ReBAC if RBAC solves your problem; added flexibility means added attack surface and more places to get the logic wrong.

    3. Enforce authorization on every layer, not just the UI

    Hiding a button in the frontend is not access control. Every API endpoint must independently verify that the authenticated user is authorized for that specific action on that specific resource. This is especially critical in microservice architectures: don't assume that because a request came from an internal service, it's already been authorized upstream. Verify at each service boundary.

    4. Watch for Insecure Direct Object References (IDOR)

    This is one of the most common real-world authorization bugs. If /api/orders/1234 returns order 1234 regardless of who's asking, that's an IDOR vulnerability. Always check that the resource being requested actually belongs to, or is permitted for, the authenticated user, not just that the ID exists.

    Practices That Apply to Both

  • Rate limit and monitor authentication endpoints. Brute-force and credential-stuffing attacks target login and password-reset endpoints specifically. Add rate limiting, CAPTCHA after repeated failures, and alerting on unusual login patterns.
  • Log security-relevant events, including failed logins, permission denials, and privilege changes, without logging sensitive data like passwords or full tokens.
  • Follow OWASP's Top 10, since "Broken Access Control" and "Identification and Authentication Failures" have consistently ranked among the most common and highest-impact vulnerability categories in production applications.
  • Rotate secrets and keys used for signing tokens or encrypting sessions on a defined schedule, and immediately after any suspected compromise.
  • Closing Thought

    Authentication and authorization are often built once, early in a project, and then rarely revisited until something breaks. Treat them as living parts of the system: review access control logic whenever new roles or resource types are added, and revisit token and session policies as the system scales. The teams that get burned are usually the ones who treated auth as a solved problem after the initial implementation.

    All ArticlesSoftware Engineering
    Sathsara

    Sadeesha Sathsara

    Backend & DevOps Developer

    Writing on async systems design, automated CI/CD pipelines, containerization, and backend patterns.

    More in Software Engineering

    What Is Identity and Access Management (IAM)? A Beginner's ExplanationJuly 19, 2026 · 4 min readAre Roles the Same as Identities in IAM? A Clear BreakdownJuly 19, 2026 · 9 min read

    Keep Reading

    More Articles

    What Is Identity and Access Management (IAM)? A Beginner's Explanation
    ArticleSoftware Engineering
    July 19, 2026

    What Is Identity and Access Management (IAM)? A Beginner's Explanation

    Read
    Are Roles the Same as Identities in IAM? A Clear Breakdown
    ArticleSoftware Engineering
    July 19, 2026

    Are Roles the Same as Identities in IAM? A Clear Breakdown

    Read
    KPIs Explained: The Dashboard Numbers That Actually Run a Business
    ArticleBusiness
    July 3, 2026

    KPIs Explained: The Dashboard Numbers That Actually Run a Business

    Read
    Data Visualization Is Not Decoration, It Is Business Storytelling
    ArticleBusiness
    July 3, 2026

    Data Visualization Is Not Decoration, It Is Business Storytelling

    Read
    How Money Really Flows Through a Business
    ArticleBusiness
    July 3, 2026

    How Money Really Flows Through a Business

    Read
    What Is Data, Really? Understanding How Businesses Turn Real Life Into Records
    ArticleBusiness
    July 3, 2026

    What Is Data, Really? Understanding How Businesses Turn Real Life Into Records

    Read
    Data Analysis Is Not About Tools, It Is About Reading a Business Through Its Data
    ArticleBusiness
    July 3, 2026

    Data Analysis Is Not About Tools, It Is About Reading a Business Through Its Data

    Read
    Building Async CV Microservices with RabbitMQ & OpenCV
    ArticleMicroservices
    June 30, 2026

    Building Async CV Microservices with RabbitMQ & OpenCV

    Read
    From Monolith to Event-Driven Microservices: An Architectural Blueprint
    ArticleArchitecture
    June 28, 2026

    From Monolith to Event-Driven Microservices: An Architectural Blueprint

    Read
    Stop SSH-ing Into Production: Build Nginx & Docker CI/CD
    ArticleDevOps
    May 12, 2026

    Stop SSH-ing Into Production: Build Nginx & Docker CI/CD

    Read