423.4 Secure APIs and authentication
Understand what APIs are, why they matter, and how to protect them using strong authentication and secure coding practices.
Targets
Explain what APIs are and how they are used in software systems
Identify common security risks in poorly secured APIs
Apply authentication and access control to reduce threats like broken authentication and data leaks
Syllabus references
APIs are the glue that binds modern software together. They expose powerful functionality — but if you don’t lock them down, they also expose your system to attackers. This module explains what APIs are, how standard authentication methods work, why session management matters even in “stateless” APIs, and how to defend against broken authentication and session hijacking.
What is an API?
An API (Application Programming Interface) enables one program to communicate with another. When your timetable app fetches your schedule from the server, it calls an API. When a weather widget shows current conditions, it calls an API. APIs define the rules and formats (often JSON or XML) for sending and receiving data.
Why API security matters
APIs often sit at the boundary between a software system and the outside world. If they are not protected, attackers can:
Access private data or someone else’s account.
Bypass the login system and perform unauthorised actions.
Flood the server with requests (denial of service).
Authentication vs authorisation
Authentication verifies who is making a request — it’s about identity.
Authorisation determines what an authenticated user is allowed to do. A student should not be able to access admin features. Robust API security requires both.
Common API authentication methods
HTTP Basic authentication – sends a Base64‑encoded username/password pair in an
Authorization
header. It must be used over HTTPS to prevent eavesdropping, and it offers no protection against or attacks.API keys – unique identifiers issued to each client. Keys must be sent with every request and kept secret. Like passwords, they should be rotated regularly and never hard‑coded in public code repositories.
JWT (JSON Web Token) – a signed and optionally encrypted token that contains the user’s identity. JWTs are stateless; the server verifies the signature rather than looking up a session. Set reasonable expiration times and avoid storing sensitive data in the token.
OAuth 2.0 – a token‑based protocol that allows users to grant limited access to third‑party applications. It is the gold standard for delegated authorisation and underpins “Sign in with Google/GitHub” flows.
HTTP Basic Authentication
HTTP basic authentication is the most rudimentary way to implement API authentication. It involves sending credentials as user/password pairs in an Authorization
header field, where the credentials are encoded using Base64. However, these credentials
API Key Authentication
An API key is a unique identifier that an API provider issues to registered users in order to control usage and monitor access. The API key must be sent with every request—either in the query string, as a request header, or as a cookie. Like HTTP basic authentication, API key authentication must be used with HTTPS to ensure the API key remains secure.
JWT authentication
JWT, which stands for JSON Web Token, is a compact, stateless mechanism for API authentication. When a user logs into an application, the API server creates a digitally signed and encrypted JWT that includes the user's identity. The client then includes the JWT in every subsequent request, which the server deserialises and validates. The user's data is therefore not stored on the server's side, which improves scalability.
OAuth authentication
OAuth is a token-based authentication mechanism that enables a user to grant third-party access to their account without having to share their login credentials. OAuth 2.0, which provides greater flexibility and scalability than OAuth 1.0, has become the gold standard for API authentication, and it supports extensive API integration without putting user data at risk.
Securing APIs
In addition to robust authentication and session management, secure APIs follow these principles:
Use HTTPS everywhere to protect data in transit.
Authenticate every request; do not assume a session is valid just because the client sent a token.
Enforce authorisation using role‑based access control or attribute‑based policies.
Validate and sanitise input to prevent injection and other attacks.
Limit exposure: only return data that is necessary; avoid leaking internal identifiers.
Rate‑limit and monitor API usage. Detect anomalies, log errors, and respond to abuse.
Keep secrets out of source code – store API keys and secrets in secure configuration services or environment variables.
Summary
APIs are the gateways to your application’s data and functionality. Securing them requires more than just choosing an authentication method: you must also manage sessions carefully, handle tokens securely, and enforce both authentication and authorisation. By following best practices for session management (secure, HttpOnly and SameSite cookies limited scope, appropriate expiration, and by mitigating broken authentication through strong credentials, MFA and proper token handling, you can minimise the vulnerabilities and protect systems against common API attacks.
Questions
What is the primary purpose of an API (Application Programming Interface)?
2. Outline TWO examples of APIs in action.
3. What role do JSON and XML play in API authentication?
4. Compare HTTP basic authentication with API key authentication using a table to illustrate similarities and differences on key criteria.
5. Explain why APIs must have tight security controls.
6. Outline ways of securing APIs.
7. Distinguish between authentication and authorisation.
8. Outline some common API vulnerabilities.
Last updated
Was this helpful?