How to secure APIs for external users? Network, authentication and API clients - Part 2
Secure APIs for external users: the network security baseline, authentication flows for trusted and public clients and managing API clients and secrets.
How to secure APIs for external users? Network, authentication and API clients - Part 2
The moment an API is reachable from the internet, it will be found and probed, whether we announce it or not. An internal API which relied on “only our apps know the URL” or a shared key in a config file is not ready for this. In the previous article, we discussed what changes when an API goes public and the architecture with a single APIM front door. In this article, we are going to see how to secure the APIs at the network level, which authentication flow to use for trusted and public clients, and how to manage the API clients and their secrets.
Promoting Internal APIs series:
- How to promote internal APIs to external users? The architecture
- How to secure APIs for external users? Network, authentication and API clients (this article)
- How to manage API access for external users? Roles, consent and the service catalog
TL;DR Summary
- Put every API behind the environment’s virtual network, with NSGs, private endpoints for APIM and no direct internet access.
- Developers access the APIs through the VPN for testing; everything else outside the VNet is blocked.
- Use our Identity Service (backed by IdentityServer) for API authorization; APIM validates the token of every request in both regions.
- Use the Client Credentials flow for trusted server-to-server clients, and Authorization Code + PKCE for public clients like browsers and mobile apps.
- API users create their own clients in the Customer Control Panel, request access to API scopes, and the product’s API Admin approves or rejects the request; client secrets must expire and be renewable by the users.
Security Baseline
The majority of our APIs are hosted using Azure App Service or Azure Functions. In such cases, secure them in the following ways,
- Place your App Service behind the common virtual network of the particular environment.
- Create NSGs and map the required NICs/subnets for your API resources.
- Set up rules so that the APIs can be accessed using Postman when the development machines are connected to the VPN.
- Block all other incoming requests from outside the VNet and outside the subnet range.
- Create private endpoints for access by APIM.
This is the “Private endpoints + NSGs” area in the architecture diagram of Part 1: in each region, APIM is the only component with a path into it. The result is simple: the only ways to reach an API are through APIM (for consumers) and through the VPN (for developers). Even if someone finds the azurewebsites.net address of an API, the request is blocked.
Must Be Aware: Don’t leave the public endpoint of the App Service open “just for testing”. A forgotten open endpoint bypasses all the authentication and rate limiting we apply in APIM.
AuthN and AuthZ
Authentication (AuthN) verifies who the caller is, and Authorization (AuthZ) verifies what the caller is allowed to do. Our Identity Service, backed by IdentityServer, is the preferred service for API authorization. It is an OAuth 2.0 and OpenID Connect provider, so APIM and the APIs only need to validate the access tokens it issues.
As shown in the architecture diagram of Part 1, the Identity Service is shared by both regions, and the APIM instance in each region validates the token of every request with it before the request reaches an API. So, an API never sees a request without a valid token, whichever region serves it.
Recommended Flows
The right OAuth 2.0 flow depends on who the client is and where it runs.
Client Credentials - Trusted Clients
If the APIs are destined to be accessed from a server, it is recommended to follow the Client Credentials flow.
In this flow, the client application authenticates with its own client ID and secret and gets an access token for itself; there is no end user involved. It suits server-to-server integrations, scheduled jobs and back-end services of our customers and partners, because the secret can be kept safely on the server.
Client (server) ── client_id + client_secret + scope ──> Identity Service
Client (server) <──────────── access token ───────────── Identity Service
Client (server) ── Authorization: Bearer <token> ──> APIM (validates token) ──> API
Authorization Code + PKCE - Public Clients
If the APIs are planned to be accessed by end users from public clients/browsers, then,
- The application client should be created with the Authorization Code + PKCE flow.
- The application client is enabled to receive consent from end users (only for 3rd party applications).
- The client’s auth system (Azure AD, SAML server, etc.) is integrated for SSO (Single Sign-On) authentication.
- The application client can be restricted to allow users only from a specific identity provider (identity provider restrictions).
- Our auth system must soft register the users before they begin accessing our APIs, if they are not already registered.
A public client (like a single page application or a mobile app) can’t keep a secret, because anyone can read its code. PKCE (Proof Key for Code Exchange) solves this: the client creates a one-time random secret for each login, sends only its hash with the authorization request, and proves it owns the secret when it exchanges the code for a token. So, even if someone intercepts the authorization code, they can’t use it.
User ── login ──> Identity Service (SSO with the client's identity provider)
App <── authorization code ── Identity Service
App ── code + code_verifier ──> Identity Service
App <── access token (on behalf of the user) ── Identity Service
App ── Authorization: Bearer <token> ──> APIM (validates token) ──> API
Which flow to choose?
| Client | Example | Can keep a secret? | End user involved? | Flow |
|---|---|---|---|---|
| Trusted server | Partner’s back-end integration, scheduled data sync | Yes | No | Client Credentials |
| Public client | SPA in a browser, mobile app, desktop app | No | Yes | Authorization Code + PKCE |
| 3rd party application acting for our users | Partner app used by our customers | Depends | Yes | Authorization Code + PKCE with user consent |
Don’t Do: Don’t use the Client Credentials flow from a browser or a mobile app. The secret will be exposed to anyone who opens the developer tools.
API Clients Management
An API client is the identity of an application which calls our APIs. There are two kinds of clients, and they are managed in two different places,
| Client | Owned by | Managed in |
|---|---|---|
| API and application clients of a customer | The customer’s API User | Customer Control Panel |
| Application clients of the product | The product’s Client Admin | Auth/Resource Control Panel |
We follow these rules for API clients,
- Clients must be created by the API users (internal/external clients) themselves, in the Customer Control Panel.
- A client can’t use an API scope until the API user requests access to it and the product’s API Admin approves the request. The API Admin owns the API resources and scopes of the product and can also reject the request.
- A client secret must have a definite expiry period.
- End users should be able to regenerate secrets and update their applications accordingly.
Self-service client creation removes the manual onboarding, the scope approval keeps the product team in control of who accesses what, and expiring secrets limit the damage of a leaked secret. When users can regenerate the secrets themselves, rotating a secret becomes a routine task instead of a support ticket. We will see the roles, the control panels and the scope request flow in detail in the next article.
Wrap up
In this article, we have looked at the network security baseline for our APIs, the authentication flows for trusted and public clients and how to choose between them, and the rules to manage the API clients and their secrets. I really appreciate that you took the time to read this article. In the next article, I will walk you through how the Subscription Management and the Identity Service work together to manage API access, the API Admin and Client Admin roles, the scope request and approval flow, and the subscription/service catalog portal for external users.
Related Posts
How to manage API access for external users? Roles, consent and the service catalog - Part 3
Manage API access for external users with roles, consent approvals and a service catalog, plus a readiness checklist before an API goes public.
How to promote internal APIs to external users? The architecture - Part 1
Why we promote internal APIs, what changes when an API goes public and the general architecture with Cloudflare, Traffic Manager, Application Gateway and APIM.
Edge-first architecture - security and caching at the CDN / proxy
Edge-first architecture moves security and caching decisions toward Azure CDN or Cloudflare, reducing origin load while covering WAF, TLS, rate limits and origin protection.