create-token.asciidoc 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. ["source","java",subs="attributes,callouts,macros"]
  42. --------------------------------------------------
  43. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-response]
  44. --------------------------------------------------
  45. <1> The `accessToken` can be used to authentication to Elasticsearch.
  46. <2> The `refreshToken` can be used in to create a new `CreateTokenRequest` with a `refresh_token` grant.
  47. [[java-rest-high-security-create-token-async]]
  48. ==== Asynchronous Execution
  49. This request can be executed asynchronously using the `security().createTokenAsync()`
  50. method:
  51. ["source","java",subs="attributes,callouts,macros"]
  52. --------------------------------------------------
  53. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-execute-async]
  54. --------------------------------------------------
  55. <1> The `CreateTokenRequest` to execute and the `ActionListener` to use when
  56. the execution completes
  57. The asynchronous method does not block and returns immediately. Once the request
  58. has completed the `ActionListener` is called back using the `onResponse` method
  59. if the execution successfully completed or using the `onFailure` method if
  60. it failed.
  61. A typical listener for a `CreateTokenResponse` looks like:
  62. ["source","java",subs="attributes,callouts,macros"]
  63. --------------------------------------------------
  64. include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-execute-listener]
  65. --------------------------------------------------
  66. <1> Called when the execution is successfully completed. The response is
  67. provided as an argument
  68. <2> Called in case of failure. The raised exception is provided as an argument