Securing REST APIs: JWTs and OAuth2 Explained
In the age of SPAs (Single Page Applications) and Mobile Apps, the traditional cookie-based session is dying. We need Stateless Authentication. Enter JWT (JSON Web Tokens).
Structure of a JWT
A JWT is just a base64 encoded string with three parts: Header.Payload.Signature.
- Header: Mentions the algorithm (usually HS256).
- Payload: User data.
{"sub": "123", "role": "admin", "exp": 1234567890}. - Signature: Using a secret key on the server, we sign the Header+Payload.
The client gets this token on Login and sends it in the Authorization header for every subsequent request. The server verifies the signature. If it matches, we know the user is who they say they are—without looking up a session in the database.
OAuth2: The Specialized Handshake
OAuth2 is a protocol, not just a token format. It allows a user to grant a third-party application access to their resources without giving away their password.
The Flow (Authorization Code Grant)
- User clicks “Login with Google”.
- Redirect: Your app redirects the browser to Google’s auth server.
- Consent: User types password on Google.com and approves your app.
- Callback: Google redirects back to your app with a generic
code. - Exchange: Your server (backend-to-backend) exchanges this
codefor anAccess Token(JWT).
Security Vulnerabilities to Avoid
- Don’t store JWTs in LocalStorage: It’s vulnerable to XSS attacks. Store them in
HttpOnlycookies. - Short Expiry: Tokens should last minutes, not days.
- Refresh Tokens: Use a long-lived refresh token to get new access tokens transparently.
Security is hard. Don’t roll your own crypto. Use battle-tested libraries like Spring Security or Passport.js.