2017-06-28 14:02:40 -04:00
|
|
|
[role="xpack"]
|
2018-08-18 01:22:09 -04:00
|
|
|
[[security-api-get-token]]
|
|
|
|
=== Get token API
|
2017-04-27 11:04:31 -04:00
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
Creates a bearer token for access without requiring basic authentication.
|
2017-04-27 11:04:31 -04:00
|
|
|
|
2017-09-22 12:56:32 -04:00
|
|
|
==== Request
|
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
`POST /_xpack/security/oauth2/token`
|
2017-09-22 12:56:32 -04:00
|
|
|
|
|
|
|
==== Description
|
|
|
|
|
2018-04-09 14:39:21 -04:00
|
|
|
The tokens are created by the {es} Token Service, which is automatically enabled
|
|
|
|
when you configure TLS on the HTTP interface. See <<tls-http>>. 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.
|
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
The get token API takes the same parameters as a typical OAuth 2.0 token API
|
2017-09-22 12:56:32 -04:00
|
|
|
except for the use of a JSON request body.
|
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
A successful get token API call returns a JSON structure that contains the access
|
2017-09-22 12:56:32 -04:00
|
|
|
token, the amount of time (seconds) that the token expires in, the type, and the
|
|
|
|
scope if available.
|
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
The tokens returned by the get token API have a finite period of time for which
|
2018-04-09 14:39:21 -04:00
|
|
|
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-settings>>.
|
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
If you want to invalidate a token immediately, you can do so by using the
|
2018-11-21 02:32:56 -05:00
|
|
|
<<security-api-invalidate-token,invalidate token API>>.
|
2017-09-22 12:56:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
==== Request Body
|
|
|
|
|
|
|
|
The following parameters can be specified in the body of a POST request and
|
|
|
|
pertain to creating a token:
|
|
|
|
|
|
|
|
`grant_type`::
|
2018-08-27 12:56:21 -04:00
|
|
|
(string) The type of grant. Supported grant types are: `password`,
|
|
|
|
`client_credentials` and `refresh_token`.
|
2017-09-22 12:56:32 -04:00
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
`password`::
|
|
|
|
(string) The user's password. If you specify the `password` grant type, this
|
2018-08-27 12:56:21 -04:00
|
|
|
parameter is required. This parameter is not valid with any other supported
|
|
|
|
grant type.
|
2018-08-18 01:22:09 -04:00
|
|
|
|
|
|
|
`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
|
2018-08-27 12:56:21 -04:00
|
|
|
and enables you to extend its life. This parameter is not valid with any other
|
|
|
|
supported grant type.
|
2017-09-22 12:56:32 -04:00
|
|
|
|
|
|
|
`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.
|
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
`username`::
|
|
|
|
(string) The username that identifies the user. If you specify the `password`
|
2018-08-27 12:56:21 -04:00
|
|
|
grant type, this parameter is required. This parameter is not valid with any
|
|
|
|
other supported grant type.
|
2017-09-22 12:56:32 -04:00
|
|
|
|
|
|
|
==== Examples
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2018-08-27 12:56:21 -04:00
|
|
|
The following example obtains a token using the `client_credentials` grant type,
|
|
|
|
which simply creates a token as the authenticated user:
|
2017-04-27 11:04:31 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
POST /_xpack/security/oauth2/token
|
|
|
|
{
|
2018-08-27 12:56:21 -04:00
|
|
|
"grant_type" : "client_credentials"
|
2017-04-27 11:04:31 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
|
2017-09-22 12:56:32 -04:00
|
|
|
The following example output contains the access token, the amount of time (in
|
|
|
|
seconds) that the token expires in, and the type:
|
2017-04-27 11:04:31 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
|
|
|
|
"type" : "Bearer",
|
2018-08-27 12:56:21 -04:00
|
|
|
"expires_in" : 1200
|
2017-04-27 11:04:31 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
|
|
|
|
|
|
|
|
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`.
|
|
|
|
|
|
|
|
[source,shell]
|
|
|
|
--------------------------------------------------
|
|
|
|
curl -H "Authorization: Bearer dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==" http://localhost:9200/_cluster/health
|
|
|
|
--------------------------------------------------
|
2018-06-18 11:48:23 -04:00
|
|
|
// NOTCONSOLE
|
2017-04-27 11:04:31 -04:00
|
|
|
|
2018-08-27 12:56:21 -04:00
|
|
|
The following example obtains a token for the `test_admin` user using the
|
|
|
|
`password` grant type:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
POST /_xpack/security/oauth2/token
|
|
|
|
{
|
|
|
|
"grant_type" : "password",
|
|
|
|
"username" : "test_admin",
|
|
|
|
"password" : "x-pack-test-password"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
|
|
|
|
The following example output contains the access token, the amount of time (in
|
|
|
|
seconds) that the token expires in, the type, and the refresh token:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
|
|
|
|
"type" : "Bearer",
|
|
|
|
"expires_in" : 1200,
|
|
|
|
"refresh_token": "vLBPvmAB6KvwvJZr27cS"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
|
|
|
|
// TESTRESPONSE[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
|
|
|
|
|
2018-01-17 14:18:44 -05:00
|
|
|
[[security-api-refresh-token]]
|
2018-08-27 12:56:21 -04:00
|
|
|
To extend the life of an existing token obtained using the `password` grant type,
|
|
|
|
you can call the API again with the refresh token within 24 hours of the token's
|
|
|
|
creation. For example:
|
2018-01-17 14:18:44 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
POST /_xpack/security/oauth2/token
|
|
|
|
{
|
|
|
|
"grant_type": "refresh_token",
|
|
|
|
"refresh_token": "vLBPvmAB6KvwvJZr27cS"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
// TEST[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
|
|
|
|
// TEST[continued]
|
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
The API will return a new token and refresh token. Each refresh token may only
|
|
|
|
be used one time.
|
2018-01-17 14:18:44 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
|
|
|
|
"type" : "Bearer",
|
|
|
|
"expires_in" : 1200,
|
|
|
|
"refresh_token": "vLBPvmAB6KvwvJZr27cS"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
|
2018-08-18 01:22:09 -04:00
|
|
|
// TESTRESPONSE[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
|