OpenSearch/docs/java-rest/high-level/security/authenticate.asciidoc

67 lines
2.3 KiB
Plaintext

--
:api: authenticate
:response: AuthenticateResponse
--
[id="{upid}-{api}"]
=== Authenticate API
[id="{upid}-{api}-sync"]
==== Execution
Authenticating and retrieving information about a user can be performed
using the `security().authenticate()` method:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-execute]
--------------------------------------------------
This method does not require a request object. The client waits for the
+{response}+ to be returned before continuing with code execution.
[id="{upid}-{api}-response"]
==== Response
The returned +{response}+ contains two fields. Firstly, the `user` field
, accessed with `getUser`, contains all the information about this
authenticated user. The other field, `enabled`, tells if this user is actually
usable or has been temporalily deactivated.
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-response]
--------------------------------------------------
<1> `getUser` retrieves the `User` instance containing the information,
see {javadoc-client}/security/user/User.html.
<2> `enabled` tells if this user is usable or is deactivated.
[id="{upid}-{api}-async"]
==== Asynchronous Execution
This request can also be executed asynchronously:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-execute-async]
--------------------------------------------------
<1> The `ActionListener` to use when the execution completes. This method does
not require a request object.
The asynchronous method does not block and returns immediately. Once the request
has completed the `ActionListener` is called back using the `onResponse` method
if the execution completed successfully or using the `onFailure` method if
it failed.
A typical listener for a +{response}+ looks like:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests-file}[{api}-execute-listener]
--------------------------------------------------
<1> Called when the execution completed successfully. The response is
provided as an argument.
<2> Called in case of a failure. The exception is provided as an argument.