API Design and Microservices Architecture

Chapter 8
API Security, Privacy and Ethical Engineering

Post Graduate Diploma in Full Stack Development
API Design and Microservices Architecture   •   Post Graduate

8. API Security, Privacy and Ethical Engineering

Introduction and Context

APIs are the operational interfaces through which modern software systems exchange data and invoke capabilities. They connect mobile applications to services, enable microservices to collaborate, expose business functions to partners, and support automation through machine-to-machine communication. This centrality also makes APIs attractive targets. A defect in an API may expose an entire customer database, permit unauthorized financial transactions, reveal personal information, or allow an attacker to disrupt several dependent services simultaneously.

API security is therefore broader than adding authentication to an endpoint. It involves identifying threats, establishing trust boundaries, controlling access to individual resources and actions, protecting communication and secrets, limiting abuse, recording relevant events, and responding to incidents. Privacy and ethics extend this responsibility further. A technically secure API may still be unethical if it collects excessive information, hides consent conditions, excludes users with disabilities, or enables harmful surveillance.

In a microservices environment, security must be designed as a distributed property. A gateway, identity provider, service mesh, application service, database, message broker, and observability platform may all participate in a single request. Security decisions must remain coherent across these components without assuming that any internal network is automatically trustworthy.

Learning Objectives

After studying this chapter, the learner should be able to:

1. Construct a threat model for an API or microservices system.

2. Apply zero-trust principles to service and user interactions.

3. Explain the roles of TLS, mTLS, OAuth 2.0, OpenID Connect, and JWTs.

4. Design authentication, authorization, secrets management, and rate-limiting controls.

5. Recognize and mitigate major OWASP API risks.

6. Incorporate privacy, consent, data minimization, accessibility, and ethical reasoning into API design.

7. Evaluate security trade-offs in a realistic organizational scenario.

Security Begins with a Threat Model

A threat model is a structured representation of what must be protected, from whom, through which attack paths, and with what consequences. It should be developed before implementation and revised when architecture, data, dependencies, or business processes change.

The first task is asset identification. Assets may include credentials, access tokens, personal data, payment records, source code, service availability, audit records, and the integrity of business transactions. The second task is to identify actors and trust boundaries. Actors include ordinary users, administrators, partner systems, compromised clients, malicious insiders, automated bots, and compromised services. Trust boundaries commonly exist between the public internet and an API gateway, between a gateway and internal services, between services and databases, and between an organization and an external identity provider.

A useful process is to draw data-flow diagrams showing processes, data stores, external entities, and flows. Each flow should be examined for authentication, authorization, confidentiality, integrity, replay, injection, and logging requirements. Threats can then be classified using models such as STRIDE: spoofing, tampering, repudiation, information disclosure, denial of service, and elevation of privilege. Risk is commonly assessed by considering likelihood, impact, exposure, and the effectiveness of existing controls.

Threat modeling is not a one-time document. For example, adding a bulk-export endpoint changes the threat landscape even if the identity system remains unchanged. Security reviews should therefore be integrated into design review, pull requests, release gates, and post-incident learning.

Zero Trust in API Architectures

Zero trust rejects the assumption that a request is safe because it originates inside a corporate network or cluster. The guiding principle is “never trust, always verify.” Verification is contextual and may consider identity, service identity, device posture, workload, requested resource, time, location, risk signals, and transaction value.

In practice, zero trust requires explicit authentication, least-privilege authorization, segmentation, continuous evaluation, and comprehensive telemetry. A service should not automatically trust every caller in the same Kubernetes namespace. Each request should carry verifiable identity, and the receiving service should independently enforce its authorization policy. Network controls remain useful, but network location alone is not an adequate security boundary.

Zero trust does not mean that every request must be re-authenticated through an interactive login. It means that trust is based on verifiable evidence and policy rather than on an implicit assumption. Short-lived tokens, mTLS identities, service authorization policies, and anomaly detection can provide this evidence while preserving performance.

Protecting Communication: TLS and mTLS

Transport Layer Security (TLS) provides confidentiality, integrity, and server authentication for network communication. HTTPS is HTTP carried over TLS. During a handshake, the client and server negotiate cryptographic parameters, the server proves possession of a private key through a certificate, and session keys are established. Modern deployments should use current TLS versions, secure cipher configurations, certificate validation, and automated certificate renewal.

