Auth & Security — Quick Reference

Authentication, Authorization, OIDC, JWT, Refresh Tokens & SSO — Complete Reference

🔐

Authentication vs Authorization

Authentication = "Who are you?"  |  Authorization = "What can you do?"
🏨
Hotel Analogy: Authentication is showing your ID at the front desk to prove who you are. Authorization is your room key — it lets you into Room 305 but not Room 410 or the staff lounge. You must authenticate first, then the system decides what you're authorized to access.
Authentication (AuthN)

Verifying Identity

"Prove you are who you claim to be"
  • Username + Password
  • Multi-Factor Authentication (MFA)
  • Biometrics (fingerprint, face)
  • Certificates / Smart Cards
  • SSO via OIDC / SAML
Authorization (AuthZ)

Granting Permissions

"What are you allowed to do?"
  • Role-Based Access (RBAC)
  • Attribute-Based Access (ABAC)
  • OAuth 2.0 scopes & consents
  • API permissions / policies
  • Resource-level access control

Interview Tip

"Authentication always comes first. You can't authorize someone whose identity you haven't verified. For example, a user authenticates via SSO + MFA, then authorization determines whether they can view Project A's data or only Project B's — that's RBAC combined with data-level access control."

🪪

OpenID Connect (OIDC)

OIDC = Authentication layer built ON TOP of OAuth 2.0
🧩
Key Insight: OAuth 2.0 was designed only for authorization (granting access to resources). But apps also needed to know who the user is. OIDC adds an ID Token (a JWT) on top of OAuth so you get both identity AND access in one flow.

How OIDC Works — The Authorization Code Flow

This is the most secure and common flow for web applications:

Step 1
User clicks "Login"
App redirects to IdP
Step 2
User authenticates
At Identity Provider
Step 3
IdP returns code
Auth code to app
Step 4
App exchanges code
Code → Tokens (server-side)
Step 5
Receives tokens
ID Token + Access Token

OIDC vs OAuth 2.0 vs SAML — When to Use What

OAuth 2.0

Authorization framework. Grants third-party apps limited access to user resources WITHOUT sharing passwords.

Use: "Let this app access my Google Drive files"

OIDC

Identity layer on OAuth 2.0. Returns an ID Token (JWT) with user identity. Modern standard for web & mobile.

Use: "Login with Google" — modern apps, APIs, SPAs

SAML 2.0

XML-based, enterprise-focused SSO protocol. Older but still dominant in corporate environments. Heavy payload.

Use: Enterprise SSO — Okta, Active Directory, large orgs

LDAP

Directory protocol for looking up user/group info. Often the backend that SAML/OIDC authenticate against.

Use: Internal user directories, Active Directory queries

Key OIDC Terminology

Identity Provider (IdP)The system that authenticates users — e.g., Azure AD, Okta, Auth0, Google
Relying Party (RP)Your application — it "relies" on the IdP to verify identity
ID TokenA JWT containing user identity claims (name, email, roles). OIDC's key addition to OAuth
Access TokenToken used to call APIs. Can be JWT or opaque. Carries scopes (permissions)
ScopesPermissions requested — openid (required for OIDC), profile, email, custom scopes
ClaimsKey-value pairs inside a token — sub (user ID), name, email, roles
UserInfo EndpointAPI endpoint to fetch additional user profile data using an access token
🎫

JSON Web Token (JWT)

JWT = A self-contained, signed token with 3 parts: Header . Payload . Signature
📛
Boarding Pass Analogy: A JWT is like a boarding pass. The header says what type of pass it is. The payload has your name, seat, flight details. The signature is the airline's stamp proving it's genuine. Anyone can read the pass, but only the airline can issue a valid one. If someone tampers with the seat number, the stamp won't match.
eyJhbGciOiJSUzI1NiJ9 . eyJzdWIiOiIxMjM0NTY3ODkwIn0 . SflKxwRJSMeKKF2QT4fw

Header

