invalidate-tokens.asciidoc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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,console]
  52. --------------------------------------------------
  53. POST /_security/oauth2/token
  54. {
  55. "grant_type" : "client_credentials"
  56. }
  57. --------------------------------------------------
  58. The get token API returns the following information about the access token:
  59. [source,console-result]
  60. --------------------------------------------------
  61. {
  62. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  63. "type" : "Bearer",
  64. "expires_in" : 1200,
  65. "authentication" : {
  66. "username" : "test_admin",
  67. "roles" : [
  68. "superuser"
  69. ],
  70. "full_name" : null,
  71. "email" : null,
  72. "metadata" : { },
  73. "enabled" : true,
  74. "authentication_realm" : {
  75. "name" : "file",
  76. "type" : "file"
  77. },
  78. "lookup_realm" : {
  79. "name" : "file",
  80. "type" : "file"
  81. },
  82. "authentication_type" : "realm"
  83. }
  84. }
  85. --------------------------------------------------
  86. // TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  87. // TESTRESPONSE[s/superuser/_es_test_root/]
  88. This access token can now be immediately invalidated, as shown in the following
  89. example:
  90. [source,console]
  91. --------------------------------------------------
  92. DELETE /_security/oauth2/token
  93. {
  94. "token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ=="
  95. }
  96. --------------------------------------------------
  97. // TEST[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  98. // TEST[continued]
  99. If you used the `password` grant type to obtain a token for a user, the response
  100. might also contain a refresh token. For example:
  101. [source,console]
  102. --------------------------------------------------
  103. POST /_security/oauth2/token
  104. {
  105. "grant_type" : "password",
  106. "username" : "test_admin",
  107. "password" : "x-pack-test-password"
  108. }
  109. --------------------------------------------------
  110. The get token API returns the following information:
  111. [source,console-result]
  112. --------------------------------------------------
  113. {
  114. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  115. "type" : "Bearer",
  116. "expires_in" : 1200,
  117. "refresh_token": "vLBPvmAB6KvwvJZr27cS",
  118. "authentication" : {
  119. "username" : "test_admin",
  120. "roles" : [
  121. "superuser"
  122. ],
  123. "full_name" : null,
  124. "email" : null,
  125. "metadata" : { },
  126. "enabled" : true,
  127. "authentication_realm" : {
  128. "name" : "file",
  129. "type" : "file"
  130. },
  131. "lookup_realm" : {
  132. "name" : "file",
  133. "type" : "file"
  134. },
  135. "authentication_type" : "realm"
  136. }
  137. }
  138. --------------------------------------------------
  139. // TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  140. // TESTRESPONSE[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  141. // TESTRESPONSE[s/superuser/_es_test_root/]
  142. The refresh token can now also be immediately invalidated as shown
  143. in the following example:
  144. [source,console]
  145. --------------------------------------------------
  146. DELETE /_security/oauth2/token
  147. {
  148. "refresh_token" : "vLBPvmAB6KvwvJZr27cS"
  149. }
  150. --------------------------------------------------
  151. // TEST[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  152. // TEST[continued]
  153. The following example invalidates all access tokens and refresh tokens for the
  154. `saml1` realm immediately:
  155. [source,console]
  156. --------------------------------------------------
  157. DELETE /_security/oauth2/token
  158. {
  159. "realm_name" : "saml1"
  160. }
  161. --------------------------------------------------
  162. The following example invalidates all access tokens and refresh tokens for the
  163. user `myuser` in all realms immediately:
  164. [source,console]
  165. --------------------------------------------------
  166. DELETE /_security/oauth2/token
  167. {
  168. "username" : "myuser"
  169. }
  170. --------------------------------------------------
  171. Finally, the following example invalidates all access tokens and refresh tokens
  172. for the user `myuser` in the `saml1` realm immediately:
  173. [source,console]
  174. --------------------------------------------------
  175. DELETE /_security/oauth2/token
  176. {
  177. "username" : "myuser",
  178. "realm_name" : "saml1"
  179. }
  180. --------------------------------------------------
  181. [source,js]
  182. --------------------------------------------------
  183. {
  184. "invalidated_tokens":9, <1>
  185. "previously_invalidated_tokens":15, <2>
  186. "error_count":2, <3>
  187. "error_details":[ <4>
  188. {
  189. "type":"exception",
  190. "reason":"Elasticsearch exception [type=exception, reason=foo]",
  191. "caused_by":{
  192. "type":"exception",
  193. "reason":"Elasticsearch exception [type=illegal_argument_exception, reason=bar]"
  194. }
  195. },
  196. {
  197. "type":"exception",
  198. "reason":"Elasticsearch exception [type=exception, reason=boo]",
  199. "caused_by":{
  200. "type":"exception",
  201. "reason":"Elasticsearch exception [type=illegal_argument_exception, reason=far]"
  202. }
  203. }
  204. ]
  205. }
  206. --------------------------------------------------
  207. // NOTCONSOLE
  208. <1> The number of the tokens that were invalidated as part of this request.
  209. <2> The number of tokens that were already invalidated.
  210. <3> The number of errors that were encountered when invalidating the tokens.
  211. <4> Details about these errors. This field is not present in the response when
  212. `error_count` is 0.