<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alex-husar</title>
    <description>The latest articles on DEV Community by Alex-husar (@alexhusar).</description>
    <link>https://dev.to/alexhusar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F782490%2Fb1a5b296-483f-4c68-b3df-f65788056948.jpeg</url>
      <title>DEV Community: Alex-husar</title>
      <link>https://dev.to/alexhusar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexhusar"/>
    <language>en</language>
    <item>
      <title>How to Add User Authentication in Magento</title>
      <dc:creator>Alex-husar</dc:creator>
      <pubDate>Thu, 30 Dec 2021 15:22:51 +0000</pubDate>
      <link>https://dev.to/alexhusar/how-to-add-user-authentication-in-magento-55p6</link>
      <guid>https://dev.to/alexhusar/how-to-add-user-authentication-in-magento-55p6</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;How to Add User Authentication in Magento&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Before using the API, the system will ask you to authenticate. Why do you need user authentication in Magento? It helps to protect data from unwanted third-party users.&lt;/p&gt;

&lt;p&gt;Authentication allows Magento to determine the caller’s user type and the rights to access API requests. We make sure that the user has the required privileges, for example, to edit the product catalog or configure any other feature on your website or &lt;a href="https://onilab.com/blog/magento-headless-commerce-explained/" rel="noopener noreferrer"&gt;Magento headless commerce solution&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this post, I’ll go through the Magento 2 API &lt;a href="https://dev.to/propelauth/understanding-user-authentication-from-scratch-3pl2"&gt;authentication process&lt;/a&gt;. I will talk about Token, OAuth, and Session Authentication. But before we start, I’ll introduce you to Magento.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Short Introduction to Magento&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Magento is an eCommerce engine aiding medium-sized and large online businesses in the creation of a distinctive shopping experience.&lt;/p&gt;

&lt;p&gt;Magento is a PHP-based open-source platform currently owned by Adobe. It means you can change and customize it to meet your specific requirements. This platform stands out because of its flexibility in terms of custom development and idea feasibility.&lt;/p&gt;

&lt;p&gt;However, it lacks a built-in visual editor, making it difficult for newbies. Because Magento is a platform for expert users, you’ll almost certainly need to employ a Magento development service provider to get a store up and operating.&lt;/p&gt;

&lt;p&gt;Managing a store isn’t difficult. When you put everything up, you can add new categories, pages, and products, as well as change them directly in the admin panel. You may use it to manage add-ons, create templates, and much more.&lt;/p&gt;

&lt;p&gt;You can download and configure Magento’s Open Source edition for free, which is the choice for &lt;a href="https://litextension.com/blog/magento-2-review/" rel="noopener noreferrer"&gt;83% of Magento stores&lt;/a&gt;. However, if you require more advanced features, you can upgrade to paid Magento Commerce edition or Magento Commerce Cloud.&lt;/p&gt;

&lt;p&gt;Magento is a powerful solution for large businesses with a high volume of visitors and a high turnover.&lt;/p&gt;