TLS protects a channel, not the entire application. It does not determine whether a user may access another customer’s invoice, nor does it prevent a valid but compromised credential from being misused. Certificate validation must also be correct; disabling verification to solve development errors creates a serious production vulnerability.

Mutual TLS (mTLS) adds client authentication. Both parties present certificates, allowing a service to verify the identity of another service or managed device. mTLS is particularly useful for service-to-service communication, regulated environments, and service meshes. It should be combined with authorization: proving that a caller is the “billing-service” does not automatically authorize every billing operation. Certificate issuance, rotation, revocation, clock synchronization, and workload identity mapping require operational discipline.

Identity, Authentication, and Delegated Access

Authentication answers “Who or what is making this request?” Authorization answers “What is that identity allowed to do?” Confusing these questions is a common source of API vulnerabilities.

OAuth 2.0 is an authorization framework for delegated access. It enables a resource owner to authorize a client to access a protected resource through an authorization server. The client receives an access token with a defined scope, audience, lifetime, and other claims. Authorization Code with Proof Key for Code Exchange (PKCE) is the preferred pattern for public clients such as mobile and browser applications because it reduces the risk of intercepted authorization codes. Client credentials is suitable for machine-to-machine access where a service acts on its own behalf. The implicit grant is generally avoided in modern designs.

OAuth access tokens should be limited in scope and duration. An API must validate the token’s signature or introspection result, issuer, audience, expiration, and relevant scopes. A token intended for one API should not automatically be accepted by another. Refresh tokens require stronger protection and rotation because they can create long-lived sessions.

OpenID Connect (OIDC) is an identity layer built on OAuth 2.0. It provides authentication and an ID token describing the authenticated user to the client. An ID token is not normally a substitute for an API access token. The client uses the ID token to understand the login event, while the API uses an access token to authorize access to a resource.

JSON Web Tokens (JWTs) are compact, signed tokens containing claims. A signed JWT provides integrity and issuer authenticity when verified correctly; it does not necessarily provide confidentiality. Sensitive information should not be placed in ordinary readable claims merely because the token is signed. APIs must reject unexpected algorithms, validate the issuer and audience, enforce expiry, and avoid trusting client-supplied claims without verification. Revocation is difficult for self-contained tokens, so short lifetimes, token rotation, introspection, or a deny-list may be needed for high-risk use cases.

Authorization at the Resource and Action Level

Authentication controls entry; authorization controls behavior. Effective APIs apply authorization at the level of the requested object and operation, not only at the endpoint group.

Role-based access control (RBAC) assigns permissions to roles such as support agent or finance manager. It is understandable and useful, but roles can become excessive or too broad. Attribute-based access control (ABAC) evaluates attributes of the subject, resource, action, and context, such as department, ownership, transaction amount, or working location. Relationship-based access control (ReBAC) evaluates relationships, such as whether a user belongs to a project that owns a document.

Object-level authorization is essential. An endpoint such as `GET /orders/1842` must verify that the authenticated principal is permitted to view order 1842; it must not assume that possession of the identifier is sufficient. Identifier randomization can reduce enumeration but is not an authorization control. Similarly, mass assignment must be prevented by allowing clients to update only explicitly permitted fields. A user should not be able to include `isAdmin: true` in a profile update simply because the field is accepted by automatic object binding.

Policies should be centralized enough to remain consistent but close enough to the resource decision that business context is available. Policy-as-code tools can support review, testing, and deployment, but they do not remove the need for sound policy design.

Secrets Management and Secure Configuration

Secrets include passwords, API keys, private keys, database credentials, signing keys, and encryption keys. They should not be embedded in source code, container images, mobile applications, or ordinary configuration files committed to version control. Secret managers provide controlled storage, access policies, auditing, rotation, and sometimes dynamic credentials that expire automatically.

A strong lifecycle includes generation using a secure random source, restricted access, encryption in transit and at rest, automated rotation, versioning, emergency revocation, and monitoring for abnormal use. Different environments should use separate secrets. The principle of least privilege applies to the secret manager itself: a service should retrieve only the secrets required for its function.

Signing-key compromise has especially wide consequences. Key custody, rotation, key identifiers, overlapping validity periods, and emergency rollover procedures should be designed before an incident occurs. Logs must be checked for tokens, passwords, authorization headers, personal data, and other sensitive values before publication to centralized logging systems.