Algorithm (RS256, HS256) and token type (JWT). Tells the server how to verify the signature.

Payload (Claims)

User data: sub (user ID), name, email, roles, exp (expiry), iss (issuer). NOT encrypted — only Base64 encoded!

Signature

HMAC or RSA signature of Header + Payload. Proves the token hasn't been tampered with. Server verifies using secret/public key.

Critical Things to Remember About JWTs

NOT encryptedAnyone can decode and read the payload (it's just Base64). Never put sensitive data like passwords in a JWT. Use JWE if you need encryption.
Signed, not secretThe signature proves integrity (no tampering) and authenticity (issued by trusted party), not confidentiality.
StatelessServer doesn't need to store sessions — the token itself contains all needed info. This is why JWTs scale well for microservices.
Can't be revoked easilyOnce issued, a JWT is valid until it expires. This is why access tokens should be short-lived (5-15 min). For immediate revocation, you need a blocklist.
RS256 vs HS256HS256: shared secret (symmetric) — both sides know the key. RS256: public/private key pair (asymmetric) — anyone can verify, only issuer can sign. RS256 is preferred for distributed systems.

Interview Tip

"JWTs are ideal for microservices architectures because they're stateless — each service can independently verify the token without calling a central session store. But the trade-off is revocation: if a user's access should be revoked immediately (like a terminated employee), you need either very short token lifetimes or a token blocklist. As a PM, I'd push for short-lived access tokens (5-15 min) with refresh token rotation for any security-sensitive application."

🔄

Token Lifecycle & Refresh Strategy

Access Token = short-lived key to APIs  |  Refresh Token = long-lived key to get new access tokens
🎟️
Theme Park Analogy: Your access token is a wristband that expires at 5 PM. Your refresh token is a receipt that lets you go to the kiosk and get a new wristband without buying another ticket. If you lose the receipt (refresh token compromised), someone else could get wristbands. That's why refresh token rotation exists — each time you use the receipt, you get a new receipt too, and the old one is invalidated.

Access Token

PurposeAuthorize API requests
Lifetime5–15 minutes (short!)
StorageIn-memory (browser) or HTTP-only cookie
FormatUsually JWT (self-contained)
Sent viaAuthorization: Bearer <token> header
If stolenLimited damage due to short expiry

Refresh Token

PurposeGet new access tokens silently
LifetimeHours to days (7–30 days common)
StorageHTTP-only secure cookie or server-side
FormatOpaque string (not JWT typically)
Sent toToken endpoint only — never to APIs
If stolenMajor risk — use rotation + detection

Refresh Token Flow

Step 1
API call with Access Token
Authorization: Bearer xyz
Step 2
401 Unauthorized
Access token expired
Step 3
Send Refresh Token
POST /token (grant_type=refresh_token)
Step 4
New tokens issued
New Access + Rotated Refresh

Refresh Token Rotation — Why It Matters

With rotation, every time a refresh token is used, it's invalidated and a new one is issued. If an attacker steals and uses the old refresh token, the server detects the reuse (since it was already consumed) and invalidates the entire token family — forcing re-authentication.

This is mandatory in security-sensitive applications (finance, healthcare, enterprise SaaS) because it provides stolen token detection — a critical security control for compliance.

🔗

Single Sign-On (SSO)

SSO = One login, access to many applications — authenticate once, authorized everywhere
🏢
Office Building Analogy: SSO is like your office badge. You swipe it once at the building entrance (authenticate). Then you can access the cafeteria, gym, meeting rooms, and your floor — all without swiping again. Each facility (application) trusts the building's security system (IdP) to verify you're an employee.
💼

Portfolio App

Trusts the IdP

Identity Provider
Azure AD / Okta / Ping
Login once here ↕
📊

Risk Dashboard

Trusts the IdP

One authentication → Access to all connected apps without re-entering credentials

SSO Protocols in Practice

SAML-based SSOMost common in enterprises. IdP sends a signed XML assertion to the app. Used by Azure AD, Okta for corporate apps. Standard in large organizations.
OIDC-based SSOModern alternative. IdP issues JWT tokens. Used by Google, Auth0. Better for APIs and SPAs. Lighter than SAML.
KerberosWindows domain authentication. Users authenticate once to Active Directory, then access any domain resource silently. Common in on-premises enterprise environments.

SSO Benefits & Risks

Benefits
  • One password to remember — fewer password-related support tickets (reduces 30-50% of helpdesk calls)
  • Centralized access control — disable one account, revoke access to all apps instantly
  • Better security posture — can enforce MFA at one point
  • Improved UX — seamless navigation between apps
  • Compliance — centralized audit trail of who accessed what
Risks to Manage
  • Single point of failure — if IdP goes down, no one can log in (need HA for IdP)
  • Blast radius — one compromised account accesses everything (must enforce MFA)
  • Session management complexity — how long should SSO session last?
  • Vendor lock-in — tightly coupled to IdP vendor
  • Federation complexity — cross-organization SSO adds trust management overhead
🎬

Animated Auth Workflow

Watch how Authentication, Authorization, Tokens & Refresh work together end-to-end
Full Login Flow
API Call with JWT
Token Refresh
👤
User
End User
💻
Client App
Web / SPA / Mobile
🛡️
Identity Provider
Azure AD / Okta
API Gateway
Azure API Mgmt
🗄️
Resources
Database / Service
Auth Code
ID Token (JWT)
Access Token (JWT)
Refresh Token
Speed: Normal

Step-by-Step Explanation

🌍

Real-World Auth Patterns

How Auth Works Across Different Application Types

Auth patterns vary by architecture. Here's how these concepts apply in practice:

  • SPA (React / Angular): Uses OIDC Authorization Code + PKCE (no client secret in browser). Access tokens stored in-memory (never localStorage). Silent refresh via hidden iframe or refresh token rotation. API calls use Bearer header.
  • Mobile Apps: Same PKCE flow as SPAs. Refresh tokens stored in secure OS storage (Keychain / Keystore). Biometric auth for step-up. Deep link redirects for SSO with the system browser.
  • Microservices (Service-to-Service): OAuth 2.0 Client Credentials flow — no user involved. Each service gets its own access token with minimal scopes (least privilege). API Gateway validates JWTs centrally and enforces rate limits.
  • Enterprise Internal Apps: SSO via SAML or OIDC with corporate IdP (Azure AD, Okta). RBAC for role-based permissions. Often combined with network-level security (VPN, Zero Trust). Kerberos for legacy on-prem systems.
  • Multi-Tenant SaaS: Tenant ID embedded as a JWT claim. Authorization checks both role AND tenant to prevent cross-tenant data leaks. Separate signing keys per tenant for isolation. Token scopes restrict API surface per subscription tier.

Common Security Mistakes to Avoid

Storing JWTs in localStorageVulnerable to XSS attacks. Use HTTP-only cookies or in-memory storage instead.
Long-lived access tokensIf stolen, attacker has extended access. Keep access tokens short (5-15 min), use refresh tokens for longevity.
No refresh token rotationWithout rotation, a stolen refresh token is a permanent backdoor. Always rotate + detect reuse.
Trusting JWT payload without verifying signatureAnyone can forge a JWT payload. Always verify the cryptographic signature using the IdP's public key.
Using HS256 in distributed systemsShared secret means every service can forge tokens. Use RS256 (asymmetric) — only the IdP can sign, anyone can verify.
Missing audience (aud) validationA token meant for Service A could be replayed against Service B. Always validate aud claim matches your API.
📋

Quick-Fire Cheat Sheet

Terms You Must Know — One Liners

AuthenticationVerifying who you are (identity proof)
AuthorizationDetermining what you can access (permission check)
OAuth 2.0Authorization framework — grants scoped access to resources without sharing passwords
OIDCIdentity layer on OAuth 2.0 — adds ID Token (JWT) so you know WHO the user is
SAMLXML-based enterprise SSO protocol — older but dominant in corporate environments
JWTSelf-contained signed token: Header.Payload.Signature — stateless, not encrypted
Access TokenShort-lived (5-15 min), used to call APIs, usually a JWT
Refresh TokenLong-lived, used only to get new access tokens, should be rotated
ID TokenJWT with user identity claims — OIDC specific, never sent to APIs
SSOLogin once, access multiple apps — uses SAML or OIDC under the hood
MFAMulti-factor authentication — something you know + have + are
RBACRole-Based Access Control — permissions assigned to roles, users get roles
ABACAttribute-Based Access Control — rules based on user/resource/context attributes
ScopePermission boundary in OAuth — defines what an access token can do
ClaimKey-value pair inside a JWT — sub, name, email, roles, exp
IdPIdentity Provider — Azure AD, Okta, Auth0 — the system that authenticates users
PKCEProof Key for Code Exchange — security extension for OAuth in public clients (SPAs, mobile)
Token RotationIssue new refresh token on each use, invalidate old one — detects theft
Step-up AuthRequire stronger authentication for sensitive operations (e.g., wire transfer needs biometric even if already logged in)
Zero Trust"Never trust, always verify" — every request is authenticated and authorized regardless of network location
🎯

Interview Questions & Answers

Click any question to reveal the answer. Covers concepts interviewers frequently test.
BASIC

What is the difference between authentication and authorization?

+

Authentication (AuthN) verifies who you are — it's the process of proving your identity. Examples: entering a password, scanning a fingerprint, or completing MFA.

Authorization (AuthZ) determines what you can do — it's the process of checking permissions after identity is confirmed. Examples: RBAC (role-based access control), OAuth scopes, API permissions.

Key point: Authentication always happens first. You must establish identity before the system can check permissions. A common analogy: authentication is showing your ID at the door; authorization is the access badge that lets you into specific rooms.

BASIC

What is OAuth 2.0 and how does it differ from OIDC?

+

OAuth 2.0 is an authorization framework. It allows a third-party app to access a user's resources without knowing their password. Example: "Allow this app to access your Google Drive files." OAuth issues an Access Token but does NOT tell the app who the user is.

OIDC (OpenID Connect) is an identity layer built on top of OAuth 2.0. It adds an ID Token (a JWT) that contains user identity information (name, email, roles). So you get both: who the user is (AuthN) AND scoped access (AuthZ).

Common mistake in interviews: Saying "OAuth handles login." OAuth is NOT an authentication protocol — it's authorization only. OIDC is what handles login by extending OAuth with identity.

INTERMEDIATE

Can you explain the structure of a JWT and how it is validated?

+

A JWT has three Base64-encoded parts separated by dots: Header.Payload.Signature

Header: Contains the algorithm (RS256, HS256) and token type. Tells the verifier how to check the signature.

Payload: Contains claims — key-value data like sub (user ID), exp (expiry), iss (issuer), aud (audience), roles. This is NOT encrypted — anyone can decode it. Never put secrets here.

Signature: Cryptographic hash of Header + Payload using a secret key (HS256) or private key (RS256). Proves the token hasn't been tampered with.

Validation steps: (1) Decode the header to find the algorithm. (2) Verify the signature using the IdP's public key (for RS256). (3) Check claims: exp not expired? iss from trusted issuer? aud matches our API? If any check fails → reject with 401.

Important: JWTs are signed, not encrypted. The signature proves integrity and authenticity, not confidentiality. If you need encryption, use JWE (JSON Web Encryption).

INTERMEDIATE

What is PKCE and why was it introduced?

+

PKCE (Proof Key for Code Exchange) is a security extension for the OAuth Authorization Code flow. It prevents authorization code interception attacks.

The problem: In public clients (SPAs, mobile apps), there's no client secret — the code lives in the browser or on the device. If an attacker intercepts the authorization code (via a malicious app or redirect), they could exchange it for tokens.

How PKCE solves this: (1) The app generates a random code_verifier (kept secret, in memory). (2) It computes code_challenge = SHA256(code_verifier) and sends the challenge with the auth request. (3) When exchanging the code for tokens, the app sends the original code_verifier. (4) The IdP computes SHA256 of the verifier and checks it matches the original challenge. If it doesn't match → the exchange is rejected.

Key insight: Even if an attacker steals the authorization code, they can't exchange it because they don't have the code_verifier that was never transmitted over the network — only its hash was. PKCE is now recommended for ALL OAuth clients, not just public ones.

INTERMEDIATE

What is the difference between an ID Token and an Access Token?

+

ID Token: Issued by OIDC. Contains user identity claims (who you are): sub, name, email, roles. It's always a JWT. The client app consumes it to know who logged in. Never send it to an API — it's meant for the client only.

Access Token: Issued by OAuth/OIDC. Contains authorization claims (what you can do): scope, aud, exp. Used to call APIs via Authorization: Bearer <token>. Can be JWT or opaque. Sent to APIs, never consumed by the client for identity.

A common interview trap: "Can you use an Access Token to get user info?" Technically the token might contain user claims, but the correct answer is: the ID Token is for identity, the Access Token is for API access. If you need user info at the API, the API reads claims from the Access Token or calls the UserInfo endpoint — it should never receive the ID Token.

INTERMEDIATE

Why shouldn't you store JWTs in localStorage?

+

localStorage is accessible to any JavaScript running on the page. If your app has an XSS vulnerability (Cross-Site Scripting) — even in a third-party library — an attacker's script can read the JWT from localStorage and send it to their server. Game over: they now have your access token.

Better alternatives:

(1) In-memory storage: Store tokens in a JavaScript variable. They're cleared on page refresh, but that's actually a security feature — combine with silent refresh or refresh tokens for persistence.

(2) HTTP-only, Secure, SameSite cookies: The browser automatically sends them with requests, and JavaScript cannot access them — immune to XSS. Set Secure (HTTPS only), HttpOnly (no JS access), and SameSite=Strict (CSRF protection).

Note: sessionStorage has the same XSS vulnerability as localStorage — it's not a safe alternative.

ADVANCED

How does refresh token rotation work and why is it important?

+

With refresh token rotation, every time a refresh token is used, the server invalidates it and issues a new refresh token along with the new access token. The old refresh token can never be used again.

Why it matters — theft detection: Suppose an attacker steals a refresh token. Two scenarios:

(1) Attacker uses it first: They get new tokens. When the legitimate user tries to use the (now-invalidated) original refresh token, the server detects reuse of a consumed token → revokes the entire token family (all tokens in that chain). Attacker is also kicked out.

(2) Legitimate user uses it first: The token is rotated. When the attacker tries to use the stolen (old) token → reuse detected → entire family revoked.

Either way, compromise is detected. Without rotation, a stolen refresh token is a permanent backdoor — the attacker can silently generate new access tokens indefinitely.

This is why modern security best practices (and the OAuth 2.0 Security Best Current Practice RFC) recommend rotation for all refresh tokens, especially for public clients.

ADVANCED

What is the difference between HS256 and RS256? When would you use each?

+

HS256 (HMAC-SHA256): Symmetric algorithm. A single shared secret is used to both sign and verify the token. Fast, simple, but the secret must be shared with every service that needs to verify tokens.

RS256 (RSA-SHA256): Asymmetric algorithm. A private key signs the token (only the IdP has this). A public key verifies it (anyone can have this). The public key is published at the IdP's /.well-known/jwks.json endpoint.

When to use HS256: Single-service architectures where the same server issues and verifies tokens. Simple setup, no key distribution needed.

When to use RS256: Distributed systems, microservices, any architecture where multiple services need to verify tokens. Since only the IdP holds the private key, no other service can forge tokens — even if they have the public key. This is the standard for production systems.

Security risk of HS256 in distributed systems: If you share the HS256 secret with 10 microservices, any one of them can forge tokens for any user. With RS256, only the IdP can sign — the services can only verify.

ADVANCED

How would you revoke a JWT that hasn't expired yet?

+

This is one of JWT's fundamental trade-offs. Since JWTs are stateless — the server doesn't track issued tokens — there's no built-in revocation mechanism. A valid, unexpired JWT will be accepted until exp passes.

Strategies for revocation:

(1) Short-lived access tokens (recommended): Set expiry to 5-15 minutes. Even if stolen, the window of exploitation is small. Pair with refresh tokens for session longevity.

(2) Token blocklist/denylist: Maintain a server-side list of revoked token IDs (jti claim). Check every incoming token against this list. Downside: you've reintroduced server-side state, which defeats the stateless advantage. Use a fast store like Redis to minimize latency.

(3) Token versioning: Store a "token version" per user in your database. Embed the version in the JWT. On revocation, increment the version. Tokens with the old version are rejected. Only requires one DB lookup per user, not per token.

(4) Revoke the refresh token: You can't recall an access token in the wild, but you can revoke its refresh token so no new access tokens are issued. Combined with short access token lifetimes, the old token expires naturally within minutes.

The practical answer: Most production systems combine strategy 1 + 4: short access tokens + refresh token revocation. The blocklist approach (strategy 2) is used only for critical scenarios like immediate employee termination.

BASIC

What is Single Sign-On (SSO) and what are its security trade-offs?

+

SSO allows users to authenticate once with a central Identity Provider and then access multiple applications without logging in again. Under the hood, it uses protocols like SAML 2.0 or OIDC.

Benefits: Fewer passwords to remember (reduces password fatigue and reuse), centralized access control (disable one account → revoke access everywhere), single place to enforce MFA, comprehensive audit trail of who accessed what, and reduced help desk tickets for password resets.

Trade-offs:

(1) Single point of failure: If the IdP goes down, no one can log into any app. Mitigation: high-availability IdP deployment.

(2) Increased blast radius: One compromised account gives access to everything. Mitigation: mandatory MFA, anomaly detection, step-up auth for sensitive operations.

(3) Session management complexity: How long should the SSO session last? Too long = security risk. Too short = users re-authenticate constantly. Most systems use sliding sessions with idle timeout.

INTERMEDIATE

What is a CSRF attack and how do OAuth/OIDC protect against it?

+

CSRF (Cross-Site Request Forgery) tricks a user's browser into making an unintended request to a site where they're already authenticated. Example: a malicious site includes an image tag that triggers a bank transfer on a site where you're logged in.

In the OAuth/OIDC context, a CSRF attack could trick the app into exchanging a malicious authorization code — the attacker initiates an OAuth flow and somehow gets the victim's app to complete it, linking the attacker's account.

Protection mechanisms:

(1) State parameter: The app generates a random state value, includes it in the auth request, and verifies it matches when the callback returns. An attacker can't predict or forge this value.

(2) PKCE: Also prevents this — the code_verifier is bound to the session that started the flow.

(3) SameSite cookies: Set SameSite=Strict or Lax on auth cookies so they're not sent with cross-origin requests.

ADVANCED

What is the difference between SAML and OIDC? When would you choose one over the other?

+

SAML 2.0: XML-based, enterprise SSO protocol from 2005. Uses XML assertions signed with X.509 certificates. Communicated via browser redirects and POST bindings. Heavyweight payload. Mature, battle-tested in enterprise environments.

OIDC: JSON-based, modern identity protocol built on OAuth 2.0 (2014). Uses JWTs. Lightweight, designed for web, mobile, and APIs. Easier to implement and debug.

Choose SAML when: Integrating with legacy enterprise systems that only support SAML (many corporate IdPs like legacy Active Directory). Federated identity across organizations (B2B). Compliance requirements that mandate SAML.

Choose OIDC when: Building modern web/mobile apps. Need API authentication (SAML doesn't work well with APIs). Want simpler implementation. Working with cloud-native services. Need a lighter payload.

In practice: Most modern IdPs (Azure AD, Okta, Auth0) support both. New applications almost always use OIDC. SAML remains dominant in enterprise SSO federations where switching would be costly. Some organizations use both — SAML for legacy apps, OIDC for new ones — with the same IdP.

ADVANCED

A JWT's Access Token says roles: ["admin"] but the user was demoted 5 minutes ago. What happens and how do you handle it?

+

The user retains admin access until the JWT expires. This is the fundamental trade-off of stateless tokens — the JWT was signed with roles: ["admin"] at issuance time, and no one can change it after the fact without invalidating the signature.

Mitigation strategies (from least to most complex):

(1) Short token lifetimes: If access tokens expire in 5 minutes, the stale role persists for at most 5 minutes. This is usually acceptable.

(2) Force refresh: When a role changes, invalidate the user's refresh token. On the next refresh cycle (within minutes), the new token will have the updated role. The old access token expires naturally.

(3) Hybrid check: For sensitive operations (delete, admin actions), the API does a real-time permission check against the database, ignoring the JWT's role claim. Regular read operations still use the JWT's cached role for performance.

(4) Token blocklist: Add the old token's jti to a Redis blocklist. All services check this on every request. Most invasive but provides immediate revocation.

The "right" answer: It depends on your security requirements. For most systems, short TTL + forced refresh (1+2) is the sweet spot. For admin/delete operations, add the hybrid check (3) as a safety net.

INTERMEDIATE

What is Zero Trust architecture and how does auth fit into it?

+

Zero Trust is a security model based on the principle: "never trust, always verify." Unlike traditional security (trust everything inside the network perimeter), Zero Trust assumes every request could be malicious — regardless of whether it comes from inside or outside the network.

How auth supports Zero Trust:

(1) Every request is authenticated: No implicit trust based on network location. Even internal microservice-to-microservice calls must carry valid tokens (mTLS or OAuth client credentials).

(2) Every request is authorized: Fine-grained, context-aware access control. Not just "is this a valid user?" but "should this user access this resource, from this device, at this time, from this location?"

(3) Least privilege: Tokens carry only the minimum scopes needed. A service that only needs to read data should never receive a token with write permissions.

(4) Continuous verification: Don't just authenticate once — re-verify throughout the session. Short-lived tokens force periodic re-validation. Step-up auth for sensitive actions.

Practical examples: Google's BeyondCorp is a Zero Trust implementation. Azure AD Conditional Access evaluates device compliance, location, and risk level on every sign-in — even for users inside the corporate network.

ADVANCED

How would you secure service-to-service communication in a microservices architecture?

+

When there's no user involved (e.g., Service A calls Service B on a schedule), you need machine-to-machine authentication. The main approaches:

(1) OAuth 2.0 Client Credentials flow: Each service has its own client_id and client_secret (or certificate). It authenticates directly with the IdP and receives an access token scoped to what that service needs. The receiving service validates the JWT as normal. This is the most common approach in cloud environments.

(2) Mutual TLS (mTLS): Both sides present TLS certificates. The connection itself is authenticated — you know which service is calling at the transport layer. Often used in service meshes (Istio, Linkerd). Can be combined with JWT for authorization claims.

(3) API Keys: Simplest but weakest. A shared secret sent as a header. No expiry, no scoping, difficult to rotate. Acceptable only for low-risk internal services or as a supplementary check alongside other mechanisms.

Best practices: Use client credentials flow with short-lived JWTs. Each service gets its own identity (not a shared account). Apply least-privilege scoping — Service A's token only grants access to the specific endpoints it needs on Service B. Rotate credentials regularly. Log all service-to-service calls for audit trails.