&lt;p&gt;For example, Magento Commerce can handle 350 million catalog views and 487,000 orders daily. Ahmad Tea, Nestle Nespresso, Land Rover, and other well-known and high-traffic online Magento stores are just a few examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Defining XML Elements and Attributes&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Where can you establish web API resources and associated permissions in Magento? There is the &lt;code&gt;webapi.xml&lt;/code&gt; configuration file. This file is used to register our API routes and specify the rights, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;indicating the URL;&lt;/li&gt;
&lt;li&gt;the method (&lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;SAVE&lt;/code&gt;, and so on);&lt;/li&gt;
&lt;li&gt;interface, where our processes are registered;&lt;/li&gt;
&lt;li&gt;resources, i.e., who has access to the API (&lt;code&gt;anonymous&lt;/code&gt;, &lt;code&gt;self&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The table below shows the resources each user type can reach:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
   &lt;td&gt;
&lt;strong&gt;Type of User&lt;/strong&gt;
   &lt;/td&gt;
   &lt;td&gt;
&lt;strong&gt;Available Resources&lt;/strong&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Administrator or Integration
   &lt;/td&gt;
   &lt;td&gt;Resources with admin or integrator authorization. Suppose administrators are entitled to the &lt;code&gt;Magento_Customer::manage&lt;/code&gt; resource. It means they can make a &lt;code&gt;PUT /V1/customers/:customerId&lt;/code&gt; call.
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Customer
   &lt;/td&gt;
   &lt;td&gt;Access to resources with &lt;code&gt;anonymous&lt;/code&gt; or &lt;code&gt;self &lt;/code&gt;permission
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;Guest user
   &lt;/td&gt;
   &lt;td&gt;
&lt;code&gt;anonymous &lt;/code&gt;permission
   &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Steps to Add User Authentication in Magento&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There are three types of authentication in Magento: Token, OAuth, and Session authentication. Token and OAuth are roughly the same things. But for OAuth, you need to log in first and receive an access token for your account.&lt;/p&gt;

&lt;p&gt;Or you can simply create a token that will have certain rights and doesn’t &lt;a href="https://dev.to/ubahthebuilder/user-authentication-vs-user-authorization-what-do-they-mean-in-back-end-web-development-18bb"&gt;require authorization&lt;/a&gt; with Token Authentication. Each subsection below will tell you how to configure them in steps.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Token Authentication&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Token-based authentication is preferable for registered users making web API calls using a &lt;strong&gt;mobile application&lt;/strong&gt;. What is a token? It’s an electronic key for accessing API(s).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A registered user requests** a token** from the token service at the endpoint. Note that this endpoint should be defined for your user type.&lt;/li&gt;
&lt;li&gt;Once the token service receives a Magento account username and password, it returns a unique &lt;strong&gt;authentication token&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Insert this token in the &lt;code&gt;Authorization&lt;/code&gt; request header as proof of your identity on web API calls.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are three types of access tokens by Magento, which differ in terms of longevity: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integration \&lt;br&gt;
**It&lt;/strong&gt; &lt;strong&gt;doesn’t have time restrictions, and the access granted by the merchant lasts forever **until it is manually revoked&lt;/strong&gt;. \&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Admin&lt;/strong&gt; \&lt;br&gt;
The merchant determines an admin user’s access to Magento resources, lasting &lt;strong&gt;four hours&lt;/strong&gt;. \&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Customer \&lt;br&gt;
**Such&lt;/strong&gt; &lt;strong&gt;tokens are valid for **one hour&lt;/strong&gt;. Users with &lt;code&gt;anonymous&lt;/code&gt;or &lt;code&gt;self&lt;/code&gt;authorization get access to resources from Magento. These options are not editable by merchants.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since the token is only valid for a while, we need to ask for it again when it expires.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1. Integration Tokens&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;What happens when a merchant creates and activates an integration? Magento generates the following credentials:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;consumer key;&lt;/li&gt;
&lt;li&gt;consumer secret;&lt;/li&gt;
&lt;li&gt;access token;&lt;/li&gt;
&lt;li&gt;access token secret.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of them are also relevant for OAuth-based authentication, but &lt;strong&gt;Token-based authentication&lt;/strong&gt; simply requires the &lt;strong&gt;access token&lt;/strong&gt;, and that’s how you can create it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Access the Integrations page. Log in to Admin and go to &lt;strong&gt;System *&lt;em&gt;&amp;gt; **Extensions *&lt;/em&gt;&amp;gt; **Integrations&lt;/strong&gt;. \&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To access the New Integration page, click &lt;strong&gt;Add New Integration&lt;/strong&gt;. \&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Proceed to the &lt;strong&gt;Name **field and give the integration a unique name. Type your admin password in the **Your Password&lt;/strong&gt; section. Don’t fill in other fields. \&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Navigate to the &lt;strong&gt;API tab&lt;/strong&gt;, where you can choose the access to Magento resources for the integration (all resources or a custom list). \&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After saving your modifications by clicking the **Save **button, return to the Integrations page. \&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find the grid of the newly-created integration, click the &lt;strong&gt;Activate **link, and select **Allow&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You will see a dialogue like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbnqegoy7xknoex805bo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbnqegoy7xknoex805bo.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1pc0i231rzt9xdcgbmb0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1pc0i231rzt9xdcgbmb0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The access token can be used for any calls performed by the integration.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 2. Admin and Customer Access Tokens&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Administrators and customers each have their own token service in Magento. When you ask one of these services for a token, you’ll receive a unique access token in exchange for your Magento account’s username and password.&lt;/p&gt;

&lt;p&gt;Guest users can access resources defined with the &lt;code&gt;anonymous&lt;/code&gt;permission level using the Magento web API architecture.&lt;/p&gt;

&lt;p&gt;Who are guest users? These are users who can’t be authenticated using the framework’s existing authentication procedures. They don’t need to specify a token in a web &lt;a href="https://dev.to/endymion1818/how-to-test-javascript-api-calls-187k"&gt;API call&lt;/a&gt; for a resource with anonymous authorization, but they can.&lt;/p&gt;

&lt;p&gt;Magento admins must be sure to authenticate using &lt;strong&gt;two-factor authentication&lt;/strong&gt;. General users don’t need it, so they have a different authorization API. You can realize your APIs for authorization or enable authorization through a third-party service, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Duo Security;&lt;/li&gt;
&lt;li&gt;Google Authenticator;&lt;/li&gt;
&lt;li&gt;U2F;&lt;/li&gt;
&lt;li&gt;Authy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Customer calls for REST and SOAP will look the following way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;REST: &lt;code&gt;POST /V1/integration/customer/token&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;SOAP: &lt;code&gt;integrationCustomerTokenServiceV1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Include this token in the &lt;code&gt;Authorization&lt;/code&gt; request header with the &lt;code&gt;Bearer&lt;/code&gt;HTTP authorization scheme to establish your identity. As I’ve mentioned, an admin token is valid for four hours by default, while a customer token remains operative for one hour. You can change the default settings from the Admin menu like this: &lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;Stores *&lt;em&gt;&amp;gt; **Settings *&lt;/em&gt;&amp;gt; *&lt;em&gt;Configuration *&lt;/em&gt;&amp;gt; *&lt;em&gt;Services *&lt;/em&gt;&amp;gt; *&lt;em&gt;OAuth *&lt;/em&gt;&amp;gt; **Access Token Expiration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All expired tokens are removed by a cron job that runs every hour.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 3. Inquire a Token&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;A request for an access token has three essential components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Endpoint&lt;/strong&gt; \
It combines the server making the request, the web service, and the &lt;code&gt;resource&lt;/code&gt;to which the request is addressed. \
\
Let’s take this endpoint as an example: \
&lt;code&gt;POST &amp;amp;lt;host&amp;gt;/rest/&amp;amp;lt;store_code&amp;gt;/V1/integration/customer/token&lt;/code&gt;. \
Here, the server is &lt;code&gt;magento.host/index.php/&lt;/code&gt;, the web service is &lt;code&gt;rest&lt;/code&gt;, and the resource is &lt;code&gt;/V1/integration/customer/token&lt;/code&gt;.&lt;code&gt;\
&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Content type&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It concerns the request body. There are two options to set this value: &lt;code&gt;"Content-Type:application/json"&lt;/code&gt; or &lt;code&gt;"Content-Type:application/xml"&lt;/code&gt;. \&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Credentials&lt;/strong&gt; \&lt;br&gt;
This is a Magento account’s username and password. Include code in the call to specify these credentials in a JSON request body: &lt;code&gt;{"username":"&amp;amp;lt;USER-NAME&amp;gt;;", "password":"&amp;amp;lt;PASSWORD&amp;gt;"}&lt;/code&gt;. \&lt;br&gt;
\&lt;br&gt;
If you need to indicate these credentials in XML, use this code in the call: &lt;code&gt;&amp;amp;lt;login&amp;gt;&amp;amp;lt;username&amp;gt;customer1&amp;amp;lt;/username&amp;gt;&amp;amp;lt;password&amp;gt;customer1pw&amp;amp;lt;/password&amp;gt;&amp;amp;lt;/login&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s an example of the &lt;code&gt;curl&lt;/code&gt;command to request a token for an admin account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -H "Content-Type: application/json" \
--request "POST" \
--data '{"username":"&amp;lt;username&amp;gt;","password":"&amp;lt;password&amp;gt;"}' \
https://&amp;lt;magento_host&amp;gt;/index.php/rest/V1/integration/admin/token
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 4. Authentication Token Response&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The response body with the token will look like this, provided the request is successful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6yivz6jrmo147x4skq0xt1ights6siob
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh7uiwbgxok9f6gdsu3t0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh7uiwbgxok9f6gdsu3t0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 5. Utilizing the Token in a Web API Call&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;You need the authentication token when you access the resource that requires a permission level higher than “anonymous”. Include it in the header of any web API call, using the following HTTP header format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Authorization: Bearer &amp;lt;authentication token&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;a) Admin Access&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Admins have full access to all resources for which they received permission. Here is how you perform a web API call with an admin token:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -X GET https://&amp;lt;magento_host&amp;gt;/index.php/rest/V1/customers/29171 \ 
-H "Authorization: Bearer 6yivz6jrmo147x4skq0xt1ights6siob"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytm0nfy4qhwk6pb3ery8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytm0nfy4qhwk6pb3ery8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b) Customer Access&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unlike admins, customers can’t access all resources other than with &lt;code&gt;self&lt;/code&gt; permissions. The following code explains how to use a customer token to make a web API call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -X GET https://&amp;lt;magento_host&amp;gt;/index.php/rest/V1/customers/me \ 
-H "Authorization: Bearer 6yivz6jrmo147x4skq0xt1ights6siob"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakqjqnlsjl41e2leyror.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakqjqnlsjl41e2leyror.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. OAuth Authentication&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s talk about the Magento OAuth authentication process. This type of authentication is based on OAuth 1.0a, a secure API authentication open standard. OAuth is a token-passing technique to specify access for &lt;strong&gt;third-party applications&lt;/strong&gt; to internal data. It’s done without revealing or storing user IDs or passwords.&lt;/p&gt;

