Class OAuth2
Represents a single OAuth2 authorization. To use this class you'll typically
need to create it then add it to an OAuth2AuthenticationURLConnectionProcessor
,
which is then added to either ReportFactory.getURLConnectionProcessors()
or
a custom list of URLConnectionProcessor
objects set on a custom
URLConnectionFactory
.
Most importantly it will need a CallbackHandler
which is able to handle
OAuth2Callback
objects. The OAuth2StandaloneCallbackHandler
is an
example which does this - when its OAuth2StandaloneCallbackHandler.handle(javax.security.auth.callback.Callback[])
method
is called, it creates a local webserver on the specified port and writes a prompt to
stdout with the URL to visit to authorize the request.
The configuration for OAuth2 is done with a Json structure. The following fields can be included
- client_id - a string representing the
client_id
, which is normally supplied by the service - client_secret - a string representing the
client_secret
, which is normally supplied by the service - scope - the scope token, a single string with one or more words seperated by spaces
- redirect_uri - the redirection endpoint URL, a URL which will be preregistered with the OAuth2 service - for example
http://127.0.0.1/oauth2
- auth_uri - the URL to call to retrieve an authorization token
- token_uri - the URL to call with the authorization token to retrieve an access token
- authorization_method - if the string "inline", the
client_id
andclient_secret
will be sent in the JSON object, otherwise they will be sent as an HTTPAuthorization: Bearer
header - protocol.auth - an option map of extra strings to be included in requests to the "auth_uri"
- protocol.grant - an option map of extra strings to be included in requests to the "token_uri"
- protocol.refresh - an option map of extra strings to be included in requests to the "token_uri" when refreshing a token
-
Constructor Summary
ConstructorDescriptionOAuth2()
Create a new OAuth2OAuth2
(Random random, CallbackHandler handler) Create a new OAuth2 -
Method Summary
Modifier and TypeMethodDescriptiongetAccessToken
(CallbackHandler handler) Return the "access_token" from thegetAuthorization(javax.security.auth.callback.CallbackHandler)
method.Json
getAuthorization
(CallbackHandler handler) Return the authorization response from the OAuth2 server.boolean
If the OAuth2 configuration supplied inload(Json)
has been updated with new tokens, since the last time this method was called, return true.void
load
(Json json) Load the OAuth2 configuration.
-
Constructor Details
-
OAuth2
public OAuth2()Create a new OAuth2 -
OAuth2
Create a new OAuth2- Parameters:
random
- theSecureRandom
for random number generation, typicallyReportFactory.getRandom()
handler
- theCallbackHandler
which must supportOAuth2Callback
objects, or null to use the one on the Report
-
-
Method Details
-
load
public void load(Json json) Load the OAuth2 configuration. A reference to the supplied Json object is kept, and the structure will be updated and should be saved ifisUpdated()
is true- Parameters:
json
- a Json which must have "client_id", "client_secret", "scope", "redirect_uri", "auth_uri" and "token_uri" strings. It may have an "authorization_method" string set to "inline"; It may have an "authorization" map with one or more of "access_token", "token_type", "refresh_token", "expires"; and it may have "protocol.auth", "protocol.grant" or "protocol.refresh" maps
-
isUpdated
public boolean isUpdated()If the OAuth2 configuration supplied inload(Json)
has been updated with new tokens, since the last time this method was called, return true. -
getAccessToken
Return the "access_token" from thegetAuthorization(javax.security.auth.callback.CallbackHandler)
method.- Throws:
IOException
-
getAuthorization
Return the authorization response from the OAuth2 server. The returned Json should have "access_token" and other properties returned from the server, except that "expires_in" is replaced with "expires" which is milliseconds since the epoch, and "id_token", if present, has been decoded. This method may block if the response needs to be requested or refreshed- Throws:
IOException
-