Nowadays, REST APIs are widely used for building APIs due to their many benefits such as simplicity, scalability, ease of use, and statelessness. REST APIs are commonly implemented using HTTP, making them accessible through standard HTTP requests and responses.
In general, when we build an API, several factors need to be considerd. Usage, scalability, performance, versioning, and security are some of them. Among them, securing APIs is crucial in API access management. A secured API not only prevents unauthorized access but also offers numerous other advantages such as integrity maintenance and regulatory compliance. As technology advances, API developers should continually strive to fortify their APIs against potential threats from unintended sources.
When we consider API security, it’s important to manage it at different layers such as the transport layer and application layer. As the REST APIs are used using the HTTP that operates at the transport layer (OSI Model - Layer 4), we can enhance the security of the transport layer by using HTTPS. HTTPS helps to have end-to-end encryption in the wire when the API requests and response are transmitted over the network. On the other hand, when dealing with the application layer security, the developers have to think about authentication, authorization, protection against malicious attacks, rate limiting, as well as service availability in the face of attaks such as DoS and DDoS. The main focus of this article is to give a basic understanding about securing the APIs and how Authentication and Authorization can help to make this successful.
Authentication is the process of verifying the identities of users or clients based on a particular security implementation. To prove their identity, users or clients must provide credentials, which the security system then evaluates to determine their validity. Primarily, the authentication process allows us to accomplish the following:
The authentication process can be implemented in different ways and it can be varied based on the requirement. Let’s explore some of the widely used implementations!
This is a common approach used in REST where the client sends the API Key along with all the requests (most often the key is sent as a header or a query parameter, which is not safe) and the server validates the API Key and checks whether the request can be authorized. In this approach, usually the API provider generates a unique API key for each client that grants access to the APIs. Following are some of the factors we have to consider when the API Key authentication is used for REST APIs:
Here is a simple sequence diagram that depicts the successful flow of this authentication mechanism.
With this approach, the username and password will be concatenated (username:password) and encoded with base64 and sent along with each API request. The credentials sent will be validated against the credentials that are saved in the server to grant access. Usually, the ‘Authorization’ header is used to send the credentials where the encoded credentials are prefixed with the word ‘Basic‘, e.g., Basic bXl1c2VxOm15dGFzc3dvrmQ=. There are certain factors to consider when using this approach to authenticate the APIs:
Here is a simple sequence diagram that depicts the successful flow of this authentication mechanism.
In this approach, the client will receive a token, e.g., OAuth2 token and JWT, from the authentication process. That token will be sent to the server along with each request. Usually, the token is sent with the ‘Authorization’ header and the token will be prefixed by ‘Bearer ‘. The server checks the token validity and provides the access to the resources if the token is valid. Under Token Authentication, we will briefly examine OAuth2 and JWT authentication.
This is a widely used approach for authentication and authorization of REST APIs. With this method, the third-party application can access data on behalf of users without revealing the credentials. OAuth2 is a result of a collaboration of multiple components/parties:
As discussed above, the authentication server generates the access token and provides it to the client. The client uses the access token in order to access the APIs in the resource server. Usually the tokens are short lived and can have different scopes as per the client needs. In OAuth 2, the ‘grant type’ is used to address different use cases:
With the collaboration of the above components, let’s discuss how we can retrieve an access token using OAuth2 using the ‘Authorization Code’ grant type. Please note that it is nearly similar to other grant types (of course there are some slight differences in each scenario):
The following sequence diagram shows how a typical OAuth2 request works for the grant type as ‘Authorization Code’.
OAuth2 is widely used and secure and suitable for various use cases. We need to have a careful implementation in order to avoid security vulnerabilities. We need to have a proper attention on token management, token expiration, and user consent for a proper OAuth2 implementation.
JSON Web Token (JWT) is a compact, self contained token. This token is used for implementing the authentication as well as authorization in web applications. A Typical JWT consists of three parts:
To create the token, we need to follow the following steps (usually we can use third-party libraries to generate the tokens by providing the required information)
After the token is created, we can use it for authenticating the APIs. Usually the token is sent along with the ‘Authorization’ header with the token prefixed with ‘Bearer ‘. The most important things to remember when using JWT tokens are:
The following diagram indicates how the JWT token is generated and utilized for API execution.
Even though token based authentications like OAuth2 and JWT are good authentication mechanisms, there are facts that we need to take care when we use it. Here are some points that need to be taken care of.
HMAC involves hashing the request data and using a shared secret key to create a signature. The server verifies the signature to authenticate the request.
This authentication approach is somewhat similar to Basic Authentication. But, this is more secure than Basic Authentication. This uses a challenge-response mechanism with hashing for authentication.
Apart from the above-mentioned authentication types, there are other implementations like Biometric authentication and certificate-based authentication. We can also implement a custom authentication mechanism by combining one or few of above-mentioned authentication types with additional steps. To strengthen the security, it is better to introduce a secondary verification method using multi-factor authentication
Authentication itself cannot fully secure an application. For example, the users with generic permissions should not be able to access the admin level features like user management. Here’s where the Authorization comes into the picture. Authorization is the process of granting (or denying) access to specific resources, data, or actions based on the permissions/role entitled as per the authentication. Some of the benefits/purposes of authorization can be be listed as follows:
As we discussed above, authorization can be split into two types:
Let’s discuss further on these to get more understanding how each of them handle the authorization for the logged in user!
In Role-Based Authorization (RBA), the access permissions are granted to a role rather than assigning them directly to a user/client. There could be multiple roles having different permission levels and a user will get assigned one or more roles. Once a user is authenticated, the permissions are retrieved based on the role(s) assigned to the user. RBA simplifies the access control and managing the roles easily when compared to the Attribute-Based Authorization. The advantage of this approach is, instead of updating access of users individually, we only need to update the role to control the access of multiple users at once.
In Attribute-Based Authorization (ABA), the access permissions are granted based on the attributes that are associated with the user. This depends on the resources or actions to be accessed and also the context associated with the request. This provides a fine-grained access control to the user rather than the role based authorization.
With the advancement of technology, new mechanisms for Authentication and Authorization may emerge. These could range from improvements in multi-factor authentication and biometrics to areas we have not yet imagined. Additionally, as technology, especially in areas like quantum computing, continues to develop, new types of threats may arise daily. Thus, applications should be adequately protected against these emerging threats, and we must remain proactive in identifying potential future challenges.
API Design-First (also known as API-first design or API-driven development) is the approach for designing the API specification before writing the code for actual implementation. Adhering to API-driven development gives us a lot of advantages.
There are different tools that can be used for implementing the API Contract (API Specification) in API Design-First approach for authentication and authorization. Xapi is such a great tool fully compliant with Open API Specification 3.1.0 and can be used to achieve design specification and testing.
Authentication and Authorization are among the most critical areas to address when safeguarding data manipulated through APIs. Protection for the APIs and data should be continuously updated to defend against the emerging threats that arise daily.