org.jets3t.service.utils.oauth
Class OAuthUtils

java.lang.Object
  extended by org.jets3t.service.utils.oauth.OAuthUtils

public class OAuthUtils
extends java.lang.Object

Utilties for obtaining OAuth authentication tokens.

Implementation is currently specific to the Google Storage OAuth 2.0 implementation, though hopefully generic enough it may be extensible in the future.

See Also:
Google Storage: OAuth 2.0 Authentication, Using OAuth 2.0 to Access Google APIs

Nested Class Summary
static class OAuthUtils.OAuthImplementation
          Which OAuth implementation to target.
 
Field Summary
protected  java.lang.String clientId
           
protected  java.lang.String clientSecret
           
protected static java.lang.String HTTP_USER_AGENT
           
protected  org.apache.http.client.HttpClient httpClient
           
protected  OAuthUtils.OAuthImplementation implementation
           
protected  org.codehaus.jackson.map.ObjectMapper jsonMapper
           
 
Constructor Summary
OAuthUtils(org.apache.http.client.HttpClient httpClient, OAuthUtils.OAuthImplementation implementation, java.lang.String clientId, java.lang.String clientSecret)
          Create utility class for a given OAuth implementation that will use the given client ID and Secret.
OAuthUtils(OAuthUtils.OAuthImplementation implementation, java.lang.String clientId, java.lang.String clientSecret)
          Create utility class for a given OAuth implementation that will use the given client ID and Secret.
OAuthUtils(OAuthUtils.OAuthImplementation implementation, java.lang.String clientId, java.lang.String clientSecret, Jets3tProperties jets3tProperties)
          Create utility class for a given OAuth implementation that will use the given client ID and Secret.
 
Method Summary
 java.lang.String generateBrowserUrlToAuthorizeNativeApplication(OAuthScope scope)
          Generate the URL for an OAuth authorization end-point that a person can visit in a web browser to authorize access to a storage resource, where access is limited to the given scope.
 java.lang.String getClientId()
           
 java.lang.String getClientSecret()
           
protected  java.util.Map<java.lang.String,java.lang.Object> performPostRequestAndParseJSONResponse(java.lang.String endpointUri, java.util.List<org.apache.http.NameValuePair> postParams)
          Performs an HTTP/S POST request to a given URL with the given POST parameters and parses the response document, which must be JSON, into a Map of name/value objects.
 OAuth2Tokens refreshOAuth2AccessToken(OAuth2Tokens tokens)
          Retrieve and return a refreshed access token from an OAuth2 token end-point using the refresh token in the provided tokens object.
 OAuth2Tokens retrieveOAuth2TokensFromAuthorization(java.lang.String authorizationCode)
          Swap the given authorization token for access/refresh tokens (and optional expiry time) from an OAuth token endpoint.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HTTP_USER_AGENT

protected static final java.lang.String HTTP_USER_AGENT
See Also:
Constant Field Values

httpClient

protected org.apache.http.client.HttpClient httpClient

jsonMapper

protected org.codehaus.jackson.map.ObjectMapper jsonMapper

implementation

protected OAuthUtils.OAuthImplementation implementation

clientId

protected java.lang.String clientId

clientSecret

protected java.lang.String clientSecret
Constructor Detail

OAuthUtils

public OAuthUtils(OAuthUtils.OAuthImplementation implementation,
                  java.lang.String clientId,
                  java.lang.String clientSecret,
                  Jets3tProperties jets3tProperties)
Create utility class for a given OAuth implementation that will use the given client ID and Secret. Values in the given Jets3tProperties object are used to configure HTTP/S connections that may be performed by this class.

Parameters:
implementation - OAuth implementation version
clientId - Client ID for installed application
clientSecret - Client secret for installed applications
jets3tProperties - Properties to configure HTTP/S connections

OAuthUtils

public OAuthUtils(org.apache.http.client.HttpClient httpClient,
                  OAuthUtils.OAuthImplementation implementation,
                  java.lang.String clientId,
                  java.lang.String clientSecret)
Create utility class for a given OAuth implementation that will use the given client ID and Secret. Values in the given Jets3tProperties object are used to configure HTTP/S connections that may be performed by this class.

Parameters:
httpClient - HTTP Client
implementation - OAuth implementation version
clientId - Client ID for installed application
clientSecret - Client secret for installed applications

OAuthUtils

public OAuthUtils(OAuthUtils.OAuthImplementation implementation,
                  java.lang.String clientId,
                  java.lang.String clientSecret)
Create utility class for a given OAuth implementation that will use the given client ID and Secret. Values in the default system Jets3tProperties object are used to configure HTTP/S connections that may be performed by this class.

Parameters:
implementation - OAuth implementation version
clientId - Client ID for installed application
clientSecret - Client secret for installed applications
Method Detail

getClientId

public java.lang.String getClientId()

getClientSecret

public java.lang.String getClientSecret()

generateBrowserUrlToAuthorizeNativeApplication

public java.lang.String generateBrowserUrlToAuthorizeNativeApplication(OAuthScope scope)
Generate the URL for an OAuth authorization end-point that a person can visit in a web browser to authorize access to a storage resource, where access is limited to the given scope. The URL will contain the Client ID stored in this class, along with other information that may be specific to the OAuth implementation.

Parameters:
scope - URI representing the access scope a user will be prompted to authorize, for example example
OAuthConstants.GSOAuth2_10.Scopes.ReadOnly
Returns:
URL to an OAuth authorization end-point.
See Also:
OAuth 2.0 for native applications

retrieveOAuth2TokensFromAuthorization

public OAuth2Tokens retrieveOAuth2TokensFromAuthorization(java.lang.String authorizationCode)
                                                   throws java.io.IOException
Swap the given authorization token for access/refresh tokens (and optional expiry time) from an OAuth token endpoint.

Parameters:
authorizationCode - token representing a pre-approved authorization (e.g. as might be generated by a user who visits the generateBrowserUrlToAuthorizeNativeApplication(OAuthScope) URL)
Returns:
object representing OAuth token and expiry data.
Throws:
java.io.IOException - Error receiving tokens

refreshOAuth2AccessToken

public OAuth2Tokens refreshOAuth2AccessToken(OAuth2Tokens tokens)
                                      throws java.io.IOException
Retrieve and return a refreshed access token from an OAuth2 token end-point using the refresh token in the provided tokens object.

Parameters:
tokens - OAuth token data that must include a valid refresh token.
Returns:
a new object containing the refreshed access token, an updated expiry timestamp (if applicable) and the original refresh token.
Throws:
java.io.IOException - Invalid response data

performPostRequestAndParseJSONResponse

protected java.util.Map<java.lang.String,java.lang.Object> performPostRequestAndParseJSONResponse(java.lang.String endpointUri,
                                                                                                  java.util.List<org.apache.http.NameValuePair> postParams)
                                                                                           throws java.io.IOException
Performs an HTTP/S POST request to a given URL with the given POST parameters and parses the response document, which must be JSON, into a Map of name/value objects.

Parameters:
endpointUri - Authorization or token endpoint
postParams - Name value pairs
Returns:
JSON mapped response
Throws:
org.apache.http.client.ClientProtocolException - No HTTP 200 response
java.io.IOException