tokens.asciidoc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. [role="xpack"]
  2. [[security-api-tokens]]
  3. === Token Management APIs
  4. The `token` API enables you to create and invalidate bearer tokens for access
  5. without requiring basic authentication.
  6. ==== Request
  7. `POST /_xpack/security/oauth2/token` +
  8. `DELETE /_xpack/security/oauth2/token`
  9. ==== Description
  10. The tokens are created by the {es} Token Service, which is automatically enabled
  11. when you configure TLS on the HTTP interface. See <<tls-http>>. Alternatively,
  12. you can explicitly enable the `xpack.security.authc.token.enabled` setting. When
  13. you are running in production mode, a bootstrap check prevents you from enabling
  14. the token service unless you also enable TLS on the HTTP interface.
  15. The Get Token API takes the same parameters as a typical OAuth 2.0 token API
  16. except for the use of a JSON request body.
  17. A successful Get Token API call returns a JSON structure that contains the access
  18. token, the amount of time (seconds) that the token expires in, the type, and the
  19. scope if available.
  20. The tokens returned by the Get Token API have a finite period of time for which
  21. they are valid and after that time period, they can no longer be used. That time
  22. period is defined by the `xpack.security.authc.token.timeout` setting. For more
  23. information, see <<token-service-settings>>.
  24. If you want to invalidate a token immediately, you can do so by using the Delete
  25. Token API.
  26. ==== Request Body
  27. The following parameters can be specified in the body of a POST request and
  28. pertain to creating a token:
  29. `grant_type`::
  30. (string) The type of grant. Currently only the `password` grant type is supported.
  31. `password` (required)::
  32. (string) The user's password.
  33. `scope`::
  34. (string) The scope of the token. Currently tokens are only issued for a scope of
  35. `FULL` regardless of the value sent with the request.
  36. `username` (required)::
  37. (string) The username that identifies the user.
  38. The following parameters can be specified in the body of a DELETE request and
  39. pertain to deleting a token:
  40. `token`::
  41. (string) An access token.
  42. ==== Examples
  43. [[security-api-get-token]]
  44. To obtain a token, submit a POST request to the `/_xpack/security/oauth2/token`
  45. endpoint.
  46. [source,js]
  47. --------------------------------------------------
  48. POST /_xpack/security/oauth2/token
  49. {
  50. "grant_type" : "password",
  51. "username" : "test_admin",
  52. "password" : "x-pack-test-password"
  53. }
  54. --------------------------------------------------
  55. // CONSOLE
  56. The following example output contains the access token, the amount of time (in
  57. seconds) that the token expires in, and the type:
  58. [source,js]
  59. --------------------------------------------------
  60. {
  61. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  62. "type" : "Bearer",
  63. "expires_in" : 1200,
  64. "refresh_token": "vLBPvmAB6KvwvJZr27cS"
  65. }
  66. --------------------------------------------------
  67. // TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  68. // TESTRESPONSE[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  69. The token returned by this API can be used by sending a request with a
  70. `Authorization` header with a value having the prefix `Bearer ` followed
  71. by the value of the `access_token`.
  72. [source,shell]
  73. --------------------------------------------------
  74. curl -H "Authorization: Bearer dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==" http://localhost:9200/_cluster/health
  75. --------------------------------------------------
  76. // NOTCONSOLE
  77. [[security-api-refresh-token]]
  78. To extend the life of an existing token, the token api may be called again with the refresh
  79. token within 24 hours of the token's creation.
  80. [source,js]
  81. --------------------------------------------------
  82. POST /_xpack/security/oauth2/token
  83. {
  84. "grant_type": "refresh_token",
  85. "refresh_token": "vLBPvmAB6KvwvJZr27cS"
  86. }
  87. --------------------------------------------------
  88. // CONSOLE
  89. // TEST[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  90. // TEST[continued]
  91. The API will return a new token and refresh token. Each refresh token may only be used one time.
  92. [source,js]
  93. --------------------------------------------------
  94. {
  95. "access_token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  96. "type" : "Bearer",
  97. "expires_in" : 1200,
  98. "refresh_token": "vLBPvmAB6KvwvJZr27cS"
  99. }
  100. --------------------------------------------------
  101. // TESTRESPONSE[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  102. // TESTRESPONSE[s/vLBPvmAB6KvwvJZr27cS/$body.refresh_token/]
  103. [[security-api-invalidate-token]]
  104. If a token must be invalidated immediately, you can do so by submitting a DELETE
  105. request to `/_xpack/security/oauth2/token`. For example:
  106. [source,js]
  107. --------------------------------------------------
  108. DELETE /_xpack/security/oauth2/token
  109. {
  110. "token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ=="
  111. }
  112. --------------------------------------------------
  113. // CONSOLE
  114. // TEST[s/dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==/$body.access_token/]
  115. // TEST[continued]
  116. A successful call returns a JSON structure that indicates whether the token
  117. has already been invalidated.
  118. [source,js]
  119. --------------------------------------------------
  120. {
  121. "created" : true <1>
  122. }
  123. --------------------------------------------------
  124. // TESTRESPONSE
  125. <1> When a token has already been invalidated, `created` is set to false.