invalidate-tokens.asciidoc 7.8 KB

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