API Security
API Security
DESIGNING
Key Components:
3. OAuth Client: Application requesting access to a resource.
4. Authorization Server: Verifies the user’s identity and issues tokens.
5. Resource Owner: The user who owns the resource/data.
6. Resource Server: Holds the protected resources (data) which the client wants
to access.
WORKFLOW:
Step 1: Authorization Request
The OAuth Client requests authorization from the Authorization Server to
access resources on behalf of the Resource Owner.
Step 2: Token Issuance
Upon authorization, the Authorization Server provides a token to the OAuth
Client.
Step 3: Access Resource
The OAuth Client uses the token to access the OAuth Resource Server, which
holds the desired resources.
Step 4: Resource Access
The Resource Server verifies the token and grants the client access to the
resources.
• PROPERTIES:
• Authentication: When a user successfully logs in using their credentials, an
ID token is returned. According to the OpenID Connect (OIDC) specs, an ID
token is always a JWT.
• Authorization: Once a user is successfully logged in, an application may
request to access routes, services, or resources (e.g., APIs) on behalf of that
user. To do so, in every request, it must pass an Access Token, which may be
in the form of a JWT. Single Sign-on (SSO) widely uses JWT because of the
small overhead of the format, and its ability to easily be used across
different domains.
• Information exchange: JWTs are a good way of securely transmitting
information between parties because they can be signed, which means you
can be certain that the senders are who they say they are. Additionally, the
structure of a JWT allows you to verify that the content hasn't been
tampered with.
1. JWT authentication is a token-based stateless authentication
mechanism. It is popularly used as a client-side-based
stateless session, this means the server doesn’t have to
completely rely on a data store (or) database to save session
information.
2. JWTs can be encrypted, but they are typically encoded &
signed. We will be focusing on Signed JWTs. The purpose of
Signed JWT is not to hide the data but to ensure the
authenticity of the data. And that is why it’s highly
recommended to use HTTPS with Signed JWTs.
Feature OAuth 2.0 Java Web Tokens (JWT)
Purpose Authorization protocol for third- Token format used for secure
party access. information exchange.
Function Delegates access to resources Self-contained tokens with
via tokens. claims, typically used in OAuth.
State Can be stateful (session-based Stateless—JWT is self-
tokens) or stateless. contained and doesn't require
server-side storage.
Use Case Used to control access to Used to transmit data between
resources and grant parties securely.
permissions.
Token Type Can use different types of JWT is one specific type of
tokens (e.g., Bearer tokens, token, often used in OAuth.
JWT).
Signature Doesn't require a signature Always includes a signature to
itself (though OAuth tokens can ensure data integrity.
be signed).
Storage OAuth tokens may be stored JWTs are typically stored in
server-side. local storage or cookies.
Sample JWT implementation
const express = require('express’);
const jwt = require('jsonwebtoken’);
const bcrypt = require('bcryptjs'); // For password hashing (optional but common)
const app = express();
app.use(express.json()); // Middleware to parse JSON
// Secret key to sign the JWT (in a real app, store this securely)
const JWT_SECRET = 'your_secret_key’;
•Whitelisting: Only allow known, safe characters or inputs. For example, for a name field,
allow letters and spaces only.
•Escaping Special Characters: Convert special characters into harmless representations to
prevent them from being interpreted as code. (e.g. <, >, ‘, “).
•Avoid Direct User Input in SQL Queries: This is essential to prevent SQL injection. Always
use prepared statements or parameterized queries that separate user inputs from the query
itself.
2. Rate limiting and throttling
Prevent brute force and denial-of-service attacks by limiting API usage per user or IP.
Rate limiting ensures that users can only make a certain number of requests in a given
period, protecting your API from being overwhelmed by excessive requests (brute force or
denial-of-service attacks).
How It Works: You can define a rule that allows each user (or IP address) to make, for
example, only 100 requests per minute. If they exceed that limit, their requests will be
blocked temporarily.
3. Error handling
Design APIs to return generic error messages to avoid leaking sensitive information.
Explanation: When an API encounters an error, it’s important to be careful about what
information is shared. If an API gives too much detail in its error messages, it can
unintentionally reveal sensitive information about the system to potential attackers.
Best Practices:
• Return generic error messages such as “An error occurred” or “Invalid input,”
without revealing technical details (e.g., "SQL syntax error").
• Log detailed errors internally: For debugging purposes, keep detailed logs in the
server, but only expose minimal information to the user.
• Avoid exposing stack traces: These contain critical information about your code
structure, which could help attackers find vulnerabilities.
THANK YOU