&lt;p&gt;Such a third-party application using OAuth for authentication is known as **integration **in Magento. OAuth authentication determines the resources that the application can access. For example, you can enable all resources or restrict the list.&lt;/p&gt;

&lt;p&gt;To illustrate my point, suppose you use Mailchimp to notify your store visitors about the abandoned carts. When a visitor leaves your website with an unpaid order, Mailchimp needs to obtain a list of such clients, the contents of their carts, and email addresses. As a store owner, you specify the Mailchimp rights with the help of OAuth authentication.&lt;/p&gt;

&lt;p&gt;That’s how Magento generates the tokens needed for authentication:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It starts by generating a &lt;strong&gt;request token&lt;/strong&gt;. \&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This token is usable for a short time and must be exchanged for an &lt;strong&gt;access token&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Access tokens have a lengthy lifespan and expire only when the merchant revokes the application access.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1. OAuth Overview&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The OAuth authentication process takes ten steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Creating an integration&lt;/strong&gt; from Admin. The merchant builds an integration, while Magento generates a &lt;strong&gt;consumer key&lt;/strong&gt; and a &lt;strong&gt;consumer secret&lt;/strong&gt;. \&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The next step is &lt;strong&gt;activating the integration&lt;/strong&gt;, which starts the OAuth process. Magento uses HTTPS post to transmit the following attributes to the external application: \&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;OAuth consumer key and secret;&lt;/li&gt;
&lt;li&gt;OAuth verifier;&lt;/li&gt;
&lt;li&gt;the store URL. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These credentials go to the page indicated in the Callback Link field in Admin.&lt;/p&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;The integrator &lt;strong&gt;receives the activation information&lt;/strong&gt; and saves it to ask for tokens. &amp;lt;/p&amp;gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Magento &lt;strong&gt;accesses the application login page&lt;/strong&gt; specified in the Admin Identity Link field. &amp;lt;/p&amp;gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;The merchant &lt;strong&gt;logs in to the third-party application&lt;/strong&gt;, which will integrate with Magento. The application returns to the call location in case of a successful login. The login page doesn’t participate in this process. &amp;lt;/p&amp;gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;The application &lt;strong&gt;asks for a request token&lt;/strong&gt;. It uses the REST API &lt;code&gt;POST /oauth/token/request&lt;/code&gt;. The consumer key and other details are included in the &lt;code&gt;Authorization&lt;/code&gt;header. &amp;lt;/p&amp;gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;The application** receives a request token and a request token secret** from Magento. &amp;lt;/p&amp;gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;The application &lt;strong&gt;asks for an access token&lt;/strong&gt; using the REST API &lt;code&gt;POST /oauth/token/access&lt;/code&gt;. The request token and other details are included in the &lt;code&gt;Authorization&lt;/code&gt;header. &amp;lt;/p&amp;gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Magento &lt;strong&gt;delivers an access token and an&lt;/strong&gt; **access token secret **if the request is successful. &amp;lt;/p&amp;gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;The application &lt;strong&gt;can operate the store resources&lt;/strong&gt;. All requests submitted to Magento must include the entire set of request parameters in the &lt;code&gt;Authorization&lt;/code&gt;header.&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 2. Activating Integration&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;How can you configure integration? Go to the Admin &lt;strong&gt;System&lt;/strong&gt; &amp;gt; &lt;strong&gt;Extensions **&amp;gt; **Integrations&lt;/strong&gt;. The process also involves a callback URL and an identity link URL.&lt;/p&gt;

