create-token.asciidoc 3.6 KB

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