1616import com .paypal .core .credential .ICredential ;
1717import com .paypal .sdk .util .UserAgentHeader ;
1818
19+ /**
20+ * OAuthTokenCredential is used for generation of OAuth Token used by PayPal
21+ * REST API service. ClientID and ClientSecret are required by the class to
22+ * generate OAuth Token, the resulting token is of the form "Bearer xxxxxx". The
23+ * class has two constructors, one of it taking an additional configuration map
24+ * used for dynamic configuration. When using the constructor with out
25+ * configuration map the endpoint is fetched from the configuration that is used
26+ * during initialization. See {@link PayPalResource} for configuring the system.
27+ * When using a configuration map the class expects an entry by the name
28+ * "oauth.EndPoint" or "service.EndPoint" to retrieve the value of the endpoint
29+ * for the OAuth Service. If either are not present the configuration should
30+ * have a entry by the name "mode" with values sandbox or live wherein the
31+ * corresponding endpoints are default to PayPal endpoints.
32+ *
33+ * @author kjayakumar
34+ *
35+ */
1936public final class OAuthTokenCredential implements ICredential {
2037
2138 /**
2239 * OAuth URI path parameter
2340 */
24- private static final String OAUTH_TOKEN_PATH = "/v1/oauth2/token" ;
41+ private static String OAUTH_TOKEN_PATH = "/v1/oauth2/token" ;
2542
2643 /**
2744 * Client ID for OAuth
@@ -43,6 +60,17 @@ public final class OAuthTokenCredential implements ICredential {
4360 */
4461 private Map <String , String > configurationMap ;
4562
63+ /**
64+ * Sets the URI path for the OAuth Token service. If not set it defaults to
65+ * "/v1/oauth2/token"
66+ *
67+ * @param oauthTokenPath
68+ * the URI part to set
69+ */
70+ public static void setOAUTH_TOKEN_PATH (String oauthTokenPath ) {
71+ OAUTH_TOKEN_PATH = oauthTokenPath ;
72+ }
73+
4674 /**
4775 * @param clientID
4876 * Client ID for the OAuth
@@ -58,10 +86,17 @@ public OAuthTokenCredential(String clientID, String clientSecret) {
5886 }
5987
6088 /**
89+ * Configuration Constructor for dynamic configuration
90+ *
6191 * @param clientID
6292 * Client ID for the OAuth
6393 * @param clientSecret
6494 * Client Secret for OAuth
95+ * @param configurationMap
96+ * Dynamic configuration map which should have an entry for
97+ * 'oauth.EndPoint' or 'service.EndPoint'. If either are not
98+ * present then there should be entry for 'mode' with values
99+ * sandbox/live, wherein PayPals endpoints are used.
65100 */
66101 public OAuthTokenCredential (String clientID , String clientSecret ,
67102 Map <String , String > configurationMap ) {
@@ -73,16 +108,13 @@ public OAuthTokenCredential(String clientID, String clientSecret,
73108
74109 /**
75110 * Computes Access Token by placing a call to OAuth server using ClientID
76- * and ClientSecret. The token is appended to the token type.
111+ * and ClientSecret. The token is appended to the token type (Bearer) .
77112 *
78113 * @return the accessToken
79114 * @throws PayPalRESTException
80115 */
81116 public String getAccessToken () throws PayPalRESTException {
82117 if (accessToken == null ) {
83- // Write Logic for passing in Detail to Identity Api Serv and
84- // computing the token
85- // Set the Value inside the accessToken and result
86118 accessToken = generateAccessToken ();
87119 }
88120 return accessToken ;
@@ -125,12 +157,13 @@ private String generateOAuthToken(String base64ClientID)
125157 httpConfiguration = getOAuthHttpConfiguration ();
126158 connection .createAndconfigureHttpConnection (httpConfiguration );
127159 Map <String , String > headers = new HashMap <String , String >();
128- headers .put (Constants .AUTHORIZATION_HEADER , "Basic " + base64ClientID );
160+ headers .put (Constants .AUTHORIZATION_HEADER , "Basic "
161+ + base64ClientID );
129162 headers .put (Constants .HTTP_ACCEPT_HEADER , "*/*" );
130163 UserAgentHeader userAgentHeader = new UserAgentHeader (
131164 PayPalResource .SDK_ID , PayPalResource .SDK_VERSION );
132165 headers .putAll (userAgentHeader .getHeader ());
133- String postRequest = "grant_type=client_credentials" ;
166+ String postRequest = getRequestPayload () ;
134167 String jsonResponse = connection .execute ("" , postRequest , headers );
135168 JsonParser parser = new JsonParser ();
136169 JsonElement jsonElement = parser .parse (jsonResponse );
@@ -145,16 +178,25 @@ private String generateOAuthToken(String base64ClientID)
145178 return generatedToken ;
146179 }
147180
181+ /**
182+ * Returns the request payload for OAuth Service. Override this method to
183+ * alter the payload
184+ *
185+ * @return Payload as String
186+ */
187+ protected String getRequestPayload () {
188+ return "grant_type=client_credentials" ;
189+ }
190+
148191 /*
149192 * Get HttpConfiguration object for OAuth server
150193 */
151194 private HttpConfiguration getOAuthHttpConfiguration () {
152195 HttpConfiguration httpConfiguration = new HttpConfiguration ();
153196 httpConfiguration
154197 .setHttpMethod (Constants .HTTP_CONFIG_DEFAULT_HTTP_METHOD );
155- String endPointUrl = (configurationMap .get (Constants .OAUTH_ENDPOINT ) != null
156- && configurationMap .get (Constants .OAUTH_ENDPOINT ).trim ()
157- .length () >= 0 ) ? configurationMap
198+ String endPointUrl = (configurationMap .get (Constants .OAUTH_ENDPOINT ) != null && configurationMap
199+ .get (Constants .OAUTH_ENDPOINT ).trim ().length () >= 0 ) ? configurationMap
158200 .get (Constants .OAUTH_ENDPOINT ) : configurationMap
159201 .get (Constants .ENDPOINT );
160202 if (endPointUrl == null || endPointUrl .trim ().length () <= 0 ) {
0 commit comments