&lt;p&gt;What is a &lt;strong&gt;callback URL&lt;/strong&gt;? This link specifies where OAuth credentials can be transmitted during OAuth token exchange. On the other hand, the identity link takes you to the login page of the external application, which will integrate with Magento.&lt;/p&gt;

&lt;p&gt;When merchants create an integration, they can select &lt;strong&gt;Save and Activate&lt;/strong&gt;. Or the merchant can use the **Activate **button to activate a previously saved integration from the Integration grid.&lt;/p&gt;

&lt;p&gt;Magento creates a consumer key and a consumer secret after initiating the integration. When you activate an integration, it sends the &lt;a href="https://dev.to/ebereplenty/react-authentication-protecting-and-accessing-routes-endpoints-96h"&gt;credentials to the endpoint&lt;/a&gt; you specified when you created it. &lt;/p&gt;

&lt;p&gt;The following attributes will be in an HTTP POST from Magento to the Integration endpoint:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;store_base_url&lt;/code&gt; (for example, &lt;a href="http://magento-store-example.com" rel="noopener noreferrer"&gt;http://magento-store-example.com&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_verifier&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_consumer_key&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_consumer_secret&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To receive a request token, integrations utilize the key: &lt;code&gt;oauth_consumer_key&lt;/code&gt;. And to get an access token, they use the &lt;code&gt;oauth_verifier&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 3. OAuth Handshake Details&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;To complete a two-legged OAuth handshake, you must obtain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a request token;&lt;/li&gt;
&lt;li&gt;an access token.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;a) Getting a Request Token&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A request token is a one-time usage token needed to exchange for an access token. This API allows you to obtain a request token from Magento:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /oauth/token/request
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These request parameters must be included in the &lt;code&gt;Authorization&lt;/code&gt;header of the call:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;oauth_consumer_key&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_signature_method&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_signature&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_nonce&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_timestamp&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_version&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fields in the response include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;oauth_token&lt;/code&gt;, the token to request an access token;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_token_secret&lt;/code&gt;, a secret value that identifies who owns the token.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example of a valid response may look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;oauth_token=6rq0x917xdzkhjlru0n4m2r6z2vvj66r&amp;amp;oauth_token_secret=4d85786q9yxisfjoh0d2xgvsard8j0zj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;b) Acquiring an Access Token&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Integrators obtain an access token in exchange for the request token, using the following API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /oauth/token/access
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The call &lt;code&gt;Authorization&lt;/code&gt; header contains the same request parameters as for the request token, plus:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;oauth_token&lt;/code&gt;, or the request token;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_verifier&lt;/code&gt;, a verification code transmitted as part of the initial POST transaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s an example of a valid response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;oauth_token=6rdpi1d4qypjpcdxcktef35kmmqxw6b1&amp;amp;oauth_token_secret=fcufgnt83chiljiftg2uj7nty6vvfzgo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It includes the following fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;oauth_token&lt;/code&gt;, which enables third-party applications to access protected resources;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_token_secret&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 4. Access Web APIs&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Third-party applications, or integrators, can use the access token to make Magento web APIs, such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET /rest/V1/addresses/3112
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The request parameters in the &lt;code&gt;Authorization&lt;/code&gt; request header in the call must be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;oauth_consumer_key&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_nonce&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_signature_method&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_signature&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_timestamp&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_token&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 5. The OAuth Signature&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;Authorization&lt;/code&gt;header includes the signature of every OAuth handshake and Web API requests. How do you generate the OAuth signature? The signature base string is created by connecting the following set of URL-encoded attributes and parameters with the ampersand (&amp;amp;) character:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTTP method;&lt;/li&gt;
&lt;li&gt;URL;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_nonce&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_signature_method&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_timestamp&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_version&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_consumer_key&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;oauth_token&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The signature generation requires the HMAC-SHA1 signature method. Even if the consumer secret and token secret are both empty, the signing key is the sequence of their values separated by the ampersand (&amp;amp;) character (ASCII code 38). Each value must be encoded using parameter encoding.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. Session Authentication&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Users may be required to confirm their identity every time they want to make a call. Sessions let them avoid this repetitive task. When a person logs in, their temporary session is created, which stores data. And then, the data for verification is taken from the session where authorization is required.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;JavaScript widget on the Magento storefront or Admin&lt;/strong&gt; is the preferred client for session-based authentication.&lt;/p&gt;

