2121import com .google .common .collect .ImmutableSet ;
2222import com .google .gson .JsonElement ;
2323import io .cdap .plugin .http .common .BaseHttpConfig ;
24+ import io .cdap .plugin .http .common .OAuthClientAuthentication ;
2425import io .cdap .plugin .http .common .OAuthGrantType ;
2526import io .cdap .plugin .http .common .pagination .page .JSONUtil ;
2627import io .cdap .plugin .http .source .common .BaseHttpSourceConfig ;
3031import org .apache .http .client .utils .URIBuilder ;
3132import org .apache .http .impl .client .CloseableHttpClient ;
3233import org .apache .http .impl .client .HttpClients ;
33- import org .apache .http .message .BasicHeader ;
3434import org .apache .http .message .BasicNameValuePair ;
3535import org .apache .http .util .EntityUtils ;
3636
4141import java .net .URI ;
4242import java .net .URISyntaxException ;
4343import java .nio .charset .StandardCharsets ;
44+ import java .time .Duration ;
4445import java .time .Instant ;
4546import java .util .ArrayList ;
46- import java .util .Base64 ;
4747import java .util .Date ;
4848import java .util .List ;
49+ import java .util .Objects ;
4950import javax .annotation .Nullable ;
5051
5152/**
@@ -101,32 +102,30 @@ private static AccessToken getAccessTokenByClientCredentials(CloseableHttpClient
101102 String tokenUrl , String clientId , String clientSecret , String scope ,
102103 String clientAuthentication ) throws IOException {
103104 URI uri ;
105+ HttpPost httppost ;
104106 try {
105- uri = new URIBuilder (tokenUrl ).build ();
107+ if (Objects .equals (clientAuthentication , OAuthClientAuthentication .BODY .getValue ())) {
108+ uri = new URIBuilder (tokenUrl ).build ();
109+ List <BasicNameValuePair > nameValuePairs = new ArrayList <>();
110+ nameValuePairs .add (new BasicNameValuePair ("scope" , scope ));
111+ nameValuePairs .add (
112+ new BasicNameValuePair ("grant_type" , OAuthGrantType .CLIENT_CREDENTIALS .getValue ()));
113+ nameValuePairs .add (new BasicNameValuePair ("client_id" , clientId ));
114+ nameValuePairs .add (new BasicNameValuePair ("client_secret" , clientSecret ));
115+ httppost = new HttpPost (uri );
116+ httppost .setEntity (new UrlEncodedFormEntity (nameValuePairs ));
117+ } else {
118+ uri = new URIBuilder (tokenUrl ).setParameter ("client_id" , clientId )
119+ .setParameter ("client_secret" , clientSecret )
120+ .setParameter ("grant_type" , OAuthGrantType .CLIENT_CREDENTIALS .getValue ()).build ();
121+ httppost = new HttpPost (uri );
122+ }
106123 } catch (URISyntaxException e ) {
107124 throw new IllegalArgumentException (
108125 "Failed to build access token URI for OAuth2 with grant type = "
109126 + OAuthGrantType .CLIENT_CREDENTIALS .getValue (), e );
110- } catch (NullPointerException e ) {
111- throw new IllegalArgumentException (
112- "One or more required OAuth2 parameters (Client ID, Client Secret, "
113- + "or Token URL) are missing." , e );
114127 }
115128
116- HttpPost httppost = new HttpPost (uri );
117- List <BasicNameValuePair > nameValuePairs = new ArrayList <>();
118- nameValuePairs .add (new BasicNameValuePair ("scope" , scope ));
119- nameValuePairs .add (
120- new BasicNameValuePair ("grant_type" , OAuthGrantType .CLIENT_CREDENTIALS .getValue ()));
121- nameValuePairs .add (new BasicNameValuePair ("client_authentication" , clientAuthentication ));
122-
123- httppost .setEntity (new UrlEncodedFormEntity (nameValuePairs ));
124-
125- String authorizationKey =
126- "Basic " + Base64 .getEncoder ().encodeToString ((clientId + ":" + clientSecret ).getBytes ());
127-
128- httppost .addHeader (new BasicHeader ("Authorization" , authorizationKey ));
129-
130129 CloseableHttpResponse response = httpclient .execute (httppost );
131130 String responseString = EntityUtils .toString (response .getEntity (), "UTF-8" );
132131
@@ -139,9 +138,12 @@ private static AccessToken getAccessTokenByClientCredentials(CloseableHttpClient
139138 JsonElement expiresInElement = JSONUtil .toJsonObject (responseString ).get ("expires_in" );
140139 Date expiresInDate = null ;
141140 if (expiresInElement != null ) {
142- long expiresAtMilliseconds =
143- System .currentTimeMillis () + (long ) (expiresInElement .getAsInt () * 1000 ) - 60000L ;
144- expiresInDate = new Date (expiresAtMilliseconds );
141+ Instant now = Instant .now ();
142+ Duration expiresIn = Duration .ofSeconds (expiresInElement .getAsInt ());
143+ Duration buffer = Duration .ofMinutes (1 );
144+
145+ Instant expiresAt = now .plus (expiresIn ).minus (buffer );
146+ expiresInDate = Date .from (expiresAt );
145147 }
146148
147149 return new AccessToken (accessTokenElement .getAsString (), expiresInDate );
@@ -188,10 +190,6 @@ public static AccessToken getAccessTokenByRefreshToken(CloseableHttpClient httpc
188190 .build ();
189191 } catch (URISyntaxException e ) {
190192 throw new IllegalArgumentException ("Failed to build token URI for OAuth2" , e );
191- } catch (NullPointerException e ) {
192- throw new IllegalArgumentException (
193- "One or more required OAuth2 parameters (Auth URL, Token URL, Client ID, Client Secret, "
194- + "or Refresh Token) are missing." , e );
195193 }
196194
197195 HttpPost httppost = new HttpPost (uri );
@@ -206,9 +204,12 @@ public static AccessToken getAccessTokenByRefreshToken(CloseableHttpClient httpc
206204 JsonElement expiresInElement = JSONUtil .toJsonObject (responseString ).get ("expires_in" );
207205 Date expiresInDate = null ;
208206 if (expiresInElement != null ) {
209- long expiresAtMilliseconds = System .currentTimeMillis ()
210- + (long ) (expiresInElement .getAsInt () * 1000 ) - 60000L ;
211- expiresInDate = new Date (expiresAtMilliseconds );
207+ Instant now = Instant .now ();
208+ Duration expiresIn = Duration .ofSeconds (expiresInElement .getAsInt ());
209+ Duration buffer = Duration .ofMinutes (1 );
210+
211+ Instant expiresAt = now .plus (expiresIn ).minus (buffer );
212+ expiresInDate = Date .from (expiresAt );
212213 }
213214
214215 return new AccessToken (accessTokenElement .getAsString (), expiresInDate );
0 commit comments