Concepts / Authentication and API Security

Authentication and API Security

An API is a published contract between applications that specifies the exact rules for requesting and receiving services.

  • Programming

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.

API contractAPI contractAPI contractAPI contractMonolithicapplicationflight, hotel, payment,accountsFlight serviceflight dataService-orientedapplicationmultiple independentservicesHotel servicehotel inventoryPayment servicepayment processingAccount serviceuser accounts
What is the difference between one application containing all functionality and multiple independent services working together?

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.

follows rulesmay includeservice returnsRequestwhat is being requestedParametersrequest customizationCredentialsif requiredResponsedata and status
What rules specify what an application must send and what service it will receive in return?

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.

request flight dataflight responserequest hotel inventoryhotel responserequest payment processingpayment responsecombines responsesTravel applicationorchestratorFlight serviceflight dataHotel servicehotel inventoryPayment servicepayment processingBooking experiencecombined result
How does a request travel between separate services, and how do their responses combine to produce one application feature?
  1. The application identifies the service it needs.
  2. The application sends a request that follows the service's API contract.
  3. The request includes the requested operation, any parameters, and authentication credentials if required.
  4. The service validates the request against its API contract.
  5. The service processes the request and sends a response.
  6. The response contains requested data in a standardized format such as JSON or XML, along with a status code indicating success or failure.
  7. 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

defines requestdefines responserequestreturnsPublished APIcontractrequest and response rulesApplicationfollows request rulesStandardized responsedata and status codeIndependent servicevalidates and processes
How can independently developed services connect successfully when they follow the same documented interface and data rules?

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

providesmay be includedsent across networkreturnsApplicationrequesting clientAuthenticationcredentialsif requiredAPI requestrequested serviceServicecontract validationAPI responsedata and status
What happens when a client identifies itself, sends credentials or a token, and requests access to a protected API?

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

MEDIUM

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.

  • Yes, it must understand the internal implementation.
  • No, it needs to follow the published API contract.
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

  1. An API is a published contract between applications that defines the rules for requesting and receiving services.
  2. A monolithic application is a single self-contained application, while SOA composes an application from multiple independent services connected across a network.
  3. Each service can expose its own contract, allowing a calling application to use the service without understanding its internal implementation.
  4. The calling application orchestrates service requests, receives standardized responses and status information, and combines the results into a larger feature.
  5. 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.