chapter seven

7 Adding Authentication and Authorization

 

This chapter covers

  • Identifying the difference between authentication and authorization.
  • Adding operations for creating users.
  • Adding an operation for getting a user’s token (authentication).
  • Adding the Authorization header to POST /reviews operation (authorization).

We’re going to be looking at authentication and authorization, two close friends in APIs that are often a little misunderstood. Authentication is about proving you are, who you say you are—which could be through a username and password. While authorization is about being allowed access to special actions or resources that are normally require it, eg: getting user details or creating a new review.

APIs almost always include a form of authorization and authentication, so naturally describing them is important. In today’s world we have multiple standards dealing with authorization, each with different tradeoffs and strengths, and we should communicate to our consumers which of these standards we use.

Personally, one of the biggest hurdles to using an API, is getting authorization to work. I’ve often found myself wading through oodles of documentation, searching for how to get access to consume it! OpenAPI makes it easier, by being explicit in what Authorization is needed.

At the end of this chapter you’ll be able to describe simple security schemes for authentication/authorization and add them to operations in OpenAPI.

In our FarmStallAPI we’ll be adding

7.1 The problem

7.1.1 The flow of POST /reviews

7.2 Getting set up for authentication

7.2.1 Challenge - Describe POST /users and POST /tokens

7.2.2 Solution - Definition changes

7.2.3 Verifying we can create users and get a token

7.3 Adding the Authorization header

7.3.1 How does OpenAPI handle authorization

7.3.2 Types of authorization (securities) supported in OpenAPI 3.0.x

7.3.3 Adding the Authorization header security scheme

7.3.4 Adding the security requirements to POST /reviews

7.3.5 Using the security feature of try-it-out

7.4 Other types of security schemas

7.5 How to add security schemes in general

7.6 Summary