create-token.asciidoc 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. [role="xpack"]
  2. [[java-rest-high-security-create-token]]
  3. === Create Token API
  4. [[java-rest-high-security-create-token-request]]
  5. ==== Request
  6. The `CreateTokenRequest` supports three different OAuth2 _grant types_:
  7. ===== Password Grants
  8. ["source","java",subs="attributes,callouts,macros"]
  9. --------------------------------------------------
  10. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-password-request]
  11. --------------------------------------------------
  12. ===== Refresh Token Grants
  13. ["source","java",subs="attributes,callouts,macros"]
  14. --------------------------------------------------
  15. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-refresh-request]
  16. --------------------------------------------------
  17. ===== Client Credential Grants
  18. ["source","java",subs="attributes,callouts,macros"]
  19. --------------------------------------------------
  20. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-client-credentials-request]
  21. --------------------------------------------------
  22. [[java-rest-high-security-create-token-execution]]
  23. ==== Execution
  24. Creating a OAuth2 security token can be performed by passing the appropriate request to the
  25. `security().createToken()` method:
  26. ["source","java",subs="attributes,callouts,macros"]
  27. --------------------------------------------------
  28. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-execute]
  29. --------------------------------------------------
  30. [[java-rest-high-security-create-token-response]]
  31. ==== Response
  32. The returned `CreateTokenResponse` contains the following properties:
  33. `accessToken`:: This is the newly created access token.
  34. It can be used to authenticate to the Elasticsearch cluster.
  35. `type`:: The type of the token, this is always `"Bearer"`.
  36. `expiresIn`:: The length of time until the token will expire.
  37. The token will be considered invalid after that time.
  38. `scope`:: The scope of the token. May be `null`.
  39. `refreshToken`:: A secondary "refresh" token that may be used to extend
  40. the life of an access token. May be `null`.
  41. `authentication`:: This is the authentication object for the newly created token. See also
  42. <<{upid}-authenticate-response, authenticate response>> for details.
  43. ["source","java",subs="attributes,callouts,macros"]
  44. --------------------------------------------------
  45. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-response]
  46. --------------------------------------------------
  47. <1> The `accessToken` can be used to authentication to Elasticsearch.
  48. <2> The `refreshToken` can be used in to create a new `CreateTokenRequest` with a `refresh_token` grant.
  49. [[java-rest-high-security-create-token-async]]
  50. ==== Asynchronous Execution
  51. This request can be executed asynchronously using the `security().createTokenAsync()`
  52. method:
  53. ["source","java",subs="attributes,callouts,macros"]
  54. --------------------------------------------------
  55. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-execute-async]
  56. --------------------------------------------------
  57. <1> The `CreateTokenRequest` to execute and the `ActionListener` to use when
  58. the execution completes
  59. The asynchronous method does not block and returns immediately. Once the request
  60. has completed the `ActionListener` is called back using the `onResponse` method
  61. if the execution successfully completed or using the `onFailure` method if
  62. it failed.
  63. A typical listener for a `CreateTokenResponse` looks like:
  64. ["source","java",subs="attributes,callouts,macros"]
  65. --------------------------------------------------
  66. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-execute-listener]
  67. --------------------------------------------------
  68. <1> Called when the execution is successfully completed. The response is
  69. provided as an argument
  70. <2> Called in case of failure. The raised exception is provided as an argument