REST API testing using Basic Authentication in Postman
Basic
Authentication in Postman
We need Authorization for accessing secured
servers. Authorization is the most important part while
working with secured servers, which is most likely to happen. We will learn
about
·
What is Authorization?
·
Authorization vs Authentication
·
Need for Authorization
·
Basic Authentication in Postman
What is Authorization?
The meaning of authorization be a question which is, are we eligible to access
a secured resource on the Server? If the
answer is yes, then in technical terms we can say that we are Authorized to
access the resource. If the answer is No, we can say that we are not Authorized to
access the resource.
For example, let us say you
have added yours and your sister’s fingerprint in your phone. You and your
sister can open the same mobile phone, which means only you and your sister are
authorized to open the phone and see the data.
Similarly, while there could be many APIs in a
company or a project. It is not necessary that everyone have access on all the
APIs. Only authorized people can access the secured APIs.
Authorization
Vs Authentication
Authorization and Authentication are two
closely related terms. These two terms can also be confusing at first.
Authentication is
a process of presenting your credentials to the system and the system
validating your credentials. These credentials tell the system about who you
are. Which enables the system to ensure and confirms a user’s identity. Here system can be
anything, it can be a computer, phone, bank or any physical office premises.
Whereas Authorization is
a process of allowing or denying someone from accessing something, once Authentication is
done. So in layman terms Authentication tells
who you are while Authorization tells what you can do.
When a person accesses the
server with the key/password, the server checks whether the person
is available in directory and is also associated with the same key/password.
If it is, you are good to go (Authentication). If you have access to the
resource, then you will be granted access to the resource (Authorized).
Authorization using Postman
Checking
Authorization
We will be using the endpoint https://postman-echo.com/basic-auth
1.Create a GET request and enter the endpoint as https://postman-echo.com/basic-auth
Press send and look at the response
Note: The status code
is 401 which corresponds to unauthorized access and the
response message says Unauthorized.
The status code and response from the server indicates that we are not
authorized to access the API we are trying to access
Need for
Authorization
In the last section
we discussed that a resource owner does not allow access to the resources to
everyone in the company. This
is because it can lead to possible security breach. If I allow an intern to access my
database apis then inadvertently he can change the data and that data can be
lost forever which can come as a cost to the company. There are numerous reasons possible
for the same. Maybe a person changes the data for money or a person can leak
the data to another company.
Authorization plays a
very important role in deciding the accesses and tightening the security. Let us
see the different types of Authentication available to us.
Basic
Access Authentication / HTTP Basic Authentication
A Basic
Access Authentication is the most simple and basic
type of authorization available. It requires just a username and password for
checking the authorization of any person (That is why we say basic access authentication). The username and password are sent
as header values in the Authorization
header. While using basic authentication
we add the word Basic before
entering the username and password. These username and password
values should be encoded with Base64 otherwise the server won’t be able to
recognize it.
We will follow these steps to check whether we can access the
same API we used above or not
Checking authorization using credentials
1.Enter the endpoint https://postman-echo.com/basic-auth
in GET request.
2.Go to Headers
3.Enter the
following key value pairs in Header
Authorization
: Basic postman:password
Note: We are using
username as postman and password as password
4.Press Send and see the response box and status
code.
It still says 400 , Bad Request.
Can you guess why? If you remember
what we learnt in last section, a basic access authentication requires username and password to be
encoded in Base64 but here we just sent the username and password in plain
text. As a result, server returned a 400, Bad Request status code. Before
we move forward it will be beneficial to understand what Base64 encoding
is.
What is
Base64 encoding?
An encoding is used in the authentication because we don’t want our data
to be transmitted directly over the network. There is a numerous reason for
that. Network scanners can read your Request and retrieve the Username and
Password sent without encoding. Also, bits and bytes transmitted directly can
be considered as inbuilt command bits by the modem or other equipment in the
network chain.
We use base64 particularly because it transmits the
data into textual form and send it in easier form such as HTML form data. We use Base64 particularly
because we can rely on the same 64 characters in any encoding language that we
use. Although we can use higher base encoding methods also, but they are very
hard to convert and transmit which wastes time unnecessarily.
We have two ways in front of us for creating a Base64encoded
string:
·
Through third party website
·
Through Postman
We will see both
options one by one. For now, follow the steps for accessing the api by decoding
from third party website.
Authenticating by encoding through third party website
1.Go to https://www.base64encode.org/
Note: There are thousands of
websites available for the same purpose. You can use anyone just make sure you
encode to the same value as us. Also, we are using Microsoft Edge as
the browser, though it should not make any difference.
2.Paste in the box
the following values
postman:password
3.Press Encode.
4.Copy the encoded
text.
Note: Do not use space in
between any two texts or symbols. postman : password will
encode to a different value while postman: password will
encode to a different one. Both will be considered wrong. Use postman:password only.
5.Go to the postman app and instead of postman:password,
paste the encoded value
6.Press send and see the value of the response
box and the status code.
200 OK authenticated means we have provided correct
credentials and now we are authorized to access the data.
Authenticating by encoding through Postman
Instead of going to a third-party website we will try to encode
using Postman.
1.Erase the key value pair that we entered earlier so that it now
has no values.
2.Go to the authorization tab
3.Select Basic Auth in
the Type dropdown
4.Enter username
as postman and
password as password
5.Press Preview Request
6.Go to Header and
see that Postman has converted the username and password for you.
7.Press send, and
we are authenticated.
In this
blog, we have understood that how to do REST API testing using Basic
Authentication in Postman. Thank you for reading my blog.
This blog will help you to understand the difference between Authentication and Authorization. How to perform Basic Authentication in Postman to test REST API. Thank you.
ReplyDelete