&lt;p&gt;How does this authentication work? A cookie identifies a session of a registered user, which expires after a period of inactivity. You can also use the system as a guest user without logging in.&lt;/p&gt;

&lt;p&gt;Depending on the type of user, you log in to the Magento store with customer or administrator credentials. The Magento web API framework recognizes you and controls what resources you’re trying to access.&lt;/p&gt;

&lt;p&gt;Suppose a customer logs in, and the JavaScript widget calls the &lt;code&gt;self&lt;/code&gt;API, the following method retrieves the details:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GET /rest/V1/customers/me&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note that API endpoints don’t support admin session-based authentication at this time. AJAX calls are the only way to use session-based authentication. Due to security flaws, direct browser requests are not possible. A developer can make a custom Magento widget to send requests without requiring any further authentication.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;To Sum Up&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This article covered three types of Magento authentication: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Token;&lt;/li&gt;
&lt;li&gt;OAuth;&lt;/li&gt;
&lt;li&gt;Session.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each one has a preferred type of user, so you need to know how to add them in steps. If you want to give access to the resources for customers, admins (integrations), or guest users, you configure permission in the &lt;code&gt;webapi.xml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Why do you need it all? It is needed for security so that no user can access your data or make changes in the online store without your permission.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>react</category>
    </item>
  </channel>
</rss>
