Authentication and API Security
An API is a published contract between applications that specifies the exact rules for requesting and receiving services.
A Feature Built from Services
Imagine a travel booking application that must search flights, check hotel availability, process payments, and manage user accounts. One design is to place all of this functionality inside one application. Another design is to use specialized services: a flight-data service, a hotel-inventory service, a payment service, and a user-account service. The application can call these services and combine their responses into one user experience.
The API Contract
An Application Programming Interface, or API, is a published contract between applications that specifies the exact rules for requesting and receiving services.
The word contract is important. A service's API tells another application how to make a request and what format the response will have. The calling application does not need to know how the service performs its work internally. It needs to follow the published rules.
Following One Request
To understand service integration, trace one request from the application to a service and back. The application first determines what service functionality it needs. It then creates a request that follows the service's published API rules. The request can include what is being asked for, parameters that customize the request, and authentication credentials if the service requires them.
- The application identifies the service it needs.
- The application sends a request that follows the service's API contract.
- The request includes the requested operation, any parameters, and authentication credentials if required.
- The service validates the request against its API contract.
- The service processes the request and sends a response.
- The response contains requested data in a standardized format such as JSON or XML, along with a status code indicating success or failure.
- The application parses the response and uses it to continue its work.
Tracing a travel booking feature
How can one travel application use several independent services to support a larger booking goal?
Choose services: The application uses a flight-data service, a hotel-inventory service, and a payment service instead of implementing all of those functions itself.
Follow each contract: For each service, the application sends requests according to that service's published API contract.
Receive responses: Each service returns data in the response format specified by its contract, together with a status code indicating whether the request succeeded or failed.
Compose the result: The application acts as the orchestrator: it knows which services to call, in what order, and how to combine their responses into a cohesive user experience.
Independent services can contribute to one application feature when the application follows each service's published request and response rules.
Rules That Enable Integration
Integration works because both sides can rely on the same published rules. The application knows what request information to provide. The service knows how to validate that request and what response structure to return. The application can then parse the standardized response without needing to understand the service's internal implementation.
This arrangement also separates responsibilities. A hotel service team can improve its search algorithm without touching flight-search code. A payment service can upgrade its security without affecting hotel-availability checks, provided the service interaction continues to follow the published contract.
Authentication in the Request
Authentication belongs to the request-and-response interaction when a service requires it. The source specifies that a request can include authentication credentials. The service then validates the request against its API contract, processes the request, and returns a response with requested data and a status code indicating success or failure.
Mistakes in Service Integration
Treating an API as the service's internal implementation.
An API is the published contract for requesting and receiving services. The service can keep its internal implementation separate.
Fix:
Use the documented request and response rules without depending on the service's internal details.Calling a service without following its contract.
The service validates requests against its API contract, and the calling application relies on the published response format.
Fix:
Read and follow the service's published rules for requests, parameters, credentials if required, response data, and status information.Assuming that several services automatically form one application.
In the source example, the application acts as the orchestrator and coordinates the services.
Fix:
Design the application to orchestrate service calls and combine their responses into a cohesive user experience.Ignoring the cost of network dependence.
SOA also introduces network dependencies and distributed system complexity.
Fix:
Recognize both the modular benefits and the additional complexity of coordinating independent services across a network.
Practice the Trace
A travel application needs hotel availability from an independent hotel service. Describe the request-and-response trace. Include the API contract, request information, authentication credentials if required, service validation, response data, status information, and the application's next step.
Hints
- Start with the rules published by the hotel service.
- Separate what the application sends from what the service returns.
- Remember that the application parses the response and uses it to continue its work.
What do you think happens?
Before reading the answer, predict whether the calling application must understand the hotel service's internal implementation in order to request hotel availability.
Reveal answer
Answer: No, it needs to follow the published API contract.
Each service maintains an API contract that allows applications to request functionality without needing to understand the service's internal implementation.
Essential Takeaways
- An API is a published contract between applications that defines the rules for requesting and receiving services.
- A monolithic application is a single self-contained application, while SOA composes an application from multiple independent services connected across a network.
- Each service can expose its own contract, allowing a calling application to use the service without understanding its internal implementation.
- The calling application orchestrates service requests, receives standardized responses and status information, and combines the results into a larger feature.
- Authentication credentials may be included in an API request when the service requires them; the exact security mechanism must come from the service's published rules.
Key Takeaways
- An API defines an agreed contract between applications.
- SOA uses multiple independent services, whereas a monolith is a single self-contained application.
- Network-connected services can be orchestrated to produce one larger application feature.
- Published request and response rules make integration possible without exposing internal implementations.
- Authentication credentials are part of an API request when the service requires them.