|
| 1 | +--- |
| 2 | +group: web-api |
| 3 | +title: Token-based authentication |
| 4 | +functional_areas: |
| 5 | + - Integration |
| 6 | +--- |
| 7 | + |
| 8 | +To make a web [API](https://glossary.magento.com/api) call from a client such as a mobile application, you must supply an *access token* on the call. The token acts like an electronic key that lets you access the API. |
| 9 | + |
| 10 | +Magento issues the following types of access tokens: |
| 11 | + |
| 12 | +Token type | Description | Default lifetime |
| 13 | +--- | --- | --- |
| 14 | +Integration | The merchant determines which Magento resources the integration has access to. | Indefinite. It lasts until it is manually revoked. |
| 15 | +Admin | The merchant determines which Magento resources an admin user has access to. | 4 hours |
| 16 | +Customer | Magento grants access to resources with the `anonymous` or `self` permission. Merchants cannot edit these settings. | 1 hour |
| 17 | + |
| 18 | +## Integration tokens |
| 19 | + |
| 20 | +When a merchant creates and activates an integration, Magento generates a consumer key, consumer secret, access token, and access token secret. All of these entities are used for [OAuth-based authentication]({{ page.baseurl }}/get-started/authentication/gs-authentication-oauth.html), but token-based authentication requires only the access token. |
| 21 | + |
| 22 | +Use the following steps to generate an access token: |
| 23 | + |
| 24 | +1. Log in to Admin and click **System** > **Extensions** > **Integrations** to display the Integrations page. |
| 25 | +1. Click **Add New Integration** to display the New Integration page. |
| 26 | +1. Enter a unique name for the integration in the **Name** field. Then enter your admin password in the **Your Password** field. Leave all other fields blank. |
| 27 | +1. Click the API tab. Select the Magento resources the integration can access. You can select all resources, or select a custom list. |
| 28 | +1. Click **Save** to save your changes and return to the Integrations page. |
| 29 | +1. Click the **Activate** link in the grid that corresponds to the newly-created integration. |
| 30 | +1. Click **Allow** . A dialog similar to the following displays: |
| 31 | + |
| 32 | +  |
| 33 | + |
| 34 | +The access token can be used in all calls made on behalf of the integration. |
| 35 | + |
| 36 | +## Admin and customer access tokens |
| 37 | + |
| 38 | +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 access token in exchange for the username and password for a Magento account. |
| 39 | + |
| 40 | +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. |
| 41 | + |
| 42 | +The following table lists endpoints and services that can be used to get an authentication token. Some 2FA providers may require multiple calls. |
| 43 | + |
| 44 | +Token type |REST| SOAP |
| 45 | +---|---|--- |
| 46 | +Admin with Google Authenticator | `POST /V1/tfa/provider/google/authenticate` | `twoFactorAuthGoogleAuthenticateV1` |
| 47 | +Admin with Duo Security | `POST /V1/tfa/provider/duo-security/authenticate` | `twoFactorAuthDuoAuthenticateV1` |
| 48 | +Admin with Authy | `POST /V1/tfa/provider/authy/authenticate` | `twoFactorAuthAuthyAuthenticateV1` |
| 49 | +Admin with U2F | `POST /V1/tfa/provider/u2fkey/verify` | `twoFactorAuthU2fKeyAuthenticateV1` |
| 50 | +Customer | `POST /V1/integration/customer/token` | `integrationCustomerTokenServiceV1` |
| 51 | + |
| 52 | +For most [web API](https://glossary.magento.com/web-api) calls, you supply this token in the `Authorization` request header with the `Bearer` HTTP [authorization](https://glossary.magento.com/authorization) 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** > **Settings** > **Configuration** > **Services** > **OAuth** > **Access Token Expiration**. |
| 53 | + |
| 54 | +A cron job that runs hourly removes all expired tokens. |
| 55 | + |
| 56 | +## Request a token {#request-token} |
| 57 | + |
| 58 | +A access token request contains three basic elements: |
| 59 | + |
| 60 | +Component | Specifies |
| 61 | +--- | --- |
| 62 | +Endpoint | A combination of the _server_ that fulfills the request, the web service, and the `resource` against which the request is being made.<br/><br/>For example, in the `POST <host>/rest/<store_code>/V1/integration/customer/token` endpoint:<br/>The server is `magento.host/index.php/`,<br/> the web service is `rest`.<br/> and the resource is `/V1/integration/customer/token`. |
| 63 | +Content type | The content type of the request body. Set this value to either `"Content-Type:application/json"` or `"Content-Type:application/xml"`. |
| 64 | +Credentials | The username and password for a Magento account.<br/><br/>To specify these credentials in a JSON request body, include code similar to the following in the call: <br/><br/>`{"username":"<USER-NAME>;", "password":"<PASSWORD>"}`<br/><br/>To specify these credentials in XML, include code similar to the following in the call:<br/><br/>`<login><username>customer1</username><password>customer1pw</password></login>` |
| 65 | + |
| 66 | +### Examples {#token-example} |
| 67 | + |
| 68 | +The following image shows a token request for the [admin](https://glossary.magento.com/admin) account using a REST client: |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +The following example uses the `curl` command to request a token for a customer account: |
| 73 | + |
| 74 | +```bash |
| 75 | +curl -X POST "https://magento.host/index.php/rest/V1/integration/customer/token" \ |
| 76 | + -H "Content-Type:application/json" \ |
| 77 | + -d '{"username":"[email protected]", "password":"customer_password"}' |
| 78 | +``` |
| 79 | + |
| 80 | +The following example makes the same request with [XML](https://glossary.magento.com/xml) for a customer account token: |
| 81 | + |
| 82 | +```bash |
| 83 | +curl -X POST "http://magento.vg/index.php/rest/V1/integration/customer/token" \ |
| 84 | + -H "Content-Type:application/xml" \ |
| 85 | + -d "<login><username>customer1</username><password>customer1pw</password></login>" |
| 86 | +``` |
| 87 | + |
| 88 | +For more information about the `curl` command, see [Use cURL to run the request]({{ page.baseurl }}/get-started/gs-curl.html) |
| 89 | + |
| 90 | +## Authentication token response {#auth-response} |
| 91 | + |
| 92 | +A successful request returns a response body with the token, as follows: |
| 93 | + |
| 94 | +`asdf3hjklp5iuytre` |
| 95 | + |
| 96 | +## Use the token in a Web API request {#web-api-access} |
| 97 | + |
| 98 | +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: |
| 99 | + |
| 100 | +`Authorization: Bearer <authentication token>` |
| 101 | + |
| 102 | +### Admin access {#admin-access} |
| 103 | + |
| 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 | + |
| 112 | +Customers can access only resources with `self` permissions. |
| 113 | + |
| 114 | +For example, to make a web API call with a customer token: |
| 115 | +`curl -X GET "http://magento.ll/index.php/rest/V1/customers/me" -H "Authorization: Bearer asdf3hjklp5iuytre"` |
| 116 | + |
| 117 | +{:.ref-header} |
| 118 | +Related topics |
| 119 | + |
| 120 | +[Construct a request]({{ page.baseurl }}/get-started/gs-web-api-request.html) |
| 121 | + |
| 122 | +[Configure services as web APIs]({{ page.baseurl }}/extension-dev-guide/service-contracts/service-to-web-service.html) |
| 123 | + |
| 124 | +[Restricting access to anonymous web APIs]({{ page.baseurl }}/rest/anonymous-api-security.html) |
0 commit comments