Mastering Postman: Cookie and Token Authentication

In this article, we will dive deep into the realms of Cookie Authentication and Token Authentication, two pivotal security strategies.

In the realm of web services and API management, understanding the core mechanisms that secure the communication between the client and server is crucial. In this article, we will dive deep into the realms of Cookie Authentication and Token Authentication, two pivotal security strategies. Our journey will include practical examples using Postman and echoAPI Interceptor, highlighting their implementation and nuances.

Postman.png

What Are Cookies?

A cookie is a small piece of text stored on the client's machine that is used primarily for session management, user personalization, and tracking user behavior. In a key=value format, cookies operate serving as tiny data keepers.

How to use Cookies in Postman?
What are Cookies used for? Cookies are small pieces of data stored on a user’s device by websites they visit. These data files hold information about the user’s interactions with the site, such as login credentials, preferences, and browsing history. Cookies play a vital role in maintaining session state and
image.png

Cookie authentication is a server-side method of maintaining state between HTTP transactions. It works as follows:

  1. Initial Request by Client: During the first visit to a server, if authentication is needed, the server generates a cookie.
  2. Cookie in Response Header: This cookie is sent back with the response, embedded in the Set-Cookie header.
  3. Subsequent Requests: On subsequent requests, the client returns this cookie to the server in the Cookie header, allowing the server to validate the session.

Types of Cookies:

  • Session Cookies: Stored in memory and deleted when the browser is closed.
  • Persistent Cookies: Stored on the hard drive until they expire (as defined by their expiration date) or until the user deletes the cookie.

To inspect cookies, one can use the browser's Developer Tools under the Application panel to see details such as Name, Value, Domain, Path, and Expires/Max-Age.

cookies.jpg

Practical Example: Debugging API Authentication

Let us detail the steps to understand cookie and token authentication via practical debugging:

Tools Used:

  • EchoAPI Interceptor: A modern tool for capturing and inspecting HTTP traffic.
EchoAPI Interceptor.png
Enhance Your Coding with EchoAPI Interceptor: An Exciting API-Capturing Chrome Plugin!
If you tired of using outdated, monotonous API-capturing tools? Meet EchoAPI Interceptor—the Chrome extension that transforms your workflow. Effortlessly capture and debug APIs, and sync them for deeper processing, all within your browser. Best of all, it’s free!

Steps in Action:

1. Setup:

Post login, echoAPI Interceptor automatically captures the traffic including URLs, parameters, and cookies of the session.

img_v3_02iq_53016773-c8be-4279-811f-5b4c945ec37g.jpg

Navigate to the login page at https://app.echoapi.com/login and perform the login with credentials.

img_v3_02iq_dee9da85-e16f-4892-b691-2bc1b48f2dag.jpg

2. Copying curl for API:

Right-click the API and choose to copy as curl.

img_v3_02iq_002452d0-654e-4ba9-aac5-dba69492f94g.jpg

Click on the login API from the captured list, send the request, and ensure it is debuggable.

img_v3_02iq_a9c04c05-0409-4cde-9424-f72c7718ff4g.jpg

3. Importing to Postman:

Bring the curl command into Postman to setup the environment for further requests.

img_v3_02iq_c33e79ae-664f-4706-b194-335a905f379g.jpg

4. Token Setup in Postman:

After sending a request via the login/email_login endpoint, extract the token from the response and set it as a global variable:

var data = JSON.parse(responseBody);
pm.globals.set("token", data.data.token);
img_v3_02iq_3e98f578-836b-4d4e-9e75-dfd5eae1f98g.jpg
How to set an Authorization bearer token in Postman?
When working with APIs that require authentication using bearer tokens, it’s essential to knowHow to set authorization bearer in Postman. Follow these simple steps to ensure your requests are properly authenticated: Open Postman Application Launch the Postman application on your device to begin the process. 1. Create a New Request

5. Using Token in Headers:

  • Switch to the api/online endpoint in Postman.

In the Headers section, replace the value of echoapitoken with the token variable {{token}} acquired earlier, send the request, and observe the output.

img_v3_02iq_d719af85-13fe-4b35-854c-93b9af9fdefg.jpg

The result indicates failure, as the API requires authentication via tokens, hence validating the different authentications in use.

img_v3_02iq_4cbcd275-2ee6-4922-9cf2-a9b5d763bd9g.jpg

Evaluate attempting a request to api/online with all cookies deleted or disabled.

img_v3_02iq_fd50f08f-beae-4b0a-8e64-9fb2161282bg.jpg
EchoAPI Interceptor: Revolutionizing API Data Management
The Struggle with API Data Management In my daily work routine, engaging with various APIs is a common occurrence. During the development and testing phases, capturing and analyzing API data becomes an essential step. Traditionally, every time I made an API call, meticulous recording of requests and responses was imperative,

Conclusion

Through this comprehensive guide and practical example, you have learned about the mechanisms of Cookie and Token authentications, how to inspect and manipulate these using tools like Postman and echoAPI Interceptor. Cookie and Token Authentication serve different purposes and scenarios where the former manages sessions and the latter secures API calls via tokens that represent user credentials or session states.

Understanding and implementing these authentication mechanisms properly can bolster the security and functionality of web applications and services, providing a more secure and responsive user experience.