How do you implement secure auth & authorization in web apps?
Web Security Engineer
answer
I secure authentication with OAuth2/OIDC flows, ensuring short-lived access tokens and refresh tokens protected with rotation. JWTs are signed (RS256/ES256), carry minimal claims, and expire quickly; sensitive data is never stored in them. For authorization, I apply RBAC/ABAC with least privilege and scope-limited tokens. MFA strengthens high-risk flows, using TOTP, WebAuthn, or push notifications. All tokens are validated server-side, logged, and revoked on anomaly.
Long Answer
Authentication and authorization are critical layers of defense in any web application. My approach is based on proven standards, least privilege, and layered protections that extend from token issuance to runtime monitoring.
1) Authentication with OAuth2/OIDC
I use OAuth2.0 for delegated access and OpenID Connect for identity. Depending on the client:
- Authorization Code with PKCE is the default for SPAs and mobile apps to prevent interception.
- Client Credentials flow is reserved for service-to-service calls.
- Tokens are always short-lived and transmitted only over HTTPS. Refresh tokens are rotated: when used, they return a new pair and invalidate the old one. This stops replay if a token leaks.
OIDC adds an ID token signed by the identity provider. I validate issuer, audience, nonce, and expiry. Session fixation is prevented by binding sessions to tokens, and logout revokes tokens across apps.
2) JWT security principles
JWTs (access/ID tokens) are powerful but risky if misused. My practices:
- Use asymmetric algorithms (RS256/ES256) with keys managed in HSM/KMS.
- Keep payloads minimal: no PII or secrets. Store only IDs and claims like roles/scopes.
- Set short expirations (minutes, not hours).
- Validate iss, aud, exp, and signature on every request.
- Avoid none or weak algorithms; reject tokens with mismatched headers.
- Use jti (token ID) and server-side allowlists/blacklists for revocation.
- For cookies, mark HttpOnly, Secure, SameSite=Strict/Lax.
3) Authorization strategies
Authorization follows authentication and enforces least privilege. I combine:
- Role-Based Access Control (RBAC): roles like admin, editor, user.
- Attribute-Based Access Control (ABAC): rules based on department, device, time, or risk.
- Scopes in OAuth2 tokens: ensuring apps can only access what they need.
- Multi-tenancy isolation: enforce tenant IDs at the database/query level to prevent cross-leaks.
Authorization decisions are logged and audited for compliance (GDPR, HIPAA, PCI).
4) Multi-Factor Authentication (MFA)
Passwords alone are weak. I enforce MFA for critical flows (login, payments, admin access). Options include:
- TOTP (Google Authenticator, Authy).
- WebAuthn/FIDO2 with hardware keys or biometrics.
- Push-based MFA via secure apps.
MFA policies are adaptive: required for risky logins (new device, unusual location). Recovery paths are secure, using backup codes or verified devices only.
5) Protecting sessions and tokens
Sessions and tokens are hardened by:
- Storing refresh tokens server-side or in secure HTTP-only cookies.
- Rotating keys regularly; publishing via JWKS for validation.
- Enforcing inactivity and absolute session timeouts.
- Revoking tokens immediately if anomalies (IP drift, device mismatch) are detected.
6) Common web vulnerabilities
I align with OWASP ASVS:
- Prevent CSRF with state parameters and SameSite cookies.
- Apply rate limiting and CAPTCHA for brute-force login attempts.
- Monitor for replay attacks by binding tokens to client context (device, IP).
- Encrypt data at rest and in transit (TLS1.2+).
7) Monitoring and governance
Logs capture successful and failed auth events, token use, and anomalies. Alerts trigger on repeated failures, impossible travel, or massive token requests. Security audits review OAuth scopes, JWT structure, and MFA coverage.
In practice, secure authentication/authorization is about combining OAuth2/OIDC, hardened JWTs, scoped least privilege, and MFA—all continuously validated and monitored.
Table
Common Mistakes
- Using implicit OAuth2 flow for SPAs, exposing tokens in URLs.
- Storing sensitive data inside JWT payloads.
- Not validating aud, iss, or exp on tokens.
- Relying only on RBAC without contextual rules.
- Offering “remember me” sessions with no expiry.
- Implementing MFA but allowing weak fallback (e.g., email OTP only).
- Forgetting to rotate keys or using symmetric keys everywhere.
- Storing refresh tokens in localStorage, exposing them to XSS.
Sample Answers
Junior:
“I use OAuth2.0 with PKCE for secure login. JWT tokens are short-lived and signed. I validate all claims before use. For permissions, I apply role-based access and enable MFA with authenticator apps.”
Mid:
“I design with OAuth2/OIDC, enforcing token rotation and HTTPS. JWTs use RS256 and minimal claims, validated server-side. RBAC plus tenant-level checks prevent cross-access. MFA is applied adaptively, requiring TOTP or WebAuthn for sensitive flows.”
Senior:
“My approach is layered: OAuth2/OIDC with PKCE, refresh rotation, and mTLS for services; hardened JWTs with revocation lists and key rotation; RBAC+ABAC with scope-limited access. MFA is mandatory for high-risk actions, using WebAuthn or hardware keys. Logs, SIEM alerts, and compliance checks enforce governance.”
Evaluation Criteria
Interviewers seek structured knowledge of authentication (OAuth2/OIDC), JWT best practices, MFA, and authorization. Strong answers mention PKCE, token rotation, asymmetric signing, role+attribute controls, and adaptive MFA. Red flags: vague “use JWTs” without validation details, storing secrets in tokens, or treating MFA as optional. The best answers tie practices to OWASP, zero-trust principles, least privilege, and continuous monitoring, demonstrating not just technical tactics but governance and user safety.
Preparation Tips
- Build a demo OAuth2/OIDC login flow with PKCE.
- Configure JWT signing with RS256 and validate claims server-side.
- Add RBAC (roles) and ABAC (context rules) to APIs.
- Implement TOTP MFA and WebAuthn on a test app.
- Store refresh tokens in secure, HttpOnly cookies; test XSS resilience.
- Rotate signing keys and configure JWKS endpoint.
- Simulate replay attacks to test token expiration.
- Review OWASP ASVS sections on authentication and access control.
- Prepare a 60-second explainer connecting OAuth2, JWT hardening, and MFA.
Real-world Context
A fintech app leaked access tokens via implicit flow, letting attackers hijack sessions. Switching to PKCE + HTTPS and shortening token TTL closed the gap. A SaaS platform stored emails inside JWTs; stolen tokens exposed PII. Migrating to minimal claims and encryption solved it. A healthcare provider had MFA only via SMS, which was phishable; upgrading to WebAuthn eliminated attacks. Another enterprise missed JWT key rotation, causing trust failures; adding JWKS endpoints fixed validation. These examples show how auth + JWT + MFA + monitoring directly reduce real-world risks.
Key Takeaways
- Use OAuth2/OIDC with PKCE, token rotation, and HTTPS/mTLS.
- Harden JWTs with asymmetric signing, minimal claims, and revocation.
- Enforce least-privilege authorization (RBAC + ABAC + scopes).
- Apply MFA (TOTP, WebAuthn, push) for sensitive flows.
- Monitor, log, and rotate keys continuously.
Practice Exercise
Scenario:
You are tasked with securing a new enterprise web portal with thousands of users, handling sensitive data.
Tasks:
- Implement OAuth2/OIDC with PKCE and short-lived access tokens. Use refresh token rotation.
- Sign JWTs with RS256, validate all claims (aud, iss, exp, jti). Store minimal claims only.
- Build RBAC for standard roles and ABAC for contextual rules (time, device, tenant).
- Require MFA (TOTP or WebAuthn) for logins and high-risk actions.
- Store tokens in HttpOnly cookies; enforce SameSite and Secure flags.
- Rotate signing keys; publish JWKS for verifiers.
- Add SIEM monitoring for anomalies (failed logins, token misuse).
- Document escalation: on anomaly detection, revoke tokens, notify users, and audit logs.
Deliverable:
A secure authentication and authorization framework that uses OAuth2/OIDC, hardened JWTs, least-privilege authorization, and MFA, backed by monitoring and governance, ready for enterprise deployment.

