2018-08-18 01:22:09 -04:00
|
|
|
[role="xpack"]
|
|
|
|
[[security-api-invalidate-token]]
|
2018-11-21 02:32:56 -05:00
|
|
|
=== Invalidate token API
|
2018-12-20 13:23:28 -05:00
|
|
|
++++
|
|
|
|
<titleabbrev>Invalidate token</titleabbrev>
|
|
|
|
++++
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2018-12-18 03:05:50 -05:00
|
|
|
Invalidates one or more access tokens or refresh tokens.
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
[[security-api-invalidate-token-request]]
|
|
|
|
==== {api-request-title}
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2018-12-11 04:13:10 -05:00
|
|
|
`DELETE /_security/oauth2/token`
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
[[security-api-invalidate-token-desc]]
|
|
|
|
==== {api-description-title}
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2018-11-21 02:32:56 -05:00
|
|
|
The access tokens returned by the <<security-api-get-token,get token API>> have a
|
2018-08-18 01:22:09 -04:00
|
|
|
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-settings>>.
|
|
|
|
|
2018-11-21 02:32:56 -05:00
|
|
|
The refresh tokens returned by the <<security-api-get-token,get token API>> are
|
|
|
|
only valid for 24 hours. They can also be used exactly once.
|
|
|
|
|
2019-03-12 16:25:16 -04:00
|
|
|
If you want to invalidate one or more access or refresh tokens immediately, use
|
|
|
|
this invalidate token API.
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
[[security-api-invalidate-token-request-body]]
|
|
|
|
==== {api-request-body-title}
|
2018-08-18 01:22:09 -04:00
|
|
|
|
|
|
|
The following parameters can be specified in the body of a DELETE request and
|
2018-12-18 03:05:50 -05:00
|
|
|
pertain to invalidating tokens:
|
2018-11-21 02:32:56 -05:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
`token`::
|
|
|
|
(Optional, string) An access token. This parameter cannot be used any of
|
|
|
|
`refresh_token`, `realm_name` or `username` are used.
|
2018-11-21 02:32:56 -05:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
`refresh_token`::
|
|
|
|
(Optional, string) A refresh token. This parameter cannot be used any of
|
|
|
|
`refresh_token`, `realm_name` or `username` are used.
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
`realm_name`::
|
|
|
|
(Optional, string) The name of an authentication realm. This parameter cannot be
|
|
|
|
used with either `refresh_token` or `token`.
|
2018-12-18 03:05:50 -05:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
`username`::
|
|
|
|
(Optional, string) The username of a user. This parameter cannot be used with
|
|
|
|
either `refresh_token` or `token`
|
2018-12-18 03:05:50 -05:00
|
|
|
|
2019-03-12 16:25:16 -04:00
|
|
|
NOTE: While all parameters are optional, at least one of them is required. More
|
|
|
|
specifically, either one of `token` or `refresh_token` parameters is required.
|
|
|
|
If none of these two are specified, then `realm_name` and/or `username` need to
|
|
|
|
be specified.
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2019-08-02 13:56:05 -04:00
|
|
|
|
|
|
|
[[security-api-invalidate-token-response-body]]
|
|
|
|
==== {api-response-body-title}
|
|
|
|
|
|
|
|
A successful call returns a JSON structure that contains the number of tokens
|
|
|
|
that were invalidated, the number of tokens that had already been invalidated,
|
|
|
|
and potentially a list of errors encountered while invalidating specific tokens.
|
|
|
|
|
|
|
|
[[security-api-invalidate-token-example]]
|
|
|
|
==== {api-examples-title}
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2019-03-12 16:25:16 -04:00
|
|
|
For example, if you create a token using the `client_credentials` grant type as
|
|
|
|
follows:
|
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console]
|
2019-03-12 16:25:16 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
POST /_security/oauth2/token
|
|
|
|
{
|
|
|
|
"grant_type" : "client_credentials"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
The get token API returns the following information about the access token:
|
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console-result]
|
2019-03-12 16:25:16 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
|
|
|
|
"type" : "Bearer",
|
|
|
|
"expires_in" : 1200
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
|
|
|
|
|
|
|
|
This access token can now be immediately invalidated, as shown in the following
|
|
|
|
example:
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console]
|
2018-08-18 01:22:09 -04:00
|
|
|
--------------------------------------------------
|
2018-12-11 04:13:10 -05:00
|
|
|
DELETE /_security/oauth2/token
|
2018-08-18 01:22:09 -04:00
|
|
|
{
|
|
|
|
"token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ=="
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2019-03-12 16:25:16 -04:00
|
|
|
// TEST[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
|
|
|
|
// TEST[continued]
|
2018-08-18 01:22:09 -04:00
|
|
|
|
2019-03-12 16:25:16 -04:00
|
|
|
If you used the `password` grant type to obtain a token for a user, the response
|
|
|
|
might also contain a refresh token. For example:
|
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console]
|
2019-03-12 16:25:16 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
POST /_security/oauth2/token
|
|
|
|
{
|
|
|
|
"grant_type" : "password",
|
|
|
|
"username" : "test_admin",
|
|
|
|
"password" : "x-pack-test-password"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
The get token API returns the following information:
|
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console-result]
|
2019-03-12 16:25:16 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
|
|
|
|
"type" : "Bearer",
|
|
|
|
"expires_in" : 1200,
|
|
|
|
"refresh_token": "vLBPvmAB6KvwvJZr27cS"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
|
|
|
|
// TESTRESPONSE[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
|
|
|
|
|
|
|
|
The refresh token can now also be immediately invalidated as shown
|
|
|
|
in the following example:
|
2018-11-21 02:32:56 -05:00
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console]
|
2018-11-21 02:32:56 -05:00
|
|
|
--------------------------------------------------
|
2018-12-11 04:13:10 -05:00
|
|
|
DELETE /_security/oauth2/token
|
2018-11-21 02:32:56 -05:00
|
|
|
{
|
2019-03-12 16:25:16 -04:00
|
|
|
"refresh_token" : "vLBPvmAB6KvwvJZr27cS"
|
2018-11-21 02:32:56 -05:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2019-03-12 16:25:16 -04:00
|
|
|
// TEST[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
|
|
|
|
// TEST[continued]
|
2018-11-21 02:32:56 -05:00
|
|
|
|
2019-03-12 16:25:16 -04:00
|
|
|
The following example invalidates all access tokens and refresh tokens for the
|
|
|
|
`saml1` realm immediately:
|
2018-12-18 03:05:50 -05:00
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console]
|
2018-12-18 03:05:50 -05:00
|
|
|
--------------------------------------------------
|
2019-03-12 16:25:16 -04:00
|
|
|
DELETE /_security/oauth2/token
|
2018-12-18 03:05:50 -05:00
|
|
|
{
|
|
|
|
"realm_name" : "saml1"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2019-03-12 16:25:16 -04:00
|
|
|
The following example invalidates all access tokens and refresh tokens for the
|
|
|
|
user `myuser` in all realms immediately:
|
2018-12-18 03:05:50 -05:00
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console]
|
2018-12-18 03:05:50 -05:00
|
|
|
--------------------------------------------------
|
2019-03-12 16:25:16 -04:00
|
|
|
DELETE /_security/oauth2/token
|
2018-12-18 03:05:50 -05:00
|
|
|
{
|
|
|
|
"username" : "myuser"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2019-03-12 16:25:16 -04:00
|
|
|
Finally, the following example invalidates all access tokens and refresh tokens
|
|
|
|
for the user `myuser` in the `saml1` realm immediately:
|
2018-12-18 03:05:50 -05:00
|
|
|
|
2019-09-05 14:12:39 -04:00
|
|
|
[source,console]
|
2018-12-18 03:05:50 -05:00
|
|
|
--------------------------------------------------
|
2019-03-12 16:25:16 -04:00
|
|
|
DELETE /_security/oauth2/token
|
2018-12-18 03:05:50 -05:00
|
|
|
{
|
|
|
|
"username" : "myuser",
|
|
|
|
"realm_name" : "saml1"
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2018-08-18 01:22:09 -04:00
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2018-12-18 03:05:50 -05:00
|
|
|
"invalidated_tokens":9, <1>
|
|
|
|
"previously_invalidated_tokens":15, <2>
|
|
|
|
"error_count":2, <3>
|
|
|
|
"error_details":[ <4>
|
|
|
|
{
|
|
|
|
"type":"exception",
|
|
|
|
"reason":"Elasticsearch exception [type=exception, reason=foo]",
|
|
|
|
"caused_by":{
|
|
|
|
"type":"exception",
|
|
|
|
"reason":"Elasticsearch exception [type=illegal_argument_exception, reason=bar]"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type":"exception",
|
|
|
|
"reason":"Elasticsearch exception [type=exception, reason=boo]",
|
|
|
|
"caused_by":{
|
|
|
|
"type":"exception",
|
|
|
|
"reason":"Elasticsearch exception [type=illegal_argument_exception, reason=far]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2018-08-18 01:22:09 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// NOTCONSOLE
|
|
|
|
|
2018-12-18 03:05:50 -05:00
|
|
|
<1> The number of the tokens that were invalidated as part of this request.
|
|
|
|
<2> The number of tokens that were already invalidated.
|
|
|
|
<3> The number of errors that were encountered when invalidating the tokens.
|
|
|
|
<4> Details about these errors. This field is not present in the response when
|
|
|
|
`error_count` is 0.
|