public final class OAuth2TokenManager
extends java.lang.Object
The OAuth2 flow can be configured using an instance of CommonAuthFlowsConfigurator
by calling its
supportOAuth2Using
and adding
so-called server support objects. Refer to the documentation of the OAuth2ServerSupport
interface and of the adder method to understand
how it works. In short: this allows for teaching the configurator how to orchestrate the OAuth2 authorization flow with a particular server.
Using the prepared CommonAuthFlowsConfigurator
(where 'prepared' means that the appropriate OAuth2
server support objects have been added to it), one can then configure a HttpConversationManager
. When the OAuth2
authorization flows run as part of the HTTP requests executed via this manager, the acquired tokens are going to be stored in the storage
specified for the configurator using the
setTokenStorage
method.
The below example summarizes all this:
Context context = ...; CommonAuthFlowsConfigurator configurator = new CommonAuthFlowsConfigurator(context); configurator.supportOAuth2Using(new SAPOAuth2ServerSupport(...)); configurator.setTokenStorage(new CustomOAuth2TokenStorage()); HttpConversationManager manager = configurator.configure(new HttpConversationManager(context));
In this example, a single OAuth2 server support, the SAPOAuth2ServerSupport
is added. This knows how to orchestrate the OAuth2 flow
against SAPcp servers. For the token storage, a new instance of CustomOAuth2TokenStorage
is used. This is just an example and assumes
that this storage is implemented by the application. It could be left out in case of which a default in-memory storage would be used.
Now, after the above configure
call the
manager
instance stands ready to fire requests against the server. If an OAuth2 challenge is detected then it will be handled using
the configured support object and the tokens will be stored in the above specified storage.
The OAuth2 token manager enters the picture at this point. It can be obtained using the CommonAuthFlowsConfigurator.getTokenManager()
method of the above configurator instance. It also contains the token storage and all the server support objects added to the configuration but
uses them for the purpose of managing the tokens.
This class is needed as a configurator is usually a short-lived object which is used only for configuring a HttpConversationManager
instance. After that, it can be thrown away. If the application wants to manage the access and refresh tokens acquired during the OAuth2
authorization flows manually then before throwing away the configurator its token manager instance should be saved. From then on, the token
manager and the configured conversation manager will belong together: the application can fire requests using the latter and have access to the
tokens using the former. Read on for the method-level documentations of this class for the details.
Constructor and Description |
---|
OAuth2TokenManager(OAuth2TokenStorage tokenStorage,
java.util.List<OAuth2ServerSupport> serverSupports,
ClientLogger logger)
Creates a new instance of the token manager given its arguments.
|
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.Future<java.lang.Boolean> |
deleteTokensForUrl(android.net.Uri url)
Removes the tokens for the specified URL.
|
java.util.concurrent.Future<java.lang.Boolean> |
removeAllTokens()
Removes all the tokens in the underlying storage.
|
java.util.concurrent.Future<OAuth2TokenWrapper> |
tokensForUrl(android.net.Uri url)
Returns the access and refresh tokens in the form of a wrapper object for the specified URL.
|
public OAuth2TokenManager(OAuth2TokenStorage tokenStorage, java.util.List<OAuth2ServerSupport> serverSupports, ClientLogger logger)
tokenStorage
- the token storage to talk to, must be non-nullserverSupports
- the list of server support objects, must be non-null and non-emptylogger
- a logger to use, must be non-nullpublic java.util.concurrent.Future<java.lang.Boolean> deleteTokensForUrl(android.net.Uri url)
url
- the URL for which access and refresh tokens should be removed, must be non-nilpublic java.util.concurrent.Future<java.lang.Boolean> removeAllTokens()
OAuth2TokenStorage.removeAllTokens()
, always non-nullpublic java.util.concurrent.Future<OAuth2TokenWrapper> tokensForUrl(android.net.Uri url)
url
- the URL for which access and refresh tokens should be acquired, must be non-null