Get token APIedit

Creates a bearer token for access without requiring basic authentication.

Requestedit

POST /_xpack/security/oauth2/token

Descriptionedit

The tokens are created by the Elasticsearch Token Service, which is automatically enabled when you configure TLS on the HTTP interface. See Encrypting HTTP Client Communications. Alternatively, you can explicitly enable the xpack.security.authc.token.enabled setting. When you are running in production mode, a bootstrap check prevents you from enabling the token service unless you also enable TLS on the HTTP interface.

The get token API takes the same parameters as a typical OAuth 2.0 token API except for the use of a JSON request body.

A successful get token API call returns a JSON structure that contains the access token, the amount of time (seconds) that the token expires in, the type, and the scope if available.

The tokens returned by the get token API have a finite period of time for which they are valid and after that time period, they can no longer be used. That time period is defined by the xpack.security.authc.token.timeout setting. For more information, see Token service settingsedit.

If you want to invalidate a token immediately, you can do so by using the delete token API.

Request Bodyedit

The following parameters can be specified in the body of a POST request and pertain to creating a token:

grant_type
(string) The type of grant. Valid grant types are: password and refresh_token.
password
(string) The user’s password. If you specify the password grant type, this parameter is required.
refresh_token
(string) If you specify the refresh_token grant type, this parameter is required. It contains the string that was returned when you created the token and enables you to extend its life.
scope
(string) The scope of the token. Currently tokens are only issued for a scope of FULL regardless of the value sent with the request.
username
(string) The username that identifies the user. If you specify the password grant type, this parameter is required.

Examplesedit

The following example obtains a token for the test_admin user:

POST /_xpack/security/oauth2/token
{
  "grant_type" : "password",
  "username" : "test_admin",
  "password" : "x-pack-test-password"
}

The following example output contains the access token, the amount of time (in seconds) that the token expires in, and the type:

{
  "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  "type" : "Bearer",
  "expires_in" : 1200,
  "refresh_token": "vLBPvmAB6KvwvJZr27cS"
}

The token returned by this API can be used by sending a request with a Authorization header with a value having the prefix Bearer ` followed by the value of the `access_token.

curl -H "Authorization: Bearer dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==" http://localhost:9200/_cluster/health

To extend the life of an existing token, you can call the API again with the refresh token within 24 hours of the token’s creation. For example:

POST /_xpack/security/oauth2/token
{
    "grant_type": "refresh_token",
    "refresh_token": "vLBPvmAB6KvwvJZr27cS"
}

The API will return a new token and refresh token. Each refresh token may only be used one time.

{
  "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  "type" : "Bearer",
  "expires_in" : 1200,
  "refresh_token": "vLBPvmAB6KvwvJZr27cS"
}