invalidate-tokens.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. [role="xpack"]
  2. [[security-api-invalidate-token]]
  3. === Invalidate token API
  4. ++++
  5. <titleabbrev>Invalidate token</titleabbrev>
  6. ++++
  7. Invalidates one or more access tokens or refresh tokens.
  8. [[security-api-invalidate-token-request]]
  9. ==== {api-request-title}
  10. `DELETE /_security/oauth2/token`
  11. [[security-api-invalidate-token-desc]]
  12. ==== {api-description-title}
  13. The access tokens returned by the <<security-api-get-token,get token API>> have a
  14. finite period of time for which they are valid and after that time period, they
  15. can no longer be used. That time period is defined by the
  16. `xpack.security.authc.token.timeout` setting. For more information, see
  17. <<token-service-settings>>.
  18. The refresh tokens returned by the <<security-api-get-token,get token API>> are
  19. only valid for 24 hours. They can also be used exactly once.
  20. If you want to invalidate one or more access or refresh tokens immediately, use
  21. this invalidate token API.
  22. [[security-api-invalidate-token-request-body]]
  23. ==== {api-request-body-title}
  24. The following parameters can be specified in the body of a DELETE request and
  25. pertain to invalidating tokens:
  26. `token`::
  27. (Optional, string) An access token. This parameter cannot be used any of
  28. `refresh_token`, `realm_name` or `username` are used.
  29. `refresh_token`::
  30. (Optional, string) A refresh token. This parameter cannot be used any of
  31. `refresh_token`, `realm_name` or `username` are used.
  32. `realm_name`::
  33. (Optional, string) The name of an authentication realm. This parameter cannot be
  34. used with either `refresh_token` or `token`.
  35. `username`::
  36. (Optional, string) The username of a user. This parameter cannot be used with
  37. either `refresh_token` or `token`
  38. NOTE: While all parameters are optional, at least one of them is required. More
  39. specifically, either one of `token` or `refresh_token` parameters is required.
  40. If none of these two are specified, then `realm_name` and/or `username` need to
  41. be specified.
  42. [[security-api-invalidate-token-response-body]]
  43. ==== {api-response-body-title}
  44. A successful call returns a JSON structure that contains the number of tokens
  45. that were invalidated, the number of tokens that had already been invalidated,
  46. and potentially a list of errors encountered while invalidating specific tokens.
  47. [[security-api-invalidate-token-example]]
  48. ==== {api-examples-title}
  49. For example, if you create a token using the `client_credentials` grant type as
  50. follows:
  51. [source,js]
  52. --------------------------------------------------
  53. POST /_security/oauth2/token
  54. {
  55. "grant_type" : "client_credentials"
  56. }
  57. --------------------------------------------------
  58. // CONSOLE
  59. // TEST
  60. The get token API returns the following information about the access token:
  61. [source,js]
  62. --------------------------------------------------
  63. {
  64. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  65. "type" : "Bearer",
  66. "expires_in" : 1200
  67. }
  68. --------------------------------------------------
  69. // CONSOLE
  70. // TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  71. This access token can now be immediately invalidated, as shown in the following
  72. example:
  73. [source,js]
  74. --------------------------------------------------
  75. DELETE /_security/oauth2/token
  76. {
  77. "token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ=="
  78. }
  79. --------------------------------------------------
  80. // CONSOLE
  81. // TEST[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  82. // TEST[continued]
  83. If you used the `password` grant type to obtain a token for a user, the response
  84. might also contain a refresh token. For example:
  85. [source,js]
  86. --------------------------------------------------
  87. POST /_security/oauth2/token
  88. {
  89. "grant_type" : "password",
  90. "username" : "test_admin",
  91. "password" : "x-pack-test-password"
  92. }
  93. --------------------------------------------------
  94. // CONSOLE
  95. // TEST
  96. The get token API returns the following information:
  97. [source,js]
  98. --------------------------------------------------
  99. {
  100. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  101. "type" : "Bearer",
  102. "expires_in" : 1200,
  103. "refresh_token": "vLBPvmAB6KvwvJZr27cS"
  104. }
  105. --------------------------------------------------
  106. // CONSOLE
  107. // TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  108. // TESTRESPONSE[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  109. The refresh token can now also be immediately invalidated as shown
  110. in the following example:
  111. [source,js]
  112. --------------------------------------------------
  113. DELETE /_security/oauth2/token
  114. {
  115. "refresh_token" : "vLBPvmAB6KvwvJZr27cS"
  116. }
  117. --------------------------------------------------
  118. // CONSOLE
  119. // TEST[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  120. // TEST[continued]
  121. The following example invalidates all access tokens and refresh tokens for the
  122. `saml1` realm immediately:
  123. [source,js]
  124. --------------------------------------------------
  125. DELETE /_security/oauth2/token
  126. {
  127. "realm_name" : "saml1"
  128. }
  129. --------------------------------------------------
  130. // CONSOLE
  131. // TEST
  132. The following example invalidates all access tokens and refresh tokens for the
  133. user `myuser` in all realms immediately:
  134. [source,js]
  135. --------------------------------------------------
  136. DELETE /_security/oauth2/token
  137. {
  138. "username" : "myuser"
  139. }
  140. --------------------------------------------------
  141. // CONSOLE
  142. // TEST
  143. Finally, the following example invalidates all access tokens and refresh tokens
  144. for the user `myuser` in the `saml1` realm immediately:
  145. [source,js]
  146. --------------------------------------------------
  147. DELETE /_security/oauth2/token
  148. {
  149. "username" : "myuser",
  150. "realm_name" : "saml1"
  151. }
  152. --------------------------------------------------
  153. // CONSOLE
  154. // TEST
  155. [source,js]
  156. --------------------------------------------------
  157. {
  158. "invalidated_tokens":9, <1>
  159. "previously_invalidated_tokens":15, <2>
  160. "error_count":2, <3>
  161. "error_details":[ <4>
  162. {
  163. "type":"exception",
  164. "reason":"Elasticsearch exception [type=exception, reason=foo]",
  165. "caused_by":{
  166. "type":"exception",
  167. "reason":"Elasticsearch exception [type=illegal_argument_exception, reason=bar]"
  168. }
  169. },
  170. {
  171. "type":"exception",
  172. "reason":"Elasticsearch exception [type=exception, reason=boo]",
  173. "caused_by":{
  174. "type":"exception",
  175. "reason":"Elasticsearch exception [type=illegal_argument_exception, reason=far]"
  176. }
  177. }
  178. ]
  179. }
  180. --------------------------------------------------
  181. // NOTCONSOLE
  182. <1> The number of the tokens that were invalidated as part of this request.
  183. <2> The number of tokens that were already invalidated.
  184. <3> The number of errors that were encountered when invalidating the tokens.
  185. <4> Details about these errors. This field is not present in the response when
  186. `error_count` is 0.