Rate Limiting, Abuse Prevention, and Resilience

Rate limiting controls the volume or cost of requests. It protects availability, reduces credential-stuffing and scraping, limits accidental overload, and controls expensive operations. Policies may be based on user, token, client application, IP address, tenant, endpoint, or a combination. A token-bucket algorithm permits controlled bursts while maintaining an average rate; a leaky-bucket approach smooths traffic.

Limits should reflect business risk rather than being identical for every endpoint. Login, password-reset, search, bulk export, and payment endpoints need different controls. Responses should communicate throttling through suitable status codes and headers without revealing information that assists attackers. Rate limiting at a gateway is useful, but services should also defend themselves because attackers may bypass or overload internal paths.

Rate limits alone do not solve denial-of-service risk. Payload-size limits, timeouts, concurrency controls, caching, circuit breakers, queues, pagination, asynchronous processing, and upstream protection may be required. Fairness is also important: a shared IP address can represent many legitimate users, so a single dimension may cause discriminatory blocking.

OWASP API Risks in Practice

The OWASP API Security Top 10 provides a practical risk vocabulary. The following risks deserve particular attention:

  • Broken object-level authorization occurs when a user can access another user’s object by changing an identifier.
  • Broken authentication includes weak credential recovery, poorly validated tokens, and insecure session handling.
  • Broken object-property-level authorization arises when clients can read or modify fields they should not control.
  • Unrestricted resource consumption allows excessive data, computation, storage, or calls.
  • Broken function-level authorization permits ordinary users to invoke administrative functions.
  • Unrestricted access to sensitive business flows enables abuse such as automated purchasing, reservation manipulation, or bulk account creation.
  • Server-side request forgery (SSRF) occurs when an API fetches attacker-controlled URLs and can reach internal services or metadata endpoints.
  • Security misconfiguration includes permissive CORS, debug errors, default credentials, missing security headers, or exposed administration interfaces.
  • Improper inventory management results from undocumented, obsolete, shadow, or inconsistent API versions.
  • Unsafe consumption of APIs occurs when an organization trusts external API data or controls without validating them.

Mitigation requires positive validation, explicit authorization, endpoint inventory, dependency assessment, secure defaults, abuse testing, and monitoring. Security testing should include horizontal privilege escalation, vertical privilege escalation, malformed input, replay, token substitution, mass assignment, SSRF probes, pagination abuse, and excessive data exposure.

Privacy, Consent, and Data Governance

Privacy engineering treats personal data protection as a design requirement rather than a legal review performed at the end. Personal data may include direct identifiers, quasi-identifiers, device data, location, behavioral records, and inferred attributes. Sensitive categories often require stronger safeguards and more restrictive purposes.

Data minimization means collecting and exposing only what is necessary for a specified purpose. An API that returns a complete customer profile to every internal consumer increases breach impact and violates least privilege. Field-level projections, purpose-specific endpoints, pseudonymization, aggregation, retention limits, and deletion workflows reduce exposure. Pseudonymization lowers direct identifiability but does not make data anonymous if re-identification remains reasonably possible.

Consent must be informed, specific, freely given where applicable, and capable of withdrawal. A consent screen should not bundle unrelated purposes or use confusing interface design to pressure acceptance. APIs should record the consent version, purpose, timestamp, and relevant subject or tenant while avoiding unnecessary storage of the full personal content.

Privacy governance also includes data subject rights, lawful processing grounds, cross-border transfer controls, breach response, processor management, retention schedules, and privacy impact assessments. Exact legal requirements vary by jurisdiction; engineering teams should work with qualified legal and privacy professionals rather than treating one compliance checklist as universal.

Accessibility and Ethical API Use

Accessibility applies to developer-facing APIs as well as end-user interfaces. Documentation should be navigable, clearly structured, searchable, and usable with assistive technologies. Error messages should be understandable and actionable. Authentication flows should not depend exclusively on inaccessible visual puzzles, a single device modality, or timing that excludes users with disabilities. Rate limits, account recovery, and consent interfaces should be tested with diverse users.

Ethical engineering asks not only whether a system can be built, but whether it should be built and under what constraints. API designers should examine foreseeable misuse, power asymmetries, discriminatory outcomes, surveillance potential, and the interests of people who are not direct customers. A location API, for example, may be commercially useful but dangerous if it enables stalking or unauthorized monitoring. Ethical controls may include purpose limitation, user visibility, strong authorization, audit trails, abuse reporting, human review, and refusal to expose certain capabilities.

When APIs support automated decisions, organizations should evaluate explainability, contestability, data quality, bias, and human accountability. Security controls must not become excuses for unjustified exclusion. Conversely, privacy requirements should not be used to conceal harmful practices from affected people.

A Secure API Delivery Process

Security is most effective when integrated into the software development lifecycle. During discovery, teams identify data classifications, users, trust boundaries, regulatory obligations, and abuse cases. During design, they select identity protocols, authorization models, encryption, error behavior, retention rules, and threat mitigations. During implementation, they use secure libraries, schema validation, dependency scanning, secret detection, and peer review.

Testing should combine unit tests for policy rules, integration tests for identity flows, contract tests for security-relevant fields, dynamic testing, fuzzing, penetration testing, and abuse-case simulation. Production readiness should include logging and alerting, key rotation, incident contacts, rollback procedures, and documented API inventory.

Operational monitoring should detect unusual token use, authorization failures, high-volume enumeration, impossible travel, repeated password resets, abnormal data exports, and calls to deprecated endpoints. Logs should support accountability while respecting minimization and access controls. Incident response should include containment, credential or certificate revocation, affected-party assessment, communication, recovery, and a blameless technical review.

Illustrative Case Study: Securing a Multi-Tenant Logistics API

Background

FleetFlow is an illustrative SaaS platform used by transport companies to manage drivers, vehicles, delivery orders, and proof-of-delivery images. Its API serves a web dashboard, a mobile driver application, and partner systems. The platform is migrating from a monolith to microservices for identity, orders, routing, billing, and documents.

Situation and Problem

A security review discovers that an authenticated dispatcher can change an order identifier in a URL and retrieve another tenant’s order. The mobile application contains a long-lived partner key, internal services trust requests from the cluster network, document uploads accept arbitrary URLs for “remote import,” and logs contain bearer tokens. The product team also plans to expose driver location history to all customer administrators.

Analysis

The identifier flaw is broken object-level authorization. The partner key is a secret-distribution and credential-lifecycle risk. Network-based service trust conflicts with zero trust. Remote import creates an SSRF threat that may reach cloud metadata services or internal administration endpoints. Token logging increases the impact of interception. Exposing detailed location history creates privacy and employee-safety concerns, especially where the purpose is not clearly defined.

Approach and Solution

FleetFlow introduces tenant-aware authorization at every order and document access decision. The policy checks the authenticated subject, tenant relationship, resource ownership, and requested action. RBAC is used for broad roles, while resource relationships and attributes handle dispatcher assignments and regional boundaries.

The web and mobile clients use OIDC with Authorization Code and PKCE. APIs validate access-token issuer, audience, signature, expiry, and scopes. Service-to-service calls use workload identities with mTLS, while each service still applies authorization. Partner access moves to OAuth client credentials with narrowly scoped, short-lived tokens; secrets are stored and rotated through a secret manager.

Remote imports are restricted to an allow-listed scheme and destination policy. Requests are resolved and checked against private address ranges, redirects are controlled, response sizes are limited, and imports occur in an isolated worker. Bearer tokens and unnecessary personal data are removed from logs. Location data is minimized, shown at an appropriate granularity, retained for a defined period, and disclosed to drivers through a transparent policy. High-risk exports require stronger authorization, rate limits, audit logging, and review.

Outcome

The revised design reduces cross-tenant exposure, limits the impact of stolen credentials, and makes service identity explicit. Security tests are added to the deployment pipeline, and monitoring alerts on enumeration and unusual exports. The organization also establishes a privacy review for new data uses rather than treating privacy as a one-time migration task.

Learning Points

The case demonstrates that API security is a system property. Authentication alone would not have fixed the tenant-isolation flaw, mTLS alone would not have authorized individual orders, and rate limiting alone would not have addressed excessive location collection. Effective engineering combines technical controls, operational processes, and ethical judgment.

Challenges, Trade-offs, and Common Mistakes

Security controls introduce cost, latency, operational complexity, and possible usability friction. Short token lifetimes improve containment but may increase refresh traffic. Detailed audit records support accountability but can create privacy and storage obligations. Strong rate limits protect services but may harm legitimate high-volume users. mTLS improves workload authentication but requires certificate lifecycle expertise.

Common mistakes include accepting any valid JWT without checking its audience, treating internal traffic as trusted, placing authorization only in an API gateway, returning excessive fields, relying on random identifiers instead of access checks, storing secrets in environment files without governance, disabling TLS verification, using a single administrator credential, and failing to inventory deprecated endpoints. Another mistake is designing for compliance as a set of documents rather than as enforceable system behavior.

Current Developments and Professional Practice

Modern platforms increasingly combine API gateways, identity-aware proxies, service meshes, workload identity, policy-as-code, software supply-chain security, and automated security testing. Continuous authorization evaluates risk signals during a session rather than treating login as permanent trust. Confidential computing and hardware-backed key protection may help protect sensitive operations, although they do not replace application authorization.

Artificial intelligence is being used for anomaly detection, API discovery, schema analysis, and assisted threat triage. These applications require careful validation: automated models can produce false positives, expose sensitive logs, or encode biased assumptions. Generative AI integrations also create risks involving prompt injection, excessive data access, insecure tool invocation, and unclear responsibility. AI-enabled APIs should use least privilege, explicit tool authorization, input and output controls, and human oversight for consequential actions.

Professional practice is ultimately characterized by verifiable controls: documented decisions, tested policies, measurable detection capability, accountable ownership, and an organizational willingness to correct systems when evidence reveals harm.

Best-Practice Checklist

A mature API program should be able to demonstrate that it:

  • maintains an accurate API and data-flow inventory;
  • models threats and abuse cases before release;
  • uses TLS correctly and mTLS where workload identity requires it;
  • separates authentication from resource and action authorization;
  • validates token issuer, audience, signature, scope, and lifetime;
  • enforces tenant and object-level isolation;
  • stores, rotates, and audits secrets through controlled facilities;
  • limits requests, payloads, concurrency, and expensive business operations;
  • minimizes personal data and records meaningful consent and retention decisions;
  • provides accessible documentation and recovery experiences;
  • monitors security events without unnecessarily logging sensitive data;
  • tests controls continuously and rehearses incident response.

Chapter Summary

API security is a distributed discipline involving identity, communication protection, authorization, resilience, monitoring, and governance. Threat modeling identifies assets, actors, trust boundaries, and attack paths. Zero trust requires explicit verification rather than reliance on network location. TLS protects communication, while mTLS can authenticate services. OAuth 2.0 supports delegated authorization, OIDC supports authentication, and JWTs carry verifiable claims but require rigorous validation.

Secure APIs enforce least privilege at object and action level, protect secrets throughout their lifecycle, and use rate limiting and resilience controls to manage abuse. OWASP API risks provide a practical framework for testing common failures. Privacy requires purpose limitation, consent, minimization, retention control, and responsible handling of personal information. Ethical and accessible engineering considers affected people, foreseeable misuse, and unequal impacts. The most reliable security programs integrate these concerns into architecture, development, deployment, and operations.

Key Terms

Threat model: A structured analysis of assets, actors, trust boundaries, threats, and mitigations.

Zero trust: A security approach that continuously verifies requests instead of trusting network location.

TLS: A cryptographic protocol providing confidentiality, integrity, and authenticated communication.

mTLS: TLS in which both communicating parties authenticate using certificates.

OAuth 2.0: A framework for delegated authorization to protected resources.

OpenID Connect: An identity and authentication layer built on OAuth 2.0.

JWT: A compact token format carrying signed or encrypted claims.

Access token: A credential presented to an API to obtain authorized access.

Scope: A bounded permission represented in an access token.

Audience: The intended recipient API or service identified in a token.

RBAC: Authorization based on roles assigned to users or services.

ABAC: Authorization based on subject, resource, action, and contextual attributes.

ReBAC: Authorization based on relationships between subjects and resources.

Secret: Sensitive authentication or cryptographic material requiring controlled protection.

Rate limiting: Restricting request volume or resource consumption according to policy.

SSRF: An attack in which a server is induced to make unauthorized network requests.

