|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +group: get-started |
| 4 | +subgroup: 40_Authentication |
| 5 | +title: Token-based authentication |
| 6 | +menu_title: Token-based authentication |
| 7 | +menu_order: 1 |
| 8 | +version: 2.0 |
| 9 | +github_link: get-started/authentication/gs-authentication-token.md |
| 10 | +redirect_from: /guides/v1.0/get-started/authentication/gs-authentication-token.html |
| 11 | +--- |
| 12 | + |
| 13 | +## Authentication tokens |
| 14 | + |
| 15 | +To make a web {% glossarytooltip 786086f2-622b-4007-97fe-2c19e5283035 %}API{% endglossarytooltip %} call from a client such as a mobile application, you must supply an *authentication token* on the call. The token acts like an electronic key that lets you access the API. |
| 16 | + |
| 17 | +Magento provides a separate token service for administrators and customers. When you request a token from one of these services, the service returns a unique authentication token in exchange for the user name and password for a Magento account. |
| 18 | + |
| 19 | +The Magento web API framework allows *guest users* to access resources that are configured with the permission level of anonymous. Guest users are users who the framework cannot authenticate through existing authentication mechanisms. As a guest user, you do not need to, but you can, specify a token in a web API call for a resource with anonymous permission. [Restricting access to anonymous web APIs]({{page.baseurl}}rest/anonymous-api-security.html) contains a list of APIs that do not require a token. |
| 20 | + |
| 21 | +Use the following calls to get an authentication token: |
| 22 | + |
| 23 | +Request|REST|SOAP |
| 24 | +---|---|--- |
| 25 | +Get an admin token | `POST /V1/integration/admin/token` | `integrationAdminTokenServiceV1` |
| 26 | +Get a customer token | `POST /V1/integration/customer/token` | `integrationCustomerTokenServiceV1` |
| 27 | + |
| 28 | +For most {% glossarytooltip 377dc0a3-b8a7-4dfa-808e-2de37e4c0029 %}web API{% endglossarytooltip %} calls, you supply this token in the `Authorization` request header with the `Bearer` HTTP {% glossarytooltip 34ecb0ab-b8a3-42d9-a728-0b893e8c0417 %}authorization{% endglossarytooltip %} scheme to prove your identity. By default, an admin token is valid for 4 hours, while a customer token is valid for 1 hour. You can change these values from Admin by selecting **Stores > Configuration > Services > OAuth > Access Token Expiration**. |
| 29 | + |
| 30 | +A cron job that runs hourly removes all expired tokens. |
| 31 | + |
| 32 | +## Request a token {#request-token} |
| 33 | + |
| 34 | +A authentication token request contains three basic elements: |
| 35 | + |
| 36 | +<table style="width:100%"> |
| 37 | + <tr bgcolor="lightgray"> |
| 38 | + <th>Component</th> |
| 39 | + <th>Specifies</th> |
| 40 | + </tr> |
| 41 | + <tr> |
| 42 | + <td>Endpoint</td> |
| 43 | + <td> |
| 44 | + <p>A combination of the <i>server</i> that fulfills the request, the web service, and the <i>resource</i> against which the request is being made.</p> |
| 45 | + <p>For example, in the <code>POST https://magento.host/index.php/rest/V1/integration/customer/token</code> endpoint:</p> |
| 46 | + <p>The server is <code>magento.host/index.php/</code></p> |
| 47 | + <p>the web service is <code>rest</code></p> |
| 48 | + the resource is <code>/V1/integration/customer/token</code>.</p> |
| 49 | + </td> |
| 50 | + </tr> |
| 51 | + <tr> |
| 52 | + <td>Content type</td> |
| 53 | + <td> |
| 54 | + <p>The content type of the request body. Set this value to either <code>"Content-Type:application/json"</code> or <code>"Content-Type:application/xml"</code>.</p> |
| 55 | + </td> |
| 56 | + </tr> |
| 57 | + <tr> |
| 58 | + <td>Credentials</td> |
| 59 | + <td> |
| 60 | + <p>The user name and password for a Magento account.</p> |
| 61 | + <p>To specify these credentials in a JSON request body, include <code>'{"username":"<USER-NAME>", "password":"<PASSWORD>"}'</code> in the call.</p> |
| 62 | + <p> To specify these credentials in XML, include <code><login><username>[email protected]</username><password>customer1pw</password></login></code> in the call.</p> |
| 63 | + </td> |
| 64 | + </tr> |
| 65 | +</table> |
| 66 | + |
| 67 | +#### Examples {#token-example} |
| 68 | + |
| 69 | +The following image shows a token request for the {% glossarytooltip 29ddb393-ca22-4df9-a8d4-0024d75739b1 %}admin{% endglossarytooltip %} account using a REST client: |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +The following example uses the `curl` command to request a token for a customer account: |
| 74 | + |
| 75 | +``` |
| 76 | +curl -X POST "https://magento.host/index.php/rest/V1/integration/customer/token" \ |
| 77 | + -H "Content-Type:application/json" \ |
| 78 | + -d "{"username":"[email protected]", "password":"customer1pw"}" |
| 79 | +``` |
| 80 | + |
| 81 | +The following example makes the same request with {% glossarytooltip 8c0645c5-aa6b-4a52-8266-5659a8b9d079 %}XML{% endglossarytooltip %} for a customer account token: |
| 82 | + |
| 83 | +``` |
| 84 | +curl -X POST "http://magento.vg/index.php/rest/V1/integration/customer/token" \ |
| 85 | + -H "Content-Type:application/xml" \ |
| 86 | + -d "<login><username>[email protected]</username><password>customer1pw</password></login>" |
| 87 | +``` |
| 88 | + |
| 89 | +For more information about the `curl` command, see [Use cURL to run the request]({{page.baseurl}}get-started/gs-curl.html) |
| 90 | + |
| 91 | +## Authentication token response {#auth-response} |
| 92 | + |
| 93 | +A successful request returns a response body with the token, as follows: |
| 94 | + |
| 95 | +`asdf3hjklp5iuytre` |
| 96 | + |
| 97 | +## Use the token in a Web API request {#web-api-access} |
| 98 | + |
| 99 | +Any web API call that accesses a resource that requires a permission level higher than anonymous must contain the authentication token in the header To do this, specify a HTTP header in the following format: |
| 100 | + |
| 101 | +`Authorization: Bearer <authentication token>` |
| 102 | + |
| 103 | +### Admin access {#admin-access} |
| 104 | +Admins can access any resources for which they are authorized. |
| 105 | + |
| 106 | +For example, to make a web API call with an admin token: |
| 107 | + |
| 108 | +`curl -X GET "http://magento.ll/index.php/rest/V1/customers/2" -H "Authorization: Bearer vbnf3hjklp5iuytre"` |
| 109 | + |
| 110 | +### Customer access |
| 111 | +Customers can access only resources with `self` permissions. |
| 112 | + |
| 113 | +For example, to make a web API call with a customer token: |
| 114 | +`curl -X GET "http://magento.ll/index.php/rest/V1/customers/me" -H "Authorization: Bearer asdf3hjklp5iuytre"` |
| 115 | + |
| 116 | +<h2>Related topics</h2> |
| 117 | +[Construct a request]({{page.baseurl}}/get-started/gs-web-api-request.html) |
| 118 | + |
| 119 | +[Configure services as web APIs]({{page.baseurl}}extension-dev-guide/service-contracts/service-to-web-service.html) |
| 120 | + |
| 121 | +[Restricting access to anonymous web APIs]({{page.baseurl}}rest/anonymous-api-security.html) |
0 commit comments