Data minimization: Collecting and exposing only data necessary for a defined purpose.

Pseudonymization: Replacing direct identifiers while retaining a controlled possibility of re-identification.

Consent: A sufficiently informed and specific agreement to a defined processing purpose.

Policy-as-code: Representing enforceable policy in a versioned, testable machine-readable form.

Review Questions

Short Answer Questions

1. Distinguish authentication from authorization in an API.

2. What security properties does TLS provide, and what does it not provide?

3. Why is an ID token not normally used as an API access token?

4. Explain object-level authorization with an example.

5. State two principles of data minimization.

Long Answer Questions

1. Explain how threat modeling can be applied to a microservices API using trust boundaries and STRIDE categories.

2. Compare OAuth 2.0, OpenID Connect, and JWTs, including their distinct purposes.

3. Discuss how RBAC, ABAC, and ReBAC can be combined in a multi-tenant API.

4. Explain the lifecycle requirements for managing API secrets and signing keys.

5. Discuss how privacy, accessibility, and ethical considerations influence API architecture.

Analytical / Application Questions

1. An API validates JWT signatures but accepts tokens issued for a different service. Identify the vulnerability and propose corrective controls.

2. A document-import endpoint accepts a user-provided URL and runs inside a privileged cluster. Develop a threat analysis and mitigation plan.

3. A public transport API must serve large enterprise customers without allowing scraping or denial of service. Design a fair rate-limiting and monitoring strategy.

22. Multiple Choice Questions with Answers

Question 1

A service validates a JWT signature but does not check the token audience. What is the principal risk?

A. The token cannot be encrypted

B. A token issued for another API may be accepted

C. TLS certificates will expire sooner

D. The client cannot refresh the token

Correct Answer: B. A token issued for another API may be accepted

Question 2

Which OAuth 2.0 flow is most appropriate for a browser or mobile public client requiring protection against authorization-code interception?

A. Implicit grant

B. Client credentials

C. Authorization Code with PKCE

D. Password grant

Correct Answer: C. Authorization Code with PKCE

Question 3

A user changes `/orders/1842` to `/orders/1843` and sees another customer’s order. Which control has failed?

A. Object-level authorization

B. Certificate rotation

C. Payload compression

D. DNS validation

Correct Answer: A. Object-level authorization

Question 4

What additional security property does mTLS provide compared with ordinary server-authenticated TLS?

A. Automatic business authorization

B. Client or service certificate authentication

C. Guaranteed database encryption

D. Unlimited request capacity

Correct Answer: B. Client or service certificate authentication

Question 5

Which statement best describes OpenID Connect?

A. A database encryption algorithm

B. An identity layer built on OAuth 2.0

C. A replacement for TLS

D. A rate-limiting algorithm

Correct Answer: B. An identity layer built on OAuth 2.0

Question 6

A remote-import API fetches arbitrary URLs supplied by users. Which threat should receive priority?

A. SSRF

B. Token expiry

C. UI color contrast

D. Certificate pinning

Correct Answer: A. SSRF

Question 7

Which practice best supports data minimization?

A. Returning every customer field to all internal clients

B. Keeping personal data indefinitely for possible future use

C. Providing purpose-specific fields and defined retention periods

D. Using random identifiers as a substitute for authorization

Correct Answer: C. Providing purpose-specific fields and defined retention periods

Question 8

Why is relying only on an API gateway for authorization dangerous?

A. Gateways cannot terminate TLS

B. Internal paths or service calls may bypass the gateway

C. Gateways always reveal passwords

D. It prevents all rate limiting

Correct Answer: B. Internal paths or service calls may bypass the gateway

Question 9

Which rate-limiting design is most appropriate for a costly bulk-export endpoint?

A. No limit because the endpoint is authenticated

B. The same IP-only limit used for every endpoint

C. A tenant- and identity-aware limit combined with export monitoring

D. A limit applied only after the export completes

Correct Answer: C. A tenant- and identity-aware limit combined with export monitoring

Question 10

Which statement about a signed JWT is correct?

A. Its claims are automatically confidential

B. Its signature provides integrity when verified correctly

C. It cannot expire

D. It eliminates the need for authorization checks

Correct Answer: B. Its signature provides integrity when verified correctly

API Design and Microservices Architecture
Chapter